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

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

Packages

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

Classes

  • AclShell
  • ApiShell
  • BakeShell
  • CommandListShell
  • ConsoleShell
  • I18nShell
  • SchemaShell
  • ServerShell
  • TestShell
  • TestsuiteShell
  • UpgradeShell
  1: <?php
  2: /**
  3:  * Command-line code generation utility to automate programmer chores.
  4:  *
  5:  * Bake is CakePHP's code generation script, which can help you kickstart
  6:  * application development by writing fully functional skeleton controllers,
  7:  * models, and views. Going further, Bake can also write Unit Tests for you.
  8:  *
  9:  * PHP 5
 10:  *
 11:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 12:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 13:  *
 14:  * Licensed under The MIT License
 15:  * For full copyright and license information, please see the LICENSE.txt
 16:  * Redistributions of files must retain the above copyright notice.
 17:  *
 18:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 19:  * @link          http://cakephp.org CakePHP(tm) Project
 20:  * @since         CakePHP(tm) v 1.2.0.5012
 21:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 22:  */
 23: 
 24: App::uses('AppShell', 'Console/Command');
 25: App::uses('Model', 'Model');
 26: 
 27: /**
 28:  * Command-line code generation utility to automate programmer chores.
 29:  *
 30:  * Bake is CakePHP's code generation script, which can help you kickstart
 31:  * application development by writing fully functional skeleton controllers,
 32:  * models, and views. Going further, Bake can also write Unit Tests for you.
 33:  *
 34:  * @package       Cake.Console.Command
 35:  * @link          http://book.cakephp.org/2.0/en/console-and-shells/code-generation-with-bake.html
 36:  */
 37: class BakeShell extends AppShell {
 38: 
 39: /**
 40:  * Contains tasks to load and instantiate
 41:  *
 42:  * @var array
 43:  */
 44:     public $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test');
 45: 
 46: /**
 47:  * The connection being used.
 48:  *
 49:  * @var string
 50:  */
 51:     public $connection = 'default';
 52: 
 53: /**
 54:  * Assign $this->connection to the active task if a connection param is set.
 55:  *
 56:  * @return void
 57:  */
 58:     public function startup() {
 59:         parent::startup();
 60:         Configure::write('debug', 2);
 61:         Configure::write('Cache.disable', 1);
 62: 
 63:         $task = Inflector::classify($this->command);
 64:         if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) {
 65:             if (isset($this->params['connection'])) {
 66:                 $this->{$task}->connection = $this->params['connection'];
 67:             }
 68:         }
 69:         if (isset($this->params['connection'])) {
 70:             $this->connection = $this->params['connection'];
 71:         }
 72:     }
 73: 
 74: /**
 75:  * Override main() to handle action
 76:  *
 77:  * @return mixed
 78:  */
 79:     public function main() {
 80:         if (!is_dir($this->DbConfig->path)) {
 81:             $path = $this->Project->execute();
 82:             if (!empty($path)) {
 83:                 $this->DbConfig->path = $path . 'Config' . DS;
 84:             } else {
 85:                 return false;
 86:             }
 87:         }
 88: 
 89:         if (!config('database')) {
 90:             $this->out(__d('cake_console', 'Your database configuration was not found. Take a moment to create one.'));
 91:             $this->args = null;
 92:             return $this->DbConfig->execute();
 93:         }
 94:         $this->out(__d('cake_console', 'Interactive Bake Shell'));
 95:         $this->hr();
 96:         $this->out(__d('cake_console', '[D]atabase Configuration'));
 97:         $this->out(__d('cake_console', '[M]odel'));
 98:         $this->out(__d('cake_console', '[V]iew'));
 99:         $this->out(__d('cake_console', '[C]ontroller'));
100:         $this->out(__d('cake_console', '[P]roject'));
101:         $this->out(__d('cake_console', '[F]ixture'));
102:         $this->out(__d('cake_console', '[T]est case'));
103:         $this->out(__d('cake_console', '[Q]uit'));
104: 
105:         $classToBake = strtoupper($this->in(__d('cake_console', 'What would you like to Bake?'), array('D', 'M', 'V', 'C', 'P', 'F', 'T', 'Q')));
106:         switch ($classToBake) {
107:             case 'D':
108:                 $this->DbConfig->execute();
109:                 break;
110:             case 'M':
111:                 $this->Model->execute();
112:                 break;
113:             case 'V':
114:                 $this->View->execute();
115:                 break;
116:             case 'C':
117:                 $this->Controller->execute();
118:                 break;
119:             case 'P':
120:                 $this->Project->execute();
121:                 break;
122:             case 'F':
123:                 $this->Fixture->execute();
124:                 break;
125:             case 'T':
126:                 $this->Test->execute();
127:                 break;
128:             case 'Q':
129:                 return $this->_stop();
130:             default:
131:                 $this->out(__d('cake_console', 'You have made an invalid selection. Please choose a type of class to Bake by entering D, M, V, F, T, or C.'));
132:         }
133:         $this->hr();
134:         $this->main();
135:     }
136: 
137: /**
138:  * Quickly bake the MVC
139:  *
140:  * @return void
141:  */
142:     public function all() {
143:         $this->out('Bake All');
144:         $this->hr();
145: 
146:         if (!isset($this->params['connection']) && empty($this->connection)) {
147:             $this->connection = $this->DbConfig->getConfig();
148:         }
149: 
150:         if (empty($this->args)) {
151:             $this->Model->interactive = true;
152:             $name = $this->Model->getName($this->connection);
153:         }
154: 
155:         foreach (array('Model', 'Controller', 'View') as $task) {
156:             $this->{$task}->connection = $this->connection;
157:             $this->{$task}->interactive = false;
158:         }
159: 
160:         if (!empty($this->args[0])) {
161:             $name = $this->args[0];
162:         }
163: 
164:         $modelExists = false;
165:         $model = $this->_modelName($name);
166: 
167:         App::uses('AppModel', 'Model');
168:         App::uses($model, 'Model');
169:         if (class_exists($model)) {
170:             $object = new $model();
171:             $modelExists = true;
172:         } else {
173:             $object = new Model(array('name' => $name, 'ds' => $this->connection));
174:         }
175: 
176:         $modelBaked = $this->Model->bake($object, false);
177: 
178:         if ($modelBaked && $modelExists === false) {
179:             if ($this->_checkUnitTest()) {
180:                 $this->Model->bakeFixture($model);
181:                 $this->Model->bakeTest($model);
182:             }
183:             $modelExists = true;
184:         }
185: 
186:         if ($modelExists === true) {
187:             $controller = $this->_controllerName($name);
188:             if ($this->Controller->bake($controller, $this->Controller->bakeActions($controller))) {
189:                 if ($this->_checkUnitTest()) {
190:                     $this->Controller->bakeTest($controller);
191:                 }
192:             }
193:             App::uses($controller . 'Controller', 'Controller');
194:             if (class_exists($controller . 'Controller')) {
195:                 $this->View->args = array($name);
196:                 $this->View->execute();
197:             }
198:             $this->out('', 1, Shell::QUIET);
199:             $this->out(__d('cake_console', '<success>Bake All complete</success>'), 1, Shell::QUIET);
200:             array_shift($this->args);
201:         } else {
202:             $this->error(__d('cake_console', 'Bake All could not continue without a valid model'));
203:         }
204:         return $this->_stop();
205:     }
206: 
207: /**
208:  * get the option parser.
209:  *
210:  * @return void
211:  */
212:     public function getOptionParser() {
213:         $parser = parent::getOptionParser();
214:         return $parser->description(__d('cake_console',
215:             'The Bake script generates controllers, views and models for your application.' .
216:             ' If run with no command line arguments, Bake guides the user through the class creation process.' .
217:             ' You can customize the generation process by telling Bake where different parts of your application are using command line arguments.'
218:         ))->addSubcommand('all', array(
219:             'help' => __d('cake_console', 'Bake a complete MVC. optional <name> of a Model'),
220:         ))->addSubcommand('project', array(
221:             'help' => __d('cake_console', 'Bake a new app folder in the path supplied or in current directory if no path is specified'),
222:             'parser' => $this->Project->getOptionParser()
223:         ))->addSubcommand('plugin', array(
224:             'help' => __d('cake_console', 'Bake a new plugin folder in the path supplied or in current directory if no path is specified.'),
225:             'parser' => $this->Plugin->getOptionParser()
226:         ))->addSubcommand('db_config', array(
227:             'help' => __d('cake_console', 'Bake a database.php file in config directory.'),
228:             'parser' => $this->DbConfig->getOptionParser()
229:         ))->addSubcommand('model', array(
230:             'help' => __d('cake_console', 'Bake a model.'),
231:             'parser' => $this->Model->getOptionParser()
232:         ))->addSubcommand('view', array(
233:             'help' => __d('cake_console', 'Bake views for controllers.'),
234:             'parser' => $this->View->getOptionParser()
235:         ))->addSubcommand('controller', array(
236:             'help' => __d('cake_console', 'Bake a controller.'),
237:             'parser' => $this->Controller->getOptionParser()
238:         ))->addSubcommand('fixture', array(
239:             'help' => __d('cake_console', 'Bake a fixture.'),
240:             'parser' => $this->Fixture->getOptionParser()
241:         ))->addSubcommand('test', array(
242:             'help' => __d('cake_console', 'Bake a unit test.'),
243:             'parser' => $this->Test->getOptionParser()
244:         ))->addOption('connection', array(
245:             'help' => __d('cake_console', 'Database connection to use in conjunction with `bake all`.'),
246:             'short' => 'c',
247:             'default' => 'default'
248:         ));
249:     }
250: 
251: }
252: 
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