Passed
Push — master ( 53bb62...283d0c )
by Rougin
08:28
created

InstallDoctrineCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 9.38%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 64
ccs 3
cts 32
cp 0.0938
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 50 4
1
<?php
2
3
namespace Rougin\Combustor\Commands;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
use Rougin\Combustor\Common\File;
9
use Rougin\Combustor\Common\Tools;
10
use Rougin\Combustor\Common\Commands\InstallCommand;
11
12
/**
13
 * Install Doctrine Command
14
 *
15
 * Installs Doctrine for CodeIgniter
16
 *
17
 * @package Combustor
18
 * @author  Rougin Gutib <[email protected]>
19
 */
20
class InstallDoctrineCommand extends InstallCommand
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $library = 'doctrine';
26
27
    /**
28
     * Executes the command.
29
     *
30
     * @param \Symfony\Component\Console\Input\InputInterface   $input
31
     * @param \Symfony\Component\Console\Output\OutputInterface $output
32
     * @return object|\Symfony\Component\Console\Output\OutputInterface
33
     */
34 12
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36 12
        system('composer require doctrine/orm');
37
38 12
        $cli = $this->renderer->render('DoctrineCLI.tpl');
39
        $doctrine = new File(APPPATH . 'libraries/Doctrine.php');
0 ignored issues
show
Bug introduced by
The constant Rougin\Combustor\Commands\APPPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
40
        $template = $this->renderer->render('Libraries/Doctrine.tpl');
41
        $vendor = realpath('vendor');
42
43
        // Modifies the contents of vendor/bin/doctrine.php, creates the
44
        // Doctrine library and creates a "proxies" directory for lazy loading.
45
        file_put_contents($vendor . '/bin/doctrine.php', $cli);
46
        file_put_contents($vendor . '/doctrine/orm/bin/doctrine.php', $cli);
47
48
        $doctrine->putContents($template);
49
        $doctrine->close();
50
51
        $this->addLibrary('doctrine');
52
53
        if (! is_dir(APPPATH . 'models/proxies')) {
54
            mkdir(APPPATH . 'models/proxies');
55
            chmod(APPPATH . 'models/proxies', 0777);
56
        }
57
58
        $abstractCommandPath = $vendor . '/doctrine/orm/lib/Doctrine/ORM/' .
59
            'Tools/Console/Command/SchemaTool/AbstractCommand.php';
60
61
        // Includes the Base Model class in Doctrine CLI
62
        $abstractCommand = file_get_contents($abstractCommandPath);
63
64
        $search = 'use Doctrine\ORM\Tools\SchemaTool;';
65
        $replace = $search . "\n" . 'include BASEPATH . \'core/Model.php\';';
66
67
        $contents = $abstractCommand;
68
        $schemaTool = 'use Doctrine\ORM\Tools\SchemaTool;';
69
        $coreModel = 'include BASEPATH . \'core/Model.php\';';
70
71
        if (strpos($abstractCommand, $schemaTool) !== false) {
72
            if (strpos($abstractCommand, $coreModel) === false) {
73
                $contents = str_replace($search, $replace, $abstractCommand);
74
            }
75
        }
76
77
        file_put_contents($abstractCommandPath, $contents);
78
79
        Tools::ignite();
80
81
        $message = 'Doctrine ORM is now installed successfully!';
82
83
        return $output->writeln('<info>' . $message . '</info>');
84
    }
85
}
86