| 12345 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
4 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
5 |
*
|
|
|
6 |
* Licensed under The MIT License
|
|
|
7 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
8 |
* Redistributions of files must retain the above copyright notice.
|
|
|
9 |
*
|
|
|
10 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
11 |
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
12 |
* @since CakePHP(tm) v 2.5
|
|
|
13 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
App::uses('AppShell', 'Console/Command');
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Base class for Shell Command reflection.
|
|
|
20 |
*
|
|
|
21 |
* @package Cake.Console.Command.Task
|
|
|
22 |
*/
|
|
|
23 |
class CommandTask extends AppShell {
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Gets the shell command listing.
|
|
|
27 |
*
|
|
|
28 |
* @return array
|
|
|
29 |
*/
|
|
|
30 |
public function getShellList() {
|
|
|
31 |
$skipFiles = array('AppShell');
|
|
|
32 |
|
|
|
33 |
$plugins = CakePlugin::loaded();
|
|
|
34 |
$shellList = array_fill_keys($plugins, null) + array('CORE' => null, 'app' => null);
|
|
|
35 |
|
|
|
36 |
$corePath = App::core('Console/Command');
|
|
|
37 |
$shells = App::objects('file', $corePath[0]);
|
|
|
38 |
$shells = array_diff($shells, $skipFiles);
|
|
|
39 |
$this->_appendShells('CORE', $shells, $shellList);
|
|
|
40 |
|
|
|
41 |
$appShells = App::objects('Console/Command', null, false);
|
|
|
42 |
$appShells = array_diff($appShells, $shells, $skipFiles);
|
|
|
43 |
$this->_appendShells('app', $appShells, $shellList);
|
|
|
44 |
|
|
|
45 |
foreach ($plugins as $plugin) {
|
|
|
46 |
$pluginShells = App::objects($plugin . '.Console/Command');
|
|
|
47 |
$this->_appendShells($plugin, $pluginShells, $shellList);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
return array_filter($shellList);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Scan the provided paths for shells, and append them into $shellList
|
|
|
55 |
*
|
|
|
56 |
* @param string $type The type of object.
|
|
|
57 |
* @param array $shells The shell name.
|
|
|
58 |
* @param array &$shellList List of shells.
|
|
|
59 |
* @return void
|
|
|
60 |
*/
|
|
|
61 |
protected function _appendShells($type, $shells, &$shellList) {
|
|
|
62 |
foreach ($shells as $shell) {
|
|
|
63 |
$shellList[$type][] = Inflector::underscore(str_replace('Shell', '', $shell));
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Return a list of all commands
|
|
|
69 |
*
|
|
|
70 |
* @return array
|
|
|
71 |
*/
|
|
|
72 |
public function commands() {
|
|
|
73 |
$shellList = $this->getShellList();
|
|
|
74 |
|
|
|
75 |
$options = array();
|
|
|
76 |
foreach ($shellList as $type => $commands) {
|
|
|
77 |
$prefix = '';
|
|
|
78 |
if (!in_array(strtolower($type), array('app', 'core'))) {
|
|
|
79 |
$prefix = $type . '.';
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
foreach ($commands as $shell) {
|
|
|
83 |
$options[] = $prefix . $shell;
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
return $options;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Return a list of subcommands for a given command
|
|
|
92 |
*
|
|
|
93 |
* @param string $commandName The command you want subcommands from.
|
|
|
94 |
* @return array
|
|
|
95 |
*/
|
|
|
96 |
public function subCommands($commandName) {
|
|
|
97 |
$Shell = $this->getShell($commandName);
|
|
|
98 |
|
|
|
99 |
if (!$Shell) {
|
|
|
100 |
return array();
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
$taskMap = TaskCollection::normalizeObjectArray((array)$Shell->tasks);
|
|
|
104 |
$return = array_keys($taskMap);
|
|
|
105 |
$return = array_map('Inflector::underscore', $return);
|
|
|
106 |
|
|
|
107 |
$ShellReflection = new ReflectionClass('AppShell');
|
|
|
108 |
$shellMethods = $ShellReflection->getMethods(ReflectionMethod::IS_PUBLIC);
|
|
|
109 |
$shellMethodNames = array('main', 'help');
|
|
|
110 |
foreach ($shellMethods as $method) {
|
|
|
111 |
$shellMethodNames[] = $method->getName();
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
$Reflection = new ReflectionClass($Shell);
|
|
|
115 |
$methods = $Reflection->getMethods(ReflectionMethod::IS_PUBLIC);
|
|
|
116 |
$methodNames = array();
|
|
|
117 |
foreach ($methods as $method) {
|
|
|
118 |
$methodNames[] = $method->getName();
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
$return += array_diff($methodNames, $shellMethodNames);
|
|
|
122 |
sort($return);
|
|
|
123 |
|
|
|
124 |
return $return;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Get Shell instance for the given command
|
|
|
129 |
*
|
|
|
130 |
* @param mixed $commandName The command you want.
|
|
|
131 |
* @return mixed
|
|
|
132 |
*/
|
|
|
133 |
public function getShell($commandName) {
|
|
|
134 |
list($pluginDot, $name) = pluginSplit($commandName, true);
|
|
|
135 |
|
|
|
136 |
if (in_array(strtolower($pluginDot), array('app.', 'core.'))) {
|
|
|
137 |
$commandName = $name;
|
|
|
138 |
$pluginDot = '';
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
if (!in_array($commandName, $this->commands())) {
|
|
|
142 |
return false;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
$name = Inflector::camelize($name);
|
|
|
146 |
$pluginDot = Inflector::camelize($pluginDot);
|
|
|
147 |
$class = $name . 'Shell';
|
|
|
148 |
App::uses($class, $pluginDot . 'Console/Command');
|
|
|
149 |
|
|
|
150 |
$Shell = new $class();
|
|
|
151 |
$Shell->plugin = trim($pluginDot, '.');
|
|
|
152 |
$Shell->initialize();
|
|
|
153 |
|
|
|
154 |
return $Shell;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Get Shell instance for the given command
|
|
|
159 |
*
|
|
|
160 |
* @param mixed $commandName The command to get options for.
|
|
|
161 |
* @return array
|
|
|
162 |
*/
|
|
|
163 |
public function options($commandName) {
|
|
|
164 |
$Shell = $this->getShell($commandName);
|
|
|
165 |
if (!$Shell) {
|
|
|
166 |
$parser = new ConsoleOptionParser();
|
|
|
167 |
} else {
|
|
|
168 |
$parser = $Shell->getOptionParser();
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
$options = array();
|
|
|
172 |
$array = $parser->options();
|
|
|
173 |
foreach ($array as $name => $obj) {
|
|
|
174 |
$options[] = "--$name";
|
|
|
175 |
$short = $obj->short();
|
|
|
176 |
if ($short) {
|
|
|
177 |
$options[] = "-$short";
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
return $options;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
}
|