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 2.1 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 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

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • AclShell
  • ApiShell
  • BakeShell
  • CommandListShell
  • ConsoleShell
  • I18nShell
  • SchemaShell
  • TestShell
  • TestsuiteShell
  • UpgradeShell
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * Redistributions of files must retain the above copyright notice.
  8:  *
  9:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 10:  * @link          http://cakephp.org CakePHP(tm) Project
 11:  * @since         CakePHP(tm) v 1.2.0.5012
 12:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 13:  */
 14: 
 15: App::uses('AppShell', 'Console/Command');
 16: 
 17: /**
 18:  * Provides a very basic 'interactive' console for CakePHP apps.
 19:  *
 20:  * @package       Cake.Console.Command
 21:  */
 22: class ConsoleShell extends AppShell {
 23: 
 24: /**
 25:  * Available binding types
 26:  *
 27:  * @var array
 28:  */
 29:     public $associations = array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany');
 30: 
 31: /**
 32:  * Chars that describe invalid commands
 33:  *
 34:  * @var array
 35:  */
 36:     public $badCommandChars = array('$', ';');
 37: 
 38: /**
 39:  * Available models
 40:  *
 41:  * @var array
 42:  */
 43:     public $models = array();
 44: 
 45: /**
 46:  * Override startup of the Shell
 47:  *
 48:  * @return void
 49:  */
 50:     public function startup() {
 51:         App::uses('Dispatcher', 'Routing');
 52:         $this->Dispatcher = new Dispatcher();
 53:         $this->models = App::objects('Model');
 54: 
 55:         foreach ($this->models as $model) {
 56:             $class = $model;
 57:             $this->models[$model] = $class;
 58:             App::uses($class, 'Model');
 59:             $this->{$class} = new $class();
 60:         }
 61:         $this->out(__d('cake_console', 'Model classes:'));
 62:         $this->hr();
 63: 
 64:         foreach ($this->models as $model) {
 65:             $this->out(" - {$model}");
 66:         }
 67:         $this->_loadRoutes();
 68:     }
 69: 
 70: /**
 71:  * Prints the help message
 72:  *
 73:  * @return void
 74:  */
 75:     public function help() {
 76:         $out  = 'Console help:';
 77:         $out .= '-------------';
 78:         $out .= 'The interactive console is a tool for testing parts of your app before you';
 79:         $out .= 'write code.';
 80:         $out .= "\n";
 81:         $out .= 'Model testing:';
 82:         $out .= 'To test model results, use the name of your model without a leading $';
 83:         $out .= 'e.g. Foo->find("all")';
 84:         $out .= "\n";
 85:         $out .= 'To dynamically set associations, you can do the following:';
 86:         $out .= "\tModelA bind <association> ModelB";
 87:         $out .= "where the supported associations are hasOne, hasMany, belongsTo, hasAndBelongsToMany";
 88:         $out .= "\n";
 89:         $out .= 'To dynamically remove associations, you can do the following:';
 90:         $out .= "\t ModelA unbind <association> ModelB";
 91:         $out .= "where the supported associations are the same as above";
 92:         $out .= "\n";
 93:         $out .= "To save a new field in a model, you can do the following:";
 94:         $out .= "\tModelA->save(array('foo' => 'bar', 'baz' => 0))";
 95:         $out .= "where you are passing a hash of data to be saved in the format";
 96:         $out .= "of field => value pairs";
 97:         $out .= "\n";
 98:         $out .= "To get column information for a model, use the following:";
 99:         $out .= "\tModelA columns";
100:         $out .= "which returns a list of columns and their type";
101:         $out .= "\n";
102:         $out .= "\n";
103:         $out .= 'Route testing:';
104:         $out .= "\n";
105:         $out .= 'To test URLs against your app\'s route configuration, type:';
106:         $out .= "\n";
107:         $out .= "\tRoute <url>";
108:         $out .= "\n";
109:         $out .= "where url is the path to your your action plus any query parameters,";
110:         $out .= "minus the application's base path.  For example:";
111:         $out .= "\n";
112:         $out .= "\tRoute /posts/view/1";
113:         $out .= "\n";
114:         $out .= "will return something like the following:";
115:         $out .= "\n";
116:         $out .= "\tarray(";
117:         $out .= "\t  [...]";
118:         $out .= "\t  'controller' => 'posts',";
119:         $out .= "\t  'action' => 'view',";
120:         $out .= "\t  [...]";
121:         $out .= "\t)";
122:         $out .= "\n";
123:         $out .= 'Alternatively, you can use simple array syntax to test reverse';
124:         $out .= 'To reload your routes config (Config/routes.php), do the following:';
125:         $out .= "\n";
126:         $out .= "\tRoutes reload";
127:         $out .= "\n";
128:         $out .= 'To show all connected routes, do the following:';
129:         $out .= "\tRoutes show";
130:         $this->out($out);
131:     }
132: 
133: /**
134:  * Override main() to handle action
135:  *
136:  * @param string $command
137:  * @return void
138:  */
139:     public function main($command = null) {
140:         while (true) {
141:             if (empty($command)) {
142:                 $command = trim($this->in(''));
143:             }
144: 
145:             switch ($command) {
146:                 case 'help':
147:                     $this->help();
148:                 break;
149:                 case 'quit':
150:                 case 'exit':
151:                     return true;
152:                 break;
153:                 case 'models':
154:                     $this->out(__d('cake_console', 'Model classes:'));
155:                     $this->hr();
156:                     foreach ($this->models as $model) {
157:                         $this->out(" - {$model}");
158:                     }
159:                 break;
160:                 case (preg_match("/^(\w+) bind (\w+) (\w+)/", $command, $tmp) == true):
161:                     foreach ($tmp as $data) {
162:                         $data = strip_tags($data);
163:                         $data = str_replace($this->badCommandChars, "", $data);
164:                     }
165: 
166:                     $modelA = $tmp[1];
167:                     $association = $tmp[2];
168:                     $modelB = $tmp[3];
169: 
170:                     if ($this->_isValidModel($modelA) && $this->_isValidModel($modelB) && in_array($association, $this->associations)) {
171:                         $this->{$modelA}->bindModel(array($association => array($modelB => array('className' => $modelB))), false);
172:                         $this->out(__d('cake_console', "Created %s association between %s and %s",
173:                             $association, $modelA, $modelB));
174:                     } else {
175:                         $this->out(__d('cake_console', "Please verify you are using valid models and association types"));
176:                     }
177:                 break;
178:                 case (preg_match("/^(\w+) unbind (\w+) (\w+)/", $command, $tmp) == true):
179:                     foreach ($tmp as $data) {
180:                         $data = strip_tags($data);
181:                         $data = str_replace($this->badCommandChars, "", $data);
182:                     }
183: 
184:                     $modelA = $tmp[1];
185:                     $association = $tmp[2];
186:                     $modelB = $tmp[3];
187: 
188:                     // Verify that there is actually an association to unbind
189:                     $currentAssociations = $this->{$modelA}->getAssociated();
190:                     $validCurrentAssociation = false;
191: 
192:                     foreach ($currentAssociations as $model => $currentAssociation) {
193:                         if ($model == $modelB && $association == $currentAssociation) {
194:                             $validCurrentAssociation = true;
195:                         }
196:                     }
197: 
198:                     if ($this->_isValidModel($modelA) && $this->_isValidModel($modelB) && in_array($association, $this->associations) && $validCurrentAssociation) {
199:                         $this->{$modelA}->unbindModel(array($association => array($modelB)));
200:                         $this->out(__d('cake_console', "Removed %s association between %s and %s",
201:                             $association, $modelA, $modelB));
202:                     } else {
203:                         $this->out(__d('cake_console', "Please verify you are using valid models, valid current association, and valid association types"));
204:                     }
205:                 break;
206:                 case (strpos($command, "->find") > 0):
207:                     // Remove any bad info
208:                     $command = strip_tags($command);
209:                     $command = str_replace($this->badCommandChars, "", $command);
210: 
211:                     // Do we have a valid model?
212:                     list($modelToCheck, $tmp) = explode('->', $command);
213: 
214:                     if ($this->_isValidModel($modelToCheck)) {
215:                         $findCommand = "\$data = \$this->$command;";
216:                         @eval($findCommand);
217: 
218:                         if (is_array($data)) {
219:                             foreach ($data as $idx => $results) {
220:                                 if (is_numeric($idx)) { // findAll() output
221:                                     foreach ($results as $modelName => $result) {
222:                                         $this->out("$modelName");
223: 
224:                                         foreach ($result as $field => $value) {
225:                                             if (is_array($value)) {
226:                                                 foreach ($value as $field2 => $value2) {
227:                                                     $this->out("\t$field2: $value2");
228:                                                 }
229: 
230:                                                 $this->out();
231:                                             } else {
232:                                                 $this->out("\t$field: $value");
233:                                             }
234:                                         }
235:                                     }
236:                                 } else { // find() output
237:                                     $this->out($idx);
238: 
239:                                     foreach ($results as $field => $value) {
240:                                         if (is_array($value)) {
241:                                             foreach ($value as $field2 => $value2) {
242:                                                 $this->out("\t$field2: $value2");
243:                                             }
244: 
245:                                             $this->out();
246:                                         } else {
247:                                             $this->out("\t$field: $value");
248:                                         }
249:                                     }
250:                                 }
251:                             }
252:                         } else {
253:                             $this->out();
254:                             $this->out(__d('cake_console', "No result set found"));
255:                         }
256:                     } else {
257:                         $this->out(__d('cake_console', "%s is not a valid model", $modelToCheck));
258:                     }
259: 
260:                 break;
261:                 case (strpos($command, '->save') > 0):
262:                     // Validate the model we're trying to save here
263:                     $command = strip_tags($command);
264:                     $command = str_replace($this->badCommandChars, "", $command);
265:                     list($modelToSave, $tmp) = explode("->", $command);
266: 
267:                     if ($this->_isValidModel($modelToSave)) {
268:                         // Extract the array of data we are trying to build
269:                         list($foo, $data) = explode("->save", $command);
270:                         $data = preg_replace('/^\(*(array)?\(*(.+?)\)*$/i', '\\2', $data);
271:                         $saveCommand = "\$this->{$modelToSave}->save(array('{$modelToSave}' => array({$data})));";
272:                         @eval($saveCommand);
273:                         $this->out(__d('cake_console', 'Saved record for %s', $modelToSave));
274:                     }
275:                 break;
276:                 case (preg_match("/^(\w+) columns/", $command, $tmp) == true):
277:                     $modelToCheck = strip_tags(str_replace($this->badCommandChars, "", $tmp[1]));
278: 
279:                     if ($this->_isValidModel($modelToCheck)) {
280:                         // Get the column info for this model
281:                         $fieldsCommand = "\$data = \$this->{$modelToCheck}->getColumnTypes();";
282:                         @eval($fieldsCommand);
283: 
284:                         if (is_array($data)) {
285:                             foreach ($data as $field => $type) {
286:                                 $this->out("\t{$field}: {$type}");
287:                             }
288:                         }
289:                     } else {
290:                         $this->out(__d('cake_console', "Please verify that you selected a valid model"));
291:                     }
292:                 break;
293:                 case (preg_match("/^routes\s+reload/i", $command, $tmp) == true):
294:                     $router = Router::getInstance();
295:                     if (!$this->_loadRoutes()) {
296:                         $this->out(__d('cake_console', "There was an error loading the routes config. Please check that the file exists and is free of parse errors."));
297:                         break;
298:                     }
299:                     $this->out(__d('cake_console', "Routes configuration reloaded, %d routes connected", count($router->routes)));
300:                 break;
301:                 case (preg_match("/^routes\s+show/i", $command, $tmp) == true):
302:                     $router = Router::getInstance();
303:                     $this->out(implode("\n", Set::extract($router->routes, '{n}.0')));
304:                 break;
305:                 case (preg_match("/^route\s+(\(.*\))$/i", $command, $tmp) == true):
306:                     if ($url = eval('return array' . $tmp[1] . ';')) {
307:                         $this->out(Router::url($url));
308:                     }
309:                 break;
310:                 case (preg_match("/^route\s+(.*)/i", $command, $tmp) == true):
311:                     $this->out(var_export(Router::parse($tmp[1]), true));
312:                 break;
313:                 default:
314:                     $this->out(__d('cake_console', "Invalid command"));
315:                     $this->out();
316:                 break;
317:             }
318:             $command = '';
319:         }
320:     }
321: 
322: /**
323:  * Tells if the specified model is included in the list of available models
324:  *
325:  * @param string $modelToCheck
326:  * @return boolean true if is an available model, false otherwise
327:  */
328:     protected function _isValidModel($modelToCheck) {
329:         return in_array($modelToCheck, $this->models);
330:     }
331: 
332: /**
333:  * Reloads the routes configuration from app/Config/routes.php, and compiles
334:  * all routes found
335:  *
336:  * @return boolean True if config reload was a success, otherwise false
337:  */
338:     protected function _loadRoutes() {
339:         Router::reload();
340:         extract(Router::getNamedExpressions());
341: 
342:         if (!@include APP . 'Config' . DS . 'routes.php') {
343:             return false;
344:         }
345:         CakePlugin::routes();
346: 
347:         Router::parse('/');
348: 
349:         foreach (array_keys(Router::getNamedExpressions()) as $var) {
350:             unset(${$var});
351:         }
352: 
353:         foreach (Router::$routes as $route) {
354:             $route->compile();
355:         }
356:         return true;
357:     }
358: 
359: }
360: 
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