Completed
Push — master ( cfe8fe...f9982c )
by Rougin
04:59
created

InstallCommand::addLibrary()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 8.9713
cc 2
eloc 13
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Rougin\Combustor\Common\Commands;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
use Rougin\Combustor\Common\Tools;
9
use Rougin\Combustor\Commands\AbstractCommand;
10
11
/**
12
 * Install Command
13
 *
14
 * Installs Doctrine/Wildfire library for CodeIgniter.
15
 * 
16
 * @package Combustor
17
 * @author  Rougin Royce Gutib <[email protected]>
18
 */
19
class InstallCommand extends AbstractCommand
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $library = '';
25
26
    /**
27
     * Checks whether the command is enabled or not in the current environment.
28
     *
29
     * Override this to check for x or y and return false if the command can not
30
     * run properly under the current conditions.
31
     *
32
     * @return boolean
33
     */
34 6
    public function isEnabled()
35
    {
36 6
        $library = ucfirst($this->library);
37
38 6
        return ! file_exists(APPPATH . 'libraries/' . $library . '.php');
39
    }
40
41
    /**
42
     * Sets the configurations of the specified command.
43
     *
44
     * @return void
45
     */
46 30
    protected function configure()
47
    {
48 30
        $this
49 30
            ->setName('install:' . $this->library)
50 30
            ->setDescription('Installs ' . ucfirst($this->library));
51 30
    }
52
53
    /**
54
     * Adds the specified library in the autoload.php.
55
     * 
56
     * @param  string $library
57
     * @return void
58
     */
59 24
    protected function addLibrary($library)
60
    {
61 24
        $autoload = file_get_contents(APPPATH . 'config/autoload.php');
62 24
        $lines = explode(PHP_EOL, $autoload);
63
64 24
        $pattern = '/\$autoload\[\'libraries\'\] = array\((.*?)\)/';
65
66 24
        preg_match_all($pattern, $lines[60], $match);
67
68 24
        $libraries = explode(', ', end($match[1]));
69
70 24
        if ( ! in_array('\'' . $library . '\'', $libraries)) {
71 24
            array_push($libraries, '\'' . $library . '\'');
72
73 24
            $libraries = array_filter($libraries);
74
75 24
            $pattern = '/\$autoload\[\'libraries\'\] = array\([^)]*\);/';
76 24
            $replacement = '$autoload[\'libraries\'] = array(' . implode(', ', $libraries) . ');';
77
78 24
            $lines[60] = preg_replace($pattern, $replacement, $lines[60]);
79
80 24
            file_put_contents(APPPATH . 'config/autoload.php', implode(PHP_EOL, $lines));
81 24
        }
82 24
    }
83
}
84