Completed
Push — master ( 4deee6...b383ba )
by Zach
05:43 queued 03:44
created

Kernel::getApplicationCommands()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak;
4
5
use Yarak\Config\Config;
6
use Yarak\Exceptions\InvalidInput;
7
use Yarak\Helpers\NamespaceResolver;
8
use Symfony\Component\Console\Application;
9
use Symfony\Component\Console\Input\InputInterface;
10
11
class Kernel
12
{
13
    /**
14
     * Application config.
15
     *
16
     * @var array
17
     */
18
    private $configArray;
19
20
    /**
21
     * Array of registered commands.
22
     *
23
     * @var array
24
     */
25
    protected $commands;
26
27
    /**
28
     * Construct.
29
     *
30
     * @param array $configArray
31
     */
32
    public function __construct(array $configArray = [])
33
    {
34
        $this->configArray = $configArray;
35
    }
36
37
    /**
38
     * Handle an incoming console command.
39
     */
40
    public function handle($input = null, $output = null)
41
    {
42
        $application = new Application('Yarak - Phalcon devtools');
43
44
        $this->registerCommands($application);
45
46
        if ($input && $output) {
47
            $this->validateCommand($application, $input);
48
49
            $application->setAutoExit(false);
50
51
            return $application->run($input, $output);
52
        }
53
54
        $application->run();
55
    }
56
57
    /**
58
     * Register all Yarak commands.
59
     *
60
     * @param Application $application
61
     */
62
    protected function registerCommands(Application $application)
63
    {
64
        $this->getApplicationCommands();
65
66
        $this->getUserCommands();
67
68
        foreach ($this->commands as $command) {
69
            $application->add(new $command());
70
        }
71
    }
72
73
    /**
74
     * Get array of all Yarak commands.
75
     */
76
    protected function getApplicationCommands()
77
    {
78
        $directory = new \DirectoryIterator(__DIR__.'/Commands');
79
80
        foreach ($directory as $file) {
81
            if (!$file->isDot()) {
82
                $className = str_replace('.php', '', $file->getFilename());
83
84
                $this->commands[] = 'Yarak\\Commands\\'.$className;
85
            }
86
        }
87
    }
88
89
    /**
90
     * Get array of all user defined commands.
91
     */
92
    protected function getUserCommands()
93
    {
94
        $path = Config::getInstance()->getConsoleDirectory('Kernel.php');
95
96
        if (file_exists($path)) {
97
            $kernelClassName = NamespaceResolver::resolve('console', 'Kernel');
98
99
            $kernel = new $kernelClassName();
100
101
            $this->commands = array_merge($this->commands, $kernel->getCommands());
102
        }
103
    }
104
105
    /**
106
     * Validate the given command.
107
     *
108
     * @param Application    $application
109
     * @param InputInterface $input
110
     *
111
     * @throws InvalidInput
112
     */
113
    protected function validateCommand(Application $application, InputInterface $input)
114
    {
115
        $command = $input->getFirstArgument();
116
117
        if ($application->has($command) === false) {
118
            throw InvalidInput::invalidCommand($command);
119
        }
120
    }
121
122
    /**
123
     * Return the Yarak config array.
124
     *
125
     * @return array
126
     */
127
    public function getConfigArray()
128
    {
129
        return $this->configArray;
130
    }
131
}
132