Passed
Pull Request — master (#91)
by Théo
02:17
created

PhpSettingsHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A requiresRestart() 0 3 2
A restart() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box;
16
17
use Composer\XdebugHandler\XdebugHandler;
18
use Psr\Log\LoggerInterface;
19
use Symfony\Component\Filesystem\Exception\IOException;
20
use const FILE_APPEND;
21
use const PHP_EOL;
22
use function file_put_contents;
23
24
final class PhpSettingsHandler extends XdebugHandler
25
{
26
    private $required;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function __construct(LoggerInterface $logger)
32
    {
33
        parent::__construct('box', '--ansi');
34
35
        $this->setLogger($logger);
36
37
        $this->required = (bool) ini_get('phar.readonly');
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function requiresRestart($isLoaded)
44
    {
45
        return $this->required || $isLoaded;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function restart($command): void
52
    {
53
        if ($this->required) {
54
            if (false === @file_put_contents($this->tmpIni, 'phar.readonly=0'.PHP_EOL, FILE_APPEND)) {
55
                throw new IOException(
56
                    sprintf('Failed to write file "%s".', $this->tmpIni),
57
                    0,
58
                    null,
59
                    $this->tmpIni
60
                );
61
            }
62
63
            // TODO: log that phar.readonly was appended: https://github.com/composer/xdebug-handler/pull/51
64
        }
65
66
        parent::restart($command);
67
    }
68
}
69