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