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
|
|
|
* Array of registered commands. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $commands; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Construct. |
22
|
|
|
* |
23
|
|
|
* @param array|string $userConfig |
24
|
|
|
* @param bool $merge |
25
|
|
|
*/ |
26
|
|
|
public function __construct($userConfig = null, $merge = true) |
27
|
|
|
{ |
28
|
|
|
Config::getInstance()->setConfig($userConfig, $merge); |
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
|
|
|
$this->getApplicationCommands(); |
59
|
|
|
|
60
|
|
|
$this->getUserCommands(); |
61
|
|
|
|
62
|
|
|
foreach ($this->commands as $command) { |
63
|
|
|
$application->add(new $command()); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get array of all Yarak commands. |
69
|
|
|
*/ |
70
|
|
|
protected function getApplicationCommands() |
71
|
|
|
{ |
72
|
|
|
$directory = new \DirectoryIterator(__DIR__.'/Commands'); |
73
|
|
|
|
74
|
|
|
foreach ($directory as $file) { |
75
|
|
|
if (!$file->isDot()) { |
76
|
|
|
$className = str_replace('.php', '', $file->getFilename()); |
77
|
|
|
|
78
|
|
|
$this->commands[] = 'Yarak\\Commands\\'.$className; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get array of all user defined commands. |
85
|
|
|
*/ |
86
|
|
|
protected function getUserCommands() |
87
|
|
|
{ |
88
|
|
|
$path = Config::getInstance()->getConsoleDirectory('Kernel.php'); |
89
|
|
|
|
90
|
|
|
if (file_exists($path)) { |
91
|
|
|
$kernelClassName = NamespaceResolver::resolve('console', 'Kernel'); |
92
|
|
|
|
93
|
|
|
$kernel = new $kernelClassName(); |
94
|
|
|
|
95
|
|
|
$this->commands = array_merge($this->commands, $kernel->getCommands()); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Validate the given command. |
101
|
|
|
* |
102
|
|
|
* @param Application $application |
103
|
|
|
* @param InputInterface $input |
104
|
|
|
* |
105
|
|
|
* @throws InvalidInput |
106
|
|
|
*/ |
107
|
|
|
protected function validateCommand(Application $application, InputInterface $input) |
108
|
|
|
{ |
109
|
|
|
$command = $input->getFirstArgument(); |
110
|
|
|
|
111
|
|
|
if ($application->has($command) === false) { |
112
|
|
|
throw InvalidInput::invalidCommand($command); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|