Completed
Push — dev ( b3da26...a54b70 )
by Zach
03:06
created

Kernel::getDI()   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\Commands\Migrate;
6
use Yarak\Commands\DBGenerate;
7
use Phalcon\Di\FactoryDefault;
8
use Yarak\Commands\MakeMigration;
9
use Yarak\Exceptions\InvalidInput;
10
use Symfony\Component\Console\Application;
11
use Symfony\Component\Console\Input\InputInterface;
12
13
class Kernel
14
{
15
    /**
16
     * Application config.
17
     *
18
     * @var array
19
     */
20
    private $config;
21
22
    /**
23
     * Construct.
24
     *
25
     * @param array $config
26
     */
27
    public function __construct(array $config = [])
28
    {
29
        $this->config = $config;
30
    }
31
32
    /**
33
     * Handle an incoming console command.
34
     */
35
    public function handle($input = null, $output = null)
36
    {
37
        $application = new Application('Yarak - Phalcon devtools');
38
39
        $this->registerCommands($application);
40
41
        if ($input && $output) {
42
            $this->validateCommand($application, $input);
43
44
            $application->setAutoExit(false);
45
46
            return $application->run($input, $output);
47
        }
48
49
        $application->run();
50
    }
51
52
    /**
53
     * Register all Yarak commands.
54
     *
55
     * @param Application $application
56
     */
57
    protected function registerCommands(Application $application)
58
    {
59
        $application->add(new DBGenerate($this->config));
60
        $application->add(new MakeMigration($this->config));
61
        $application->add(new Migrate($this->config));
62
    }
63
64
    /**
65
     * Validate the given command.
66
     *
67
     * @param Application    $application
68
     * @param InputInterface $input
69
     *
70
     * @throws InvalidInput
71
     */
72
    protected function validateCommand(Application $application, InputInterface $input)
73
    {
74
        $command = $input->getFirstArgument();
75
76
        if ($application->has($command) === false) {
77
            throw InvalidInput::invalidCommand($command);
78
        }
79
    }
80
    
81
    /**
82
     * Return the Yarak config array.
83
     *
84
     * @return array
85
     */
86
    public function getConfig()
87
    {
88
        return $this->config;
89
    }
90
}
91