Completed
Push — master ( d40925...50bfa9 )
by Felix
02:26
created

PhpIniSetter::getFilePath()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.2
cc 4
eloc 3
nc 8
nop 1
crap 4
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
    /**
49
     * @param InputInterface $input
50
     * @param OutputInterface $output
51
     * @return int
52
     */
53 4
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55 4
        if (!($filePath = $this->getFilePath($input))) {
56 3
            $output->writeln('<error>The specified php.ini file does not exist or is not writeable</error>');
57 3
            return -1;
58
        }
59
60 1
        $configKey = $input->getArgument('configKey');
61 1
        $configLine = $configKey . " = " . $input->getArgument('configValue') . "\n";
62
63 1
        $lines = [];
64 1
        $configSet = false;
65 1
        foreach (file($filePath) as $line) {
66 1
            if ($this->isLineSpecifiedConfigLine($line, $configKey)) {
67 1
                $line = $configLine;
68 1
                $configSet = true;
69
            }
70 1
            $lines[] = $line;
71
        }
72
73 1
        if (!$configSet) {
74 1
            $lines[] = $configLine;
75
        }
76
77 1
        file_put_contents($filePath, implode($lines));
78 1
        $output->writeln('<info>The php.ini file has been updated</info>');
79
80 1
        return 0;
81
    }
82
    
83
    /**
84
     * @param string $line
85
     * @param string $configKey
86
     * @return bool
87
     */
88 1
    protected function isLineSpecifiedConfigLine($line, $configKey)
89
    {
90 1
        $line = trim($line);
91 1
        if (substr($line, 0, 1) == ';') {
92 1
            return false;
93
        }
94 1
        $configKeyLine = strtolower($configKey . '=');
95 1
        $line = str_replace(' ', '', $line) . '=';
96 1
        $configLineKeyPart = strtolower(substr($line, 0, strlen($configKeyLine)));
97
        
98 1
        return $configLineKeyPart == $configKeyLine;
99
    }
100
    
101
    /**
102
     * @param InputInterface $input
103
     * @return string
104
     */
105 1
    protected function getFilePath(InputInterface $input)
106
    {
107 1
        $filePath = $input->getOption('file') ?: php_ini_loaded_file();
108
        
109 1
        return (!is_file($filePath) || !is_writable($filePath)) ? '' : $filePath;
110
    }
111
}
112