CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • AssetsTask
  • CommandTask
  • ExtractTask
  • LoadTask
  • UnloadTask
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         2.5.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\Shell\Task;
 16: 
 17: use Cake\Console\Shell;
 18: use Cake\Core\App;
 19: use Cake\Core\Plugin;
 20: use Cake\Filesystem\Folder;
 21: use Cake\Utility\Hash;
 22: use Cake\Utility\Inflector;
 23: use ReflectionClass;
 24: use ReflectionMethod;
 25: 
 26: /**
 27:  * Base class for Shell Command reflection.
 28:  */
 29: class CommandTask extends Shell
 30: {
 31:     /**
 32:      * Gets the shell command listing.
 33:      *
 34:      * @return array
 35:      */
 36:     public function getShellList()
 37:     {
 38:         $skipFiles = ['app'];
 39:         $hiddenCommands = ['command_list', 'completion'];
 40:         $plugins = Plugin::loaded();
 41:         $shellList = array_fill_keys($plugins, null) + ['CORE' => null, 'app' => null];
 42: 
 43:         $appPath = App::path('Shell');
 44:         $shellList = $this->_findShells($shellList, $appPath[0], 'app', $skipFiles);
 45: 
 46:         $appPath = App::path('Command');
 47:         $shellList = $this->_findShells($shellList, $appPath[0], 'app', $skipFiles);
 48: 
 49:         $skipCore = array_merge($skipFiles, $hiddenCommands, $shellList['app']);
 50:         $corePath = dirname(__DIR__);
 51:         $shellList = $this->_findShells($shellList, $corePath, 'CORE', $skipCore);
 52: 
 53:         $corePath = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'Command';
 54:         $shellList = $this->_findShells($shellList, $corePath, 'CORE', $skipCore);
 55: 
 56:         foreach ($plugins as $plugin) {
 57:             $pluginPath = Plugin::classPath($plugin) . 'Shell';
 58:             $shellList = $this->_findShells($shellList, $pluginPath, $plugin, []);
 59:         }
 60: 
 61:         return array_filter($shellList);
 62:     }
 63: 
 64:     /**
 65:      * Find shells in $path and add them to $shellList
 66:      *
 67:      * @param array $shellList The shell listing array.
 68:      * @param string $path The path to look in.
 69:      * @param string $key The key to add shells to
 70:      * @param string[] $skip A list of commands to exclude.
 71:      * @return array The updated list of shells.
 72:      */
 73:     protected function _findShells($shellList, $path, $key, $skip)
 74:     {
 75:         $shells = $this->_scanDir($path);
 76: 
 77:         return $this->_appendShells($key, $shells, $shellList, $skip);
 78:     }
 79: 
 80:     /**
 81:      * Scan the provided paths for shells, and append them into $shellList
 82:      *
 83:      * @param string $type The type of object.
 84:      * @param array $shells The shell name.
 85:      * @param array $shellList List of shells.
 86:      * @param array $skip List of command names to skip.
 87:      * @return array The updated $shellList
 88:      */
 89:     protected function _appendShells($type, $shells, $shellList, $skip)
 90:     {
 91:         if (!isset($shellList[$type])) {
 92:             $shellList[$type] = [];
 93:         }
 94: 
 95:         foreach ($shells as $shell) {
 96:             $name = Inflector::underscore(preg_replace('/(Shell|Command)$/', '', $shell));
 97:             if (!in_array($name, $skip, true)) {
 98:                 $shellList[$type][] = $name;
 99:             }
100:         }
101:         sort($shellList[$type]);
102: 
103:         return $shellList;
104:     }
105: 
106:     /**
107:      * Scan a directory for .php files and return the class names that
108:      * should be within them.
109:      *
110:      * @param string $dir The directory to read.
111:      * @return array The list of shell classnames based on conventions.
112:      */
113:     protected function _scanDir($dir)
114:     {
115:         $dir = new Folder($dir);
116:         $contents = $dir->read(true, true);
117:         if (empty($contents[1])) {
118:             return [];
119:         }
120:         $shells = [];
121:         foreach ($contents[1] as $file) {
122:             if (substr($file, -4) !== '.php') {
123:                 continue;
124:             }
125:             $shells[] = substr($file, 0, -4);
126:         }
127: 
128:         return $shells;
129:     }
130: 
131:     /**
132:      * Return a list of all commands
133:      *
134:      * @return array
135:      */
136:     public function commands()
137:     {
138:         $shellList = $this->getShellList();
139:         $flatten = Hash::flatten($shellList);
140:         $duplicates = array_intersect($flatten, array_unique(array_diff_key($flatten, array_unique($flatten))));
141:         $duplicates = Hash::expand($duplicates);
142: 
143:         $options = [];
144:         foreach ($shellList as $type => $commands) {
145:             foreach ($commands as $shell) {
146:                 $prefix = '';
147:                 if (!in_array(strtolower($type), ['app', 'core']) &&
148:                     isset($duplicates[$type]) &&
149:                     in_array($shell, $duplicates[$type])
150:                 ) {
151:                     $prefix = $type . '.';
152:                 }
153: 
154:                 $options[] = $prefix . $shell;
155:             }
156:         }
157: 
158:         return $options;
159:     }
160: 
161:     /**
162:      * Return a list of subcommands for a given command
163:      *
164:      * @param string $commandName The command you want subcommands from.
165:      * @return string[]
166:      * @throws \ReflectionException
167:      */
168:     public function subCommands($commandName)
169:     {
170:         $Shell = $this->getShell($commandName);
171: 
172:         if (!$Shell) {
173:             return [];
174:         }
175: 
176:         $taskMap = $this->Tasks->normalizeArray((array)$Shell->tasks);
177:         $return = array_keys($taskMap);
178:         $return = array_map('Cake\Utility\Inflector::underscore', $return);
179: 
180:         $shellMethodNames = ['main', 'help', 'getOptionParser', 'initialize', 'runCommand'];
181: 
182:         $baseClasses = ['Object', 'Shell', 'AppShell'];
183: 
184:         $Reflection = new ReflectionClass($Shell);
185:         $methods = $Reflection->getMethods(ReflectionMethod::IS_PUBLIC);
186:         $methodNames = [];
187:         foreach ($methods as $method) {
188:             $declaringClass = $method->getDeclaringClass()->getShortName();
189:             if (!in_array($declaringClass, $baseClasses)) {
190:                 $methodNames[] = $method->getName();
191:             }
192:         }
193: 
194:         $return = array_merge($return, array_diff($methodNames, $shellMethodNames));
195:         sort($return);
196: 
197:         return $return;
198:     }
199: 
200:     /**
201:      * Get Shell instance for the given command
202:      *
203:      * @param string $commandName The command you want.
204:      * @return \Cake\Console\Shell|bool Shell instance if the command can be found, false otherwise.
205:      */
206:     public function getShell($commandName)
207:     {
208:         list($pluginDot, $name) = pluginSplit($commandName, true);
209: 
210:         if (in_array(strtolower($pluginDot), ['app.', 'core.'])) {
211:             $commandName = $name;
212:             $pluginDot = '';
213:         }
214: 
215:         if (!in_array($commandName, $this->commands()) && (empty($pluginDot) && !in_array($name, $this->commands()))) {
216:             return false;
217:         }
218: 
219:         if (empty($pluginDot)) {
220:             $shellList = $this->getShellList();
221: 
222:             if (!in_array($commandName, $shellList['app']) && !in_array($commandName, $shellList['CORE'])) {
223:                 unset($shellList['CORE'], $shellList['app']);
224:                 foreach ($shellList as $plugin => $commands) {
225:                     if (in_array($commandName, $commands)) {
226:                         $pluginDot = $plugin . '.';
227:                         break;
228:                     }
229:                 }
230:             }
231:         }
232: 
233:         $name = Inflector::camelize($name);
234:         $pluginDot = Inflector::camelize($pluginDot);
235:         $class = App::className($pluginDot . $name, 'Shell', 'Shell');
236:         if (!$class) {
237:             return false;
238:         }
239: 
240:         /* @var \Cake\Console\Shell $Shell */
241:         $Shell = new $class();
242:         $Shell->plugin = trim($pluginDot, '.');
243:         $Shell->initialize();
244: 
245:         return $Shell;
246:     }
247: 
248:     /**
249:      * Get options list for the given command or subcommand
250:      *
251:      * @param string $commandName The command to get options for.
252:      * @param string $subCommandName The subcommand to get options for. Can be empty to get options for the command.
253:      * If this parameter is used, the subcommand must be a valid subcommand of the command passed
254:      * @return array Options list for the given command or subcommand
255:      */
256:     public function options($commandName, $subCommandName = '')
257:     {
258:         $Shell = $this->getShell($commandName);
259: 
260:         if (!$Shell) {
261:             return [];
262:         }
263: 
264:         $parser = $Shell->getOptionParser();
265: 
266:         if (!empty($subCommandName)) {
267:             $subCommandName = Inflector::camelize($subCommandName);
268:             if ($Shell->hasTask($subCommandName)) {
269:                 $parser = $Shell->{$subCommandName}->getOptionParser();
270:             } else {
271:                 return [];
272:             }
273:         }
274: 
275:         $options = [];
276:         $array = $parser->options();
277:         /* @var \Cake\Console\ConsoleInputOption $obj */
278:         foreach ($array as $name => $obj) {
279:             $options[] = "--$name";
280:             $short = $obj->short();
281:             if ($short) {
282:                 $options[] = "-$short";
283:             }
284:         }
285: 
286:         return $options;
287:     }
288: }
289: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs