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.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.2
      • 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
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

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