Completed
Push — dev ( 485358...122982 )
by Zach
02:10
created

Kernel::getConfigArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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 Symfony\Component\Console\Application;
8
use Symfony\Component\Console\Input\InputInterface;
9
10
class Kernel
11
{
12
    /**
13
     * Application config.
14
     *
15
     * @var array
16
     */
17
    private $configArray;
18
19
    /**
20
     * Construct.
21
     *
22
     * @param array $configArray
23
     */
24
    public function __construct(array $configArray = [])
25
    {
26
        $this->configArray = $configArray;
27
    }
28
29
    /**
30
     * Handle an incoming console command.
31
     */
32
    public function handle($input = null, $output = null)
33
    {
34
        $application = new Application('Yarak - Phalcon devtools');
35
36
        $this->registerCommands($application);
37
38
        if ($input && $output) {
39
            $this->validateCommand($application, $input);
40
41
            $application->setAutoExit(false);
42
43
            return $application->run($input, $output);
44
        }
45
46
        $application->run();
47
    }
48
49
    /**
50
     * Register all Yarak commands.
51
     *
52
     * @param Application $application
53
     */
54
    protected function registerCommands(Application $application)
55
    {
56
        $applicationCommands = $this->getApplicationCommands();
57
58
        foreach ($applicationCommands as $command) {
59
            $application->add(new $command());
60
        }
61
    }
62
63
    /**
64
     * Get array of all Yarak commands.
65
     *
66
     * @return array
67
     */
68
    protected function getApplicationCommands()
69
    {
70
        $dir = new \DirectoryIterator(__DIR__.'/Commands');
71
72
        $commands = [];
73
74
        foreach ($dir as $fileinfo) {
75
            if (!$fileinfo->isDot()) {
76
                $className = str_replace('.php', '', $fileinfo->getFilename());
77
78
                $commands[] = 'Yarak\\Commands\\'.$className;
79
            }
80
        }
81
82
        return $commands;
83
    }
84
85
    /**
86
     * Validate the given command.
87
     *
88
     * @param Application    $application
89
     * @param InputInterface $input
90
     *
91
     * @throws InvalidInput
92
     */
93
    protected function validateCommand(Application $application, InputInterface $input)
94
    {
95
        $command = $input->getFirstArgument();
96
97
        if ($application->has($command) === false) {
98
            throw InvalidInput::invalidCommand($command);
99
        }
100
    }
101
102
    /**
103
     * Return the Yarak config array.
104
     *
105
     * @return array
106
     */
107
    public function getConfigArray()
108
    {
109
        return $this->configArray;
110
    }
111
}
112