Last active
November 18, 2016 17:44
-
-
Save arodu/34839ba2b5d0b107c6f377868364e7ed to your computer and use it in GitHub Desktop.
Baking everything with scaffold
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| Baking everything with scaffold | |
| Source: http://www.rndmr.com/cakephp-tricks-baking-everything-with-scaffold-239 | |
| File: lib/Cake/Console/Command/Task/ControllerTask.php | |
| How to use: | |
| cake bake controller all --scaffold | |
| */ | |
| /* ... */ | |
| /** | |
| * Bake All the controllers at once. Will only bake controllers for models that exist. | |
| * | |
| * @return void | |
| */ | |
| public function all() { | |
| $this->interactive = false; | |
| $this->listAll($this->connection, false); | |
| ClassRegistry::config('Model', array('ds' => $this->connection)); | |
| $unitTestExists = $this->_checkUnitTest(); | |
| $admin = false; | |
| if (!empty($this->params['admin'])) { | |
| $admin = $this->Project->getPrefix(); | |
| } | |
| $controllersCreated = 0; | |
| foreach ($this->__tables as $table) { | |
| $model = $this->_modelName($table); | |
| $controller = $this->_controllerName($model); | |
| App::uses($model, 'Model'); | |
| if (class_exists($model)) { | |
| $actions = $this->bakeActions($controller); | |
| if ($admin) { | |
| $this->out(__d('cake_console', 'Adding %s methods', $admin)); | |
| $actions .= "\n" . $this->bakeActions($controller, $admin); | |
| } | |
| /* Add this lines */ | |
| if ($this->params['scaffold']) { | |
| $actions = 'scaffold'; | |
| } | |
| /* ---- */ | |
| if ($this->bake($controller, $actions) && $unitTestExists) { | |
| $this->bakeTest($controller); | |
| } | |
| $controllersCreated++; | |
| } | |
| } | |
| if (!$controllersCreated) { | |
| $this->out(__d('cake_console', 'No Controllers were baked, Models need to exist before Controllers can be baked.')); | |
| } | |
| } | |
| /* ... */ | |
| /** | |
| * Gets the option parser instance and configures it. | |
| * | |
| * @return ConsoleOptionParser | |
| */ | |
| public function getOptionParser() { | |
| $parser = parent::getOptionParser(); | |
| $parser->description( | |
| __d('cake_console', 'Bake a controller for a model. Using options you can bake public, admin or both.' | |
| ))->addArgument('name', array( | |
| 'help' => __d('cake_console', 'Name of the controller to bake. Can use Plugin.name to bake controllers into plugins.') | |
| ))->addOption('public', array( | |
| 'help' => __d('cake_console', 'Bake a controller with basic crud actions (index, view, add, edit, delete).'), | |
| 'boolean' => true | |
| /* Add this lines */ | |
| ))->addOption('scaffold', array( | |
| 'help' => __d('cake_console', 'Bake a controller using scaffold.'), | |
| 'boolean' => true | |
| /* ---- */ | |
| ))->addOption('admin', array( | |
| 'help' => __d('cake_console', 'Bake a controller with crud actions for one of the Routing.prefixes.'), | |
| 'boolean' => true | |
| ))->addOption('plugin', array( | |
| 'short' => 'p', | |
| 'help' => __d('cake_console', 'Plugin to bake the controller into.') | |
| ))->addOption('connection', array( | |
| 'short' => 'c', | |
| 'help' => __d('cake_console', 'The connection the controller\'s model is on.') | |
| ))->addOption('theme', array( | |
| 'short' => 't', | |
| 'help' => __d('cake_console', 'Theme to use when baking code.') | |
| ))->addOption('force', array( | |
| 'short' => 'f', | |
| 'help' => __d('cake_console', 'Force overwriting existing files without prompting.') | |
| ))->addSubcommand('all', array( | |
| 'help' => __d('cake_console', 'Bake all controllers with CRUD methods.') | |
| ))->epilog( | |
| __d('cake_console', 'Omitting all arguments and options will enter into an interactive mode.') | |
| ); | |
| return $parser; | |
| } | |
| } | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use:
cake bake controller all --scaffold