PhpIniSetter::isLineSpecifiedConfigLine()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * PhpIniSetter.
4
 *
5
 * @copyright Copyright (c) 2016 Felix Sandström
6
 * @license   MIT
7
 */
8
9
namespace PhpIniSetter;
10
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * @author Felix Sandström <http://github.com/felixsand>
19
 */
20
class PhpIniSetter extends Command
21
{
22
    /**
23
     */
24 4
    protected function configure()
25
    {
26
        $this
27 4
            ->setName('phpIni:set')
28 4
            ->setDescription('Set a php.ini')
29 4
            ->addArgument(
30 4
                'configKey',
31 4
                InputArgument::REQUIRED,
32 4
                'The key of the setting to be changed'
33
            )
34 4
            ->addArgument(
35 4
                'configValue',
36 4
                InputArgument::REQUIRED,
37 4
                'The value to change the setting to'
38
            )
39 4
            ->addOption(
40 4
                'file',
41 4
                null,
42 4
                InputOption::VALUE_REQUIRED,
43 4
                'If set, the specified php.ini file will be used instead of the loaded one.'
44
            )
45
        ;
46 4
    }
47
48 4
    protected function execute(InputInterface $input, OutputInterface $output): int
49
    {
50 4
        if (!($filePath = $this->getFilePath($input))) {
51 3
            $output->writeln('<error>The specified php.ini file does not exist or is not writeable</error>');
52 3
            return -1;
53
        }
54
55 1
        $configKey = $input->getArgument('configKey');
56 1
        $configLine = $configKey . " = " . $input->getArgument('configValue') . "\n";
57
58 1
        $lines = [];
59 1
        $configSet = false;
60 1
        foreach (file($filePath) as $line) {
61 1
            if ($this->isLineSpecifiedConfigLine($line, $configKey)) {
62 1
                $line = $configLine;
63 1
                $configSet = true;
64
            }
65 1
            $lines[] = $line;
66
        }
67
68 1
        if (!$configSet) {
69 1
            $lines[] = $configLine;
70
        }
71
72 1
        file_put_contents($filePath, implode($lines));
73 1
        $output->writeln('<info>The php.ini file has been updated</info>');
74
75 1
        return 0;
76
    }
77
78 1
    protected function isLineSpecifiedConfigLine(string $line, string $configKey): bool
79
    {
80 1
        $line = trim($line);
81 1
        if (substr($line, 0, 1) == ';') {
82 1
            return false;
83
        }
84 1
        $configKeyLine = strtolower($configKey . '=');
85 1
        $line = str_replace(' ', '', $line);
86 1
        $configLineKeyPart = strtolower(substr($line, 0, strlen($configKeyLine)));
87
        
88 1
        return ($configLineKeyPart == $configKeyLine);
89
    }
90
91 1
    protected function getFilePath(InputInterface $input): string
92
    {
93 1
        $filePath = $input->getOption('file') ?: php_ini_loaded_file();
94
        
95 1
        return (!is_file($filePath) || !is_writable($filePath)) ? '' : $filePath;
96
    }
97
}
98