Passed
Pull Request — master (#58)
by Keoghan
04:15
created

Xdebug::disable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace App\Support\Php;
4
5
use App\Models\PhpVersion;
6
use App\PorterLibrary;
7
use Illuminate\Filesystem\Filesystem;
8
9
class Xdebug
10
{
11
    /** @var PorterLibrary */
12
    protected $porterLibrary;
13
    /** @var Filesystem */
14
    protected $filesystem;
15
16
    protected $activationSwitches = [
17
        'xdebug.remote_enable',
18
        'xdebug.default_enable',
19
    ];
20
21 4
    public function __construct(PorterLibrary $porterLibrary, Filesystem $filesystem)
22
    {
23 4
        $this->porterLibrary = $porterLibrary;
24 4
        $this->filesystem = $filesystem;
25 4
    }
26
27
    /**
28
     * Turn Xdebug on for all PHP versions, on the one passed.
29
     * We are just editing the config, rather than disabling wholesale.
30
     *
31
     * @param PhpVersion|null $phpVersion
32
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
33
     */
34 2
    public function turnOn(PhpVersion $phpVersion = null)
35
    {
36 2
        $phpVersions = $phpVersion ? collect([$phpVersion]) : PhpVersion::all();
37 2
        $config = $this->porterLibrary->configPath() . '/';
38
39 2
        foreach ($phpVersions as $phpVersion) {
40 2
            $this->enable($config . $phpVersion->cli_name);
41 2
            $this->enable($config . $phpVersion->fpm_name);
42
        }
43 2
    }
44
45
    /**
46
     * Turn Xdebug off for all PHP versions, on the one passed.
47
     * We are just editing the config, rather than disabling wholesale.
48
     *
49
     * @param PhpVersion|null $phpVersion
50
     */
51 2
    public function turnOff(PhpVersion $phpVersion = null)
52
    {
53 2
        $phpVersions = $phpVersion ? collect([$phpVersion]) : PhpVersion::all();
54 2
        $config = $this->porterLibrary->configPath() . '/';
55
56 2
        foreach ($phpVersions as $phpVersion) {
57 2
            $this->disable($config . $phpVersion->cli_name);
58 2
            $this->disable($config . $phpVersion->fpm_name);
59
        }
60 2
    }
61
62
    /**
63
     * Edit the xdebug.ini file to enable the xdebug
64
     *
65
     * @param string $path
66
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
67
     */
68 2
    protected function enable(string $path)
69
    {
70 2
        $file = $path. '/xdebug.ini';
71
72 2
        $contents = $this->filesystem->get($file);
73
74 2
        foreach ($this->activationSwitches as $key) {
75 2
            $contents = str_replace($key.'=0', $key.'=1', $contents);
76
        }
77
78 2
        $this->filesystem->put($file, $contents);
79 2
    }
80
81
    /**
82
     * Edit the xdebug.ini file to disable xdebug
83
     *
84
     * @param string $path
85
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
86
     */
87 2
    protected function disable(string $path)
88
    {
89 2
        $file = $path. '/xdebug.ini';
90
91 2
        $contents = $this->filesystem->get($file);
92
93 2
        foreach ($this->activationSwitches as $key) {
94 2
            $contents = str_replace($key.'=1', $key.'=0', $contents);
95
        }
96
97 2
        $this->filesystem->put($file, $contents);
98 2
    }
99
}
100