forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extended.php
245 lines (207 loc) · 8.57 KB
/
Extended.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Sebastian Arrubia <sarrubia@gmail.com> |
+------------------------------------------------------------------------+
*/
namespace Phalcon\Cli\Console;
use Phalcon\Cli\Console as ConsoleApp;
use Phalcon\Annotations\Adapter\Memory as MemoryAdapter;
/**
* Phalcon\CLI\Console\Extended
*
* Extended Console Application that uses annotations in order to create automatically a help description.
*
* @package Phalcon\Cli\Console
*/
class Extended extends ConsoleApp
{
private $tasksDir = '';
private $documentation = [];
/**
* Handle the whole command-line tasks
*
* @param array $arguments Cli arguments
*
* @return mixed
* @throws \Phalcon\Cli\Console\Exception
*/
public function handle(array $arguments = null)
{
if (isset($arguments['task']) && in_array($arguments['task'], ['-h', '--help', 'help'])) {
$this->setTasksDir();
$this->createHelp();
$this->showHelp();
return;
} elseif (isset($arguments['action']) && in_array($arguments['action'], ['-h', '--help', 'help'])) {
$this->setTasksDir();
$this->createHelp();
$this->showTaskHelp($arguments['task']);
return;
}
parent::handle($arguments);
}
/**
* @throws \Phalcon\Cli\Console\Exception
*/
private function setTasksDir()
{
$config = $this->getDI()->get('config');
if (!isset($config['tasksDir']) || !is_dir($config['tasksDir'])) {
throw new Exception("Invalid provided tasks Dir");
}
$this->tasksDir = $config['tasksDir'];
}
private function createHelp()
{
$scannedTasksDir = array_diff(scandir($this->tasksDir), ['..', '.']);
$config = $this->getDI()->get('config');
$dispatcher = $this->getDI()->getShared('dispatcher');
$namespace = $dispatcher->getNamespaceName();
if (isset($config['annotationsAdapter']) && $config['annotationsAdapter']) {
$adapter = '\Phalcon\Annotations\Adapter\\' . $config['annotationsAdapter'];
if (class_exists($adapter)) {
$reader = new $adapter();
} else {
$reader = new MemoryAdapter();
}
} else {
$reader = new MemoryAdapter();
}
foreach ($scannedTasksDir as $taskFile) {
$taskFileInfo = pathinfo($taskFile);
$taskClass = ($namespace ? $namespace . '\\' : '') . $taskFileInfo["filename"];
$taskName = strtolower(str_replace('Task', '', $taskFileInfo["filename"]));
$this->documentation[$taskName] = ['description' => [''], 'actions' => []];
$reflector = $reader->get($taskClass);
$annotations = $reflector->getClassAnnotations();
if (!$annotations) {
continue;
}
// Class Annotations
foreach ($annotations as $annotation) {
if ($annotation->getName() == 'description') {
$this->documentation[$taskName]['description'] = $annotation->getArguments();
}
}
$methodAnnotations = $reflector->getMethodsAnnotations();
// Method Annotations
if (!$methodAnnotations) {
continue;
}
foreach ($methodAnnotations as $action => $collection) {
if ($collection->has('DoNotCover')) {
continue;
}
$actionName = strtolower(str_replace('Action', '', $action));
$this->documentation[$taskName]['actions'][$actionName] = [];
$actionAnnotations = $collection->getAnnotations();
foreach ($actionAnnotations as $actAnnotation) {
$_anotation = $actAnnotation->getName();
if ($_anotation == 'description') {
$getDesc = $actAnnotation->getArguments();
$this->documentation[$taskName]['actions'][$actionName]['description'] = $getDesc;
} elseif ($_anotation == 'param') {
$getParams = $actAnnotation->getArguments();
$this->documentation[$taskName]['actions'][$actionName]['params'][] = $getParams;
}
}
}
}
}
private function showHelp()
{
$config = $this->getDI()->get('config');
$helpOutput = PHP_EOL;
if (isset($config['appName'])) {
$helpOutput .= $config['appName'] . ' ';
}
if (isset($config['version'])) {
$helpOutput .= $config['version'];
}
echo $helpOutput . PHP_EOL;
echo PHP_EOL . 'Usage:' . PHP_EOL;
echo PHP_EOL;
echo "\t" , 'command [<task> [<action> [<param1> <param2> ... <paramN>] ] ]', PHP_EOL;
echo PHP_EOL;
echo PHP_EOL . 'To show task help type:' . PHP_EOL;
echo PHP_EOL;
echo ' command <task> -h | --help | help'. PHP_EOL;
echo PHP_EOL;
echo 'Available tasks '.PHP_EOL;
foreach ($this->documentation as $task => $doc) {
echo PHP_EOL;
echo ' '. $task . PHP_EOL ;
foreach ($doc['description'] as $line) {
echo ' '.$line . PHP_EOL;
}
}
}
private function showTaskHelp($taskTogetHelp)
{
$config = $this->getDI()->get('config');
$helpOutput = PHP_EOL;
if (isset($config['appName'])) {
$helpOutput .= $config['appName'] . ' ';
}
if (isset($config['version'])) {
$helpOutput .= $config['version'];
}
echo $helpOutput . PHP_EOL;
echo PHP_EOL . 'Usage:' . PHP_EOL;
echo PHP_EOL;
echo "\t" , 'command [<task> [<action> [<param1> <param2> ... <paramN>] ] ]', PHP_EOL;
echo PHP_EOL;
foreach ($this->documentation as $task => $doc) {
if ($taskTogetHelp != $task) {
continue;
}
echo PHP_EOL;
echo "Task: " . $task . PHP_EOL . PHP_EOL ;
foreach ($doc['description'] as $line) {
echo ' '.$line . PHP_EOL;
}
echo PHP_EOL;
echo 'Available actions:'.PHP_EOL.PHP_EOL;
foreach ($doc['actions'] as $actionName => $aDoc) {
echo ' '.$actionName . PHP_EOL;
if (isset($aDoc['description'])) {
echo ' '.implode(PHP_EOL, $aDoc['description']) . PHP_EOL;
}
echo PHP_EOL;
if (isset($aDoc['params']) && is_array($aDoc['params'])) {
echo ' Parameters:'.PHP_EOL;
foreach ($aDoc['params'] as $param) {
if (is_array($param)) {
$_to_print = '';
if (isset($param[0]['name'])) {
$_to_print = $param[0]['name'];
}
if (isset($param[0]['type'])) {
$_to_print .= ' ( '.$param[0]['type'].' )';
}
if (isset($param[0]['description'])) {
$_to_print .= ' '.$param[0]['description'].PHP_EOL;
}
if (!empty($_to_print)) {
echo ' '.$_to_print;
}
}
}
}
}
break;
}
}
}