|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
| 4 | + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
| 5 | + * |
| 6 | + * Licensed under The MIT License |
| 7 | + * For full copyright and license information, please see the LICENSE.txt |
| 8 | + * Redistributions of files must retain the above copyright notice. |
| 9 | + * |
| 10 | + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
| 11 | + * @link http://cakephp.org CakePHP(tm) Project |
| 12 | + * @since 3.0.0 |
| 13 | + * @license http://www.opensource.org/licenses/mit-license.php MIT License |
| 14 | + */ |
| 15 | +namespace Cake\Console\Command\Task; |
| 16 | + |
| 17 | +use Cake\Console\Command\Task\BakeTask; |
| 18 | +use Cake\Core\Configure; |
| 19 | +use Cake\Core\Plugin; |
| 20 | + |
| 21 | +/** |
| 22 | + * Behavior code generator. |
| 23 | + */ |
| 24 | +class BehaviorTask extends BakeTask { |
| 25 | + |
| 26 | +/** |
| 27 | + * Tasks to be loaded by this Task |
| 28 | + * |
| 29 | + * @var array |
| 30 | + */ |
| 31 | + public $tasks = ['Test', 'Template']; |
| 32 | + |
| 33 | +/** |
| 34 | + * Override initialize |
| 35 | + * |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + public function initialize() { |
| 39 | + $this->path = current(App::path('Model/Behavior')); |
| 40 | + } |
| 41 | + |
| 42 | +/** |
| 43 | + * Execute method |
| 44 | + * |
| 45 | + * @return void |
| 46 | + */ |
| 47 | + public function execute() { |
| 48 | + parent::execute(); |
| 49 | + $name = Inflector::classify($this->args[0]); |
| 50 | + $this->bake($name); |
| 51 | + $this->bakeTest($name); |
| 52 | + } |
| 53 | + |
| 54 | +/** |
| 55 | + * Generate a class stub |
| 56 | + * |
| 57 | + * @param string $className The classname to generate. |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public function bake($name) { |
| 61 | + $namespace = Configure::read('App.namespace'); |
| 62 | + if ($this->plugin) { |
| 63 | + $namespace = Plugin::getNamespace($this->plugin); |
| 64 | + } |
| 65 | + $data = compact('name', 'namespace'); |
| 66 | + $this->template->set($data); |
| 67 | + $contents = $this->Template->generate('classes', 'behavior'); |
| 68 | + |
| 69 | + $path = $this->getPath(); |
| 70 | + $filename = $path . $name . 'Behavior.php'; |
| 71 | + $this->createFile($filename, $contents); |
| 72 | + return $contents; |
| 73 | + } |
| 74 | + |
| 75 | +/** |
| 76 | + * Generate a test case. |
| 77 | + * |
| 78 | + * @return void |
| 79 | + */ |
| 80 | + public function bakeTest($className) { |
| 81 | + if (!empty($this->params['no-test'])) { |
| 82 | + return; |
| 83 | + } |
| 84 | + $this->Test->plugin = $this->plugin; |
| 85 | + return $this->Test->bake('Behavior', $className); |
| 86 | + } |
| 87 | + |
| 88 | +/** |
| 89 | + * Gets the option parser instance and configures it. |
| 90 | + * |
| 91 | + * @return \Cake\Console\ConsoleOptionParser |
| 92 | + */ |
| 93 | + public function getOptionParser() { |
| 94 | + $parser = parent::getOptionParser(); |
| 95 | + $parser->description( |
| 96 | + __d('cake_console', 'Bake a behavior class file.') |
| 97 | + )->addArgument('name', [ |
| 98 | + 'help' => __d('cake_console', 'Name of the Behavior to bake. Can use Plugin.name to bake controllers into plugins.') |
| 99 | + ])->addOption('plugin', [ |
| 100 | + 'short' => 'p', |
| 101 | + 'help' => __d('cake_console', 'Plugin to bake the controller into.') |
| 102 | + ])->addOption('theme', [ |
| 103 | + 'short' => 't', |
| 104 | + 'help' => __d('cake_console', 'Theme to use when baking code.') |
| 105 | + ])->addOption('no-test', [ |
| 106 | + 'boolean' => true, |
| 107 | + 'help' => __d('cake_console', 'Do not generate a test skeleton.') |
| 108 | + ])->addOption('force', [ |
| 109 | + 'short' => 'f', |
| 110 | + 'boolean' => true, |
| 111 | + 'help' => __d('cake_console', 'Force overwriting existing files without prompting.') |
| 112 | + ]); |
| 113 | + |
| 114 | + return $parser; |
| 115 | + } |
| 116 | +} |
0 commit comments