Completed
Push — master ( c325ad...10ac41 )
by Felix
02:50
created

PhpIniSetter::execute()   C

Complexity

Conditions 7
Paths 14

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 30
ccs 19
cts 19
cp 1
rs 6.7272
cc 7
eloc 19
nc 14
nop 2
crap 7
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
        $filePath = $input->getOption('file') ?: php_ini_loaded_file();
56 4
        if (!is_file($filePath) || !is_writable($filePath)) {
57 3
            $output->writeln('<error>The specified php.ini file does not exist or is not writeable</error>');
58 3
            return -1;
59
        }
60
61 1
        $configKey = $input->getArgument('configKey');
62 1
        $configLine = $configKey . " = " . $input->getArgument('configValue') . "\n";
63
64 1
        $lines = [];
65 1
        $configSet = false;
66 1
        foreach (file($filePath) as $line) {
67 1
            if (strtolower(substr(trim($line), 0, strlen($configKey))) == strtolower($configKey)) {
68 1
                $line = $configLine;
69 1
                $configSet = true;
70
            }
71 1
            $lines[] = $line;
72
        }
73
74 1
        if (!$configSet) {
75 1
            $lines[] = $configLine;
76
        }
77
78 1
        file_put_contents($filePath, implode($lines));
79 1
        $output->writeln('<info>The php.ini file has been updated</info>');
80
81 1
        return 0;
82
    }
83
}
84