CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 1.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.3
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 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

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BakeTask
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeRoute
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbConfigTask
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • FileLog
  • FixtureTask
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JqueryEngineHelper
  • JsBaseEngineHelper
  • JsHelper
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • MootoolsEngineHelper
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginShortRoute
  • PluginTask
  • ProjectTask
  • PrototypeEngineHelper
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • Set
  • Shell
  • String
  • TemplateTask
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  1: <?php
  2: /**
  3:  * Base class for Shells
  4:  *
  5:  * PHP versions 4 and 5
  6:  *
  7:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * Redistributions of files must retain the above copyright notice.
 12:  *
 13:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14:  * @link          http://cakephp.org CakePHP(tm) Project
 15:  * @package       cake
 16:  * @subpackage    cake.cake.console.libs
 17:  * @since         CakePHP(tm) v 1.2.0.5012
 18:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 19:  */
 20: 
 21: /**
 22:  * Base class for command-line utilities for automating programmer chores.
 23:  *
 24:  * @package       cake
 25:  * @subpackage    cake.cake.console.libs
 26:  */
 27: class Shell extends Object {
 28: 
 29: /**
 30:  * An instance of the ShellDispatcher object that loaded this script
 31:  *
 32:  * @var ShellDispatcher
 33:  * @access public
 34:  */
 35:     var $Dispatch = null;
 36: 
 37: /**
 38:  * If true, the script will ask for permission to perform actions.
 39:  *
 40:  * @var boolean
 41:  * @access public
 42:  */
 43:     var $interactive = true;
 44: 
 45: /**
 46:  * Holds the DATABASE_CONFIG object for the app. Null if database.php could not be found,
 47:  * or the app does not exist.
 48:  *
 49:  * @var DATABASE_CONFIG
 50:  * @access public
 51:  */
 52:     var $DbConfig = null;
 53: 
 54: /**
 55:  * Contains command switches parsed from the command line.
 56:  *
 57:  * @var array
 58:  * @access public
 59:  */
 60:     var $params = array();
 61: 
 62: /**
 63:  * Contains arguments parsed from the command line.
 64:  *
 65:  * @var array
 66:  * @access public
 67:  */
 68:     var $args = array();
 69: 
 70: /**
 71:  * The file name of the shell that was invoked.
 72:  *
 73:  * @var string
 74:  * @access public
 75:  */
 76:     var $shell = null;
 77: 
 78: /**
 79:  * The class name of the shell that was invoked.
 80:  *
 81:  * @var string
 82:  * @access public
 83:  */
 84:     var $className = null;
 85: 
 86: /**
 87:  * The command called if public methods are available.
 88:  *
 89:  * @var string
 90:  * @access public
 91:  */
 92:     var $command = null;
 93: 
 94: /**
 95:  * The name of the shell in camelized.
 96:  *
 97:  * @var string
 98:  * @access public
 99:  */
100:     var $name = null;
101: 
102: /**
103:  * An alias for the shell
104:  *
105:  * @var string
106:  * @access public
107:  */
108:     var $alias = null;
109: 
110: /**
111:  * Contains tasks to load and instantiate
112:  *
113:  * @var array
114:  * @access public
115:  */
116:     var $tasks = array();
117: 
118: /**
119:  * Contains the loaded tasks
120:  *
121:  * @var array
122:  * @access public
123:  */
124:     var $taskNames = array();
125: 
126: /**
127:  * Contains models to load and instantiate
128:  *
129:  * @var array
130:  * @access public
131:  */
132:     var $uses = array();
133: 
134: /**
135:  *  Constructs this Shell instance.
136:  *
137:  */
138:     function __construct(&$dispatch) {
139:         $vars = array('params', 'args', 'shell', 'shellCommand' => 'command');
140: 
141:         foreach ($vars as $key => $var) {
142:             if (is_string($key)) {
143:                 $this->{$var} =& $dispatch->{$key};
144:             } else {
145:                 $this->{$var} =& $dispatch->{$var};
146:             }
147:         }
148: 
149:         if ($this->name == null) {
150:             $this->name = get_class($this);
151:         }
152: 
153:         if ($this->alias == null) {
154:             $this->alias = $this->name;
155:         }
156: 
157:         ClassRegistry::addObject($this->name, $this);
158:         ClassRegistry::map($this->name, $this->alias);
159: 
160:         if (!PHP5 && isset($this->args[0])) {
161:             if (strpos($this->name, strtolower(Inflector::camelize($this->args[0]))) !== false) {
162:                 $dispatch->shiftArgs();
163:             }
164:             if (strtolower($this->command) == strtolower(Inflector::variable($this->args[0])) && method_exists($this, $this->command)) {
165:                 $dispatch->shiftArgs();
166:             }
167:         }
168: 
169:         $this->Dispatch =& $dispatch;
170:     }
171: 
172: /**
173:  * Initializes the Shell
174:  * acts as constructor for subclasses
175:  * allows configuration of tasks prior to shell execution
176:  *
177:  * @access public
178:  */
179:     function initialize() {
180:         $this->_loadModels();
181:     }
182: 
183: /**
184:  * Starts up the Shell
185:  * allows for checking and configuring prior to command or main execution
186:  * can be overriden in subclasses
187:  *
188:  * @access public
189:  */
190:     function startup() {
191:         $this->_welcome();
192:     }
193: 
194: /**
195:  * Displays a header for the shell
196:  *
197:  * @access protected
198:  */
199:     function _welcome() {
200:         $this->Dispatch->clear();
201:         $this->out();
202:         $this->out('Welcome to CakePHP v' . Configure::version() . ' Console');
203:         $this->hr();
204:         $this->out('App : '. $this->params['app']);
205:         $this->out('Path: '. $this->params['working']);
206:         $this->hr();
207:     }
208: 
209: /**
210:  * Loads database file and constructs DATABASE_CONFIG class
211:  * makes $this->DbConfig available to subclasses
212:  *
213:  * @return bool
214:  * @access protected
215:  */
216:     function _loadDbConfig() {
217:         if (config('database') && class_exists('DATABASE_CONFIG')) {
218:             $this->DbConfig =& new DATABASE_CONFIG();
219:             return true;
220:         }
221:         $this->err('Database config could not be loaded.');
222:         $this->out('Run `bake` to create the database configuration.');
223:         return false;
224:     }
225: 
226: /**
227:  * if var $uses = true
228:  * Loads AppModel file and constructs AppModel class
229:  * makes $this->AppModel available to subclasses
230:  * if var $uses is an array of models will load those models
231:  *
232:  * @return bool
233:  * @access protected
234:  */
235:     function _loadModels() {
236:         if ($this->uses === null || $this->uses === false) {
237:             return;
238:         }
239: 
240:         if ($this->uses === true && App::import('Model', 'AppModel')) {
241:             $this->AppModel =& new AppModel(false, false, false);
242:             return true;
243:         }
244: 
245:         if ($this->uses !== true && !empty($this->uses)) {
246:             $uses = is_array($this->uses) ? $this->uses : array($this->uses);
247: 
248:             $modelClassName = $uses[0];
249:             if (strpos($uses[0], '.') !== false) {
250:                 list($plugin, $modelClassName) = explode('.', $uses[0]);
251:             }
252:             $this->modelClass = $modelClassName;
253: 
254:             foreach ($uses as $modelClass) {
255:                 list($plugin, $modelClass) = pluginSplit($modelClass, true);
256:                 if (PHP5) {
257:                     $this->{$modelClass} = ClassRegistry::init($plugin . $modelClass);
258:                 } else {
259:                     $this->{$modelClass} =& ClassRegistry::init($plugin . $modelClass);
260:                 }
261:             }
262:             return true;
263:         }
264:         return false;
265:     }
266: 
267: /**
268:  * Loads tasks defined in var $tasks
269:  *
270:  * @return bool
271:  * @access public
272:  */
273:     function loadTasks() {
274:         if ($this->tasks === null || $this->tasks === false || $this->tasks === true || empty($this->tasks)) {
275:             return true;
276:         }
277: 
278:         $tasks = $this->tasks;
279:         if (!is_array($tasks)) {
280:             $tasks = array($tasks);
281:         }
282: 
283:         foreach ($tasks as $taskName) {
284:             $task = Inflector::underscore($taskName);
285:             $taskClass = Inflector::camelize($taskName . 'Task');
286: 
287:             if (!class_exists($taskClass)) {
288:                 foreach ($this->Dispatch->shellPaths as $path) {
289:                     $taskPath = $path . 'tasks' . DS . $task . '.php';
290:                     if (file_exists($taskPath)) {
291:                         require_once $taskPath;
292:                         break;
293:                     }
294:                 }
295:             }
296:             $taskClassCheck = $taskClass;
297:             if (!PHP5) {
298:                 $taskClassCheck = strtolower($taskClass);
299:             }
300:             if (ClassRegistry::isKeySet($taskClassCheck)) {
301:                 $this->taskNames[] = $taskName;
302:                 if (!PHP5) {
303:                     $this->{$taskName} =& ClassRegistry::getObject($taskClassCheck);
304:                 } else {
305:                     $this->{$taskName} = ClassRegistry::getObject($taskClassCheck);
306:                 }
307:             } else {
308:                 $this->taskNames[] = $taskName;
309:                 if (!PHP5) {
310:                     $this->{$taskName} =& new $taskClass($this->Dispatch);
311:                 } else {
312:                     $this->{$taskName} = new $taskClass($this->Dispatch);
313:                 }
314:             }
315: 
316:             if (!isset($this->{$taskName})) {
317:                 $this->err("Task `{$taskName}` could not be loaded");
318:                 $this->_stop();
319:             }
320:         }
321: 
322:         return true;
323:     }
324: 
325: /**
326:  * Prompts the user for input, and returns it.
327:  *
328:  * @param string $prompt Prompt text.
329:  * @param mixed $options Array or string of options.
330:  * @param string $default Default input value.
331:  * @return Either the default value, or the user-provided input.
332:  * @access public
333:  */
334:     function in($prompt, $options = null, $default = null) {
335:         if (!$this->interactive) {
336:             return $default;
337:         }
338:         $in = $this->Dispatch->getInput($prompt, $options, $default);
339: 
340:         if ($options && is_string($options)) {
341:             if (strpos($options, ',')) {
342:                 $options = explode(',', $options);
343:             } elseif (strpos($options, '/')) {
344:                 $options = explode('/', $options);
345:             } else {
346:                 $options = array($options);
347:             }
348:         }
349:         if (is_array($options)) {
350:             while ($in === '' || ($in !== '' && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) {
351:                 $in = $this->Dispatch->getInput($prompt, $options, $default);
352:             }
353:         }
354:         return $in;
355:     }
356: 
357: /**
358:  * Outputs a single or multiple messages to stdout. If no parameters
359:  * are passed outputs just a newline.
360:  *
361:  * @param mixed $message A string or a an array of strings to output
362:  * @param integer $newlines Number of newlines to append
363:  * @return integer Returns the number of bytes returned from writing to stdout.
364:  * @access public
365:  */
366:     function out($message = null, $newlines = 1) {
367:         if (is_array($message)) {
368:             $message = implode($this->nl(), $message);
369:         }
370:         return $this->Dispatch->stdout($message . $this->nl($newlines), false);
371:     }
372: 
373: /**
374:  * Outputs a single or multiple error messages to stderr. If no parameters
375:  * are passed outputs just a newline.
376:  *
377:  * @param mixed $message A string or a an array of strings to output
378:  * @param integer $newlines Number of newlines to append
379:  * @access public
380:  */
381:     function err($message = null, $newlines = 1) {
382:         if (is_array($message)) {
383:             $message = implode($this->nl(), $message);
384:         }
385:         $this->Dispatch->stderr($message . $this->nl($newlines));
386:     }
387: 
388: /**
389:  * Returns a single or multiple linefeeds sequences.
390:  *
391:  * @param integer $multiplier Number of times the linefeed sequence should be repeated
392:  * @access public
393:  * @return string
394:  */
395:     function nl($multiplier = 1) {
396:         return str_repeat("\n", $multiplier);
397:     }
398: 
399: /**
400:  * Outputs a series of minus characters to the standard output, acts as a visual separator.
401:  *
402:  * @param integer $newlines Number of newlines to pre- and append
403:  * @access public
404:  */
405:     function hr($newlines = 0) {
406:         $this->out(null, $newlines);
407:         $this->out('---------------------------------------------------------------');
408:         $this->out(null, $newlines);
409:     }
410: 
411: /**
412:  * Displays a formatted error message
413:  * and exits the application with status code 1
414:  *
415:  * @param string $title Title of the error
416:  * @param string $message An optional error message
417:  * @access public
418:  */
419:     function error($title, $message = null) {
420:         $this->err(sprintf(__('Error: %s', true), $title));
421: 
422:         if (!empty($message)) {
423:             $this->err($message);
424:         }
425:         $this->_stop(1);
426:     }
427: 
428: /**
429:  * Will check the number args matches otherwise throw an error
430:  *
431:  * @param integer $expectedNum Expected number of paramters
432:  * @param string $command Command
433:  * @access protected
434:  */
435:     function _checkArgs($expectedNum, $command = null) {
436:         if (!$command) {
437:             $command = $this->command;
438:         }
439:         if (count($this->args) < $expectedNum) {
440:             $message[] = "Got: " . count($this->args);
441:             $message[] = "Expected: {$expectedNum}";
442:             $message[] = "Please type `cake {$this->shell} help` for help";
443:             $message[] = "on usage of the {$this->name} {$command}.";
444:             $this->error('Wrong number of parameters', $message);
445:         }
446:     }
447: 
448: /**
449:  * Creates a file at given path
450:  *
451:  * @param string $path Where to put the file.
452:  * @param string $contents Content to put in the file.
453:  * @return boolean Success
454:  * @access public
455:  */
456:     function createFile($path, $contents) {
457:         $path = str_replace(DS . DS, DS, $path);
458: 
459:         $this->out();
460:         $this->out(sprintf(__("Creating file %s", true), $path));
461: 
462:         if (is_file($path) && $this->interactive === true) {
463:             $prompt = sprintf(__('File `%s` exists, overwrite?', true), $path);
464:             $key = $this->in($prompt,  array('y', 'n', 'q'), 'n');
465: 
466:             if (strtolower($key) == 'q') {
467:                 $this->out(__('Quitting.', true), 2);
468:                 $this->_stop();
469:             } elseif (strtolower($key) != 'y') {
470:                 $this->out(sprintf(__('Skip `%s`', true), $path), 2);
471:                 return false;
472:             }
473:         }
474:         if (!class_exists('File')) {
475:             require LIBS . 'file.php';
476:         }
477: 
478:         if ($File = new File($path, true)) {
479:             $data = $File->prepare($contents);
480:             $File->write($data);
481:             $this->out(sprintf(__('Wrote `%s`', true), $path));
482:             return true;
483:         } else {
484:             $this->err(sprintf(__('Could not write to `%s`.', true), $path), 2);
485:             return false;
486:         }
487:     }
488: 
489: /**
490:  * Outputs usage text on the standard output. Implement it in subclasses.
491:  *
492:  * @access public
493:  */
494:     function help() {
495:         if ($this->command != null) {
496:             $this->err("Unknown {$this->name} command `{$this->command}`.");
497:             $this->err("For usage, try `cake {$this->shell} help`.", 2);
498:         } else {
499:             $this->Dispatch->help();
500:         }
501:     }
502: 
503: /**
504:  * Action to create a Unit Test
505:  *
506:  * @return boolean Success
507:  * @access protected
508:  */
509:     function _checkUnitTest() {
510:         if (App::import('vendor', 'simpletest' . DS . 'simpletest')) {
511:             return true;
512:         }
513:         $prompt = 'SimpleTest is not installed. Do you want to bake unit test files anyway?';
514:         $unitTest = $this->in($prompt, array('y','n'), 'y');
515:         $result = strtolower($unitTest) == 'y' || strtolower($unitTest) == 'yes';
516: 
517:         if ($result) {
518:             $this->out();
519:             $this->out('You can download SimpleTest from http://simpletest.org');
520:         }
521:         return $result;
522:     }
523: 
524: /**
525:  * Makes absolute file path easier to read
526:  *
527:  * @param string $file Absolute file path
528:  * @return sting short path
529:  * @access public
530:  */
531:     function shortPath($file) {
532:         $shortPath = str_replace(ROOT, null, $file);
533:         $shortPath = str_replace('..' . DS, '', $shortPath);
534:         return str_replace(DS . DS, DS, $shortPath);
535:     }
536: 
537: /**
538:  * Creates the proper controller path for the specified controller class name
539:  *
540:  * @param string $name Controller class name
541:  * @return string Path to controller
542:  * @access protected
543:  */
544:     function _controllerPath($name) {
545:         return strtolower(Inflector::underscore($name));
546:     }
547: 
548: /**
549:  * Creates the proper controller plural name for the specified controller class name
550:  *
551:  * @param string $name Controller class name
552:  * @return string Controller plural name
553:  * @access protected
554:  */
555:     function _controllerName($name) {
556:         return Inflector::pluralize(Inflector::camelize($name));
557:     }
558: 
559: /**
560:  * Creates the proper controller camelized name (singularized) for the specified name
561:  *
562:  * @param string $name Name
563:  * @return string Camelized and singularized controller name
564:  * @access protected
565:  */
566:     function _modelName($name) {
567:         return Inflector::camelize(Inflector::singularize($name));
568:     }
569: 
570: /**
571:  * Creates the proper underscored model key for associations
572:  *
573:  * @param string $name Model class name
574:  * @return string Singular model key
575:  * @access protected
576:  */
577:     function _modelKey($name) {
578:         return Inflector::underscore($name) . '_id';
579:     }
580: 
581: /**
582:  * Creates the proper model name from a foreign key
583:  *
584:  * @param string $key Foreign key
585:  * @return string Model name
586:  * @access protected
587:  */
588:     function _modelNameFromKey($key) {
589:         return Inflector::camelize(str_replace('_id', '', $key));
590:     }
591: 
592: /**
593:  * creates the singular name for use in views.
594:  *
595:  * @param string $name
596:  * @return string $name
597:  * @access protected
598:  */
599:     function _singularName($name) {
600:         return Inflector::variable(Inflector::singularize($name));
601:     }
602: 
603: /**
604:  * Creates the plural name for views
605:  *
606:  * @param string $name Name to use
607:  * @return string Plural name for views
608:  * @access protected
609:  */
610:     function _pluralName($name) {
611:         return Inflector::variable(Inflector::pluralize($name));
612:     }
613: 
614: /**
615:  * Creates the singular human name used in views
616:  *
617:  * @param string $name Controller name
618:  * @return string Singular human name
619:  * @access protected
620:  */
621:     function _singularHumanName($name) {
622:         return Inflector::humanize(Inflector::underscore(Inflector::singularize($name)));
623:     }
624: 
625: /**
626:  * Creates the plural human name used in views
627:  *
628:  * @param string $name Controller name
629:  * @return string Plural human name
630:  * @access protected
631:  */
632:     function _pluralHumanName($name) {
633:         return Inflector::humanize(Inflector::underscore($name));
634:     }
635: 
636: /**
637:  * Find the correct path for a plugin. Scans $pluginPaths for the plugin you want.
638:  *
639:  * @param string $pluginName Name of the plugin you want ie. DebugKit
640:  * @return string $path path to the correct plugin.
641:  */
642:     function _pluginPath($pluginName) {
643:         return App::pluginPath($pluginName);
644:     }
645: }
646: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs