Passed
Push — master ( f4248b...2066b0 )
by Théo
02:19
created

PhpSettingsHandler::restart()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
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 $logger;
27
    private $required;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function __construct(LoggerInterface $logger)
33
    {
34
        parent::__construct('box', '--ansi');
35
36
        $this->setLogger($logger);
37
        $this->logger = $logger;
38
39
        $this->required = (bool) ini_get('phar.readonly');
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function requiresRestart($isLoaded)
46
    {
47
        return $this->required || $isLoaded;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function restart($command): void
54
    {
55
        if ($this->required) {
56
            if (false === @file_put_contents($this->tmpIni, 'phar.readonly=0'.PHP_EOL, FILE_APPEND)) {
57
                throw new IOException(
58
                    sprintf('Failed to write file "%s".', $this->tmpIni),
59
                    0,
60
                    null,
61
                    $this->tmpIni
62
                );
63
            }
64
65
            $this->logger->debug('Configured `phar.readonly=0`');
66
        }
67
68
        parent::restart($command);
69
    }
70
}
71