File: RestarterMode.php

package info (click to toggle)
php-composer-xdebug-handler 3.0.5-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 460 kB
  • sloc: php: 2,501; makefile: 18
file content (81 lines) | stat: -rw-r--r-- 2,357 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php

declare(strict_types=1);

/*
 * This file is part of composer/xdebug-handler.
 *
 * (c) Composer <https://github.com/composer>
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Composer\XdebugHandler\Tests\App\Helpers;

use Composer\Pcre\Preg;
use Composer\XdebugHandler\XdebugHandler;
use Composer\XdebugHandler\Process;

/**
 * Restarts with xdebug loaded in coverage mode, by uncommenting xdebug in the
 * temporary ini file and setting the XDEBUG_MODE environment variable.
 *
 */

class RestarterMode extends XdebugHandler
{
    protected function requiresRestart(bool $default): bool
    {
        if ($default) {
            $version = (string) phpversion('xdebug');

            if (version_compare($version, '3.1', '>=')) {
                $modes = xdebug_info('mode');
                return !in_array('coverage', $modes, true);
            }

            // See if xdebug.mode is supported in this version
            $iniMode = ini_get('xdebug.mode');
            if ($iniMode === false) {
                return false;
            }

            // Environment value wins but cannot be empty
            $envMode = (string) getenv('XDEBUG_MODE');
            if ($envMode !== '') {
                $mode = $envMode;
            } else {
                $mode = $iniMode !== '' ? $iniMode : 'off';
            }

            // An empty comma-separated list is treated as mode 'off'
            if (Preg::isMatch('/^,+$/', str_replace(' ', '', $mode))) {
                $mode = 'off';
            }

            $modes = explode(',', str_replace(' ', '', $mode));
            return !in_array('coverage', $modes, true);
        }

        return false;
    }

    protected function restart(array $command): void
    {
        // uncomment last xdebug line
        $regex = '/^;\s*(zend_extension\s*=.*xdebug.*)$/mi';
        $content = (string) file_get_contents((string) $this->tmpIni);

        if (Preg::isMatchAll($regex, $content, $matches)) {
            $index = count($matches[1]) -1;
            $line = $matches[1][$index];
            $content .= $line.PHP_EOL;
        }

        file_put_contents((string) $this->tmpIni, $content);
        Process::setEnv('XDEBUG_MODE', 'coverage');

        parent::restart($command);
    }
}