1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: App::uses('AppShell', 'Console/Command');
17:
18: 19: 20: 21: 22:
23: class CommandTask extends AppShell {
24:
25: 26: 27: 28: 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: 55: 56: 57: 58: 59: 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: 69: 70: 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: 92: 93: 94: 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: 129: 130: 131: 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: 159: 160: 161: 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: }
184: