Browse Source

First pass at a behavior bake task.

mark_story 12 years ago
parent
commit
51bf8768c6

+ 7 - 0
src/Console/Command/Task/BakeTask.php

@@ -27,6 +27,13 @@ use Cake\Core\Configure;
 class BakeTask extends Shell {
 
 /**
+ * The default path to bake files into.
+ *
+ * @var string
+ */
+	public $path;
+
+/**
  * Name of plugin
  *
  * @var string

+ 116 - 0
src/Console/Command/Task/BehaviorTask.php

@@ -0,0 +1,116 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Console\Command\Task;
+
+use Cake\Console\Command\Task\BakeTask;
+use Cake\Core\Configure;
+use Cake\Core\Plugin;
+
+/**
+ * Behavior code generator.
+ */
+class BehaviorTask extends BakeTask {
+
+/**
+ * Tasks to be loaded by this Task
+ *
+ * @var array
+ */
+	public $tasks = ['Test', 'Template'];
+
+/**
+ * Override initialize
+ *
+ * @return void
+ */
+	public function initialize() {
+		$this->path = current(App::path('Model/Behavior'));
+	}
+
+/**
+ * Execute method
+ *
+ * @return void
+ */
+	public function execute() {
+		parent::execute();
+		$name = Inflector::classify($this->args[0]);
+		$this->bake($name);
+		$this->bakeTest($name);
+	}
+
+/**
+ * Generate a class stub
+ *
+ * @param string $className The classname to generate.
+ * @return void
+ */
+	public function bake($name) {
+		$namespace = Configure::read('App.namespace');
+		if ($this->plugin) {
+			$namespace = Plugin::getNamespace($this->plugin);
+		}
+		$data = compact('name', 'namespace');
+		$this->template->set($data);
+		$contents = $this->Template->generate('classes', 'behavior');
+
+		$path = $this->getPath();
+		$filename = $path . $name . 'Behavior.php';
+		$this->createFile($filename, $contents);
+		return $contents;
+	}
+
+/**
+ * Generate a test case.
+ *
+ * @return void
+ */
+	public function bakeTest($className) {
+		if (!empty($this->params['no-test'])) {
+			return;
+		}
+		$this->Test->plugin = $this->plugin;
+		return $this->Test->bake('Behavior', $className);
+	}
+
+/**
+ * Gets the option parser instance and configures it.
+ *
+ * @return \Cake\Console\ConsoleOptionParser
+ */
+	public function getOptionParser() {
+		$parser = parent::getOptionParser();
+		$parser->description(
+			__d('cake_console', 'Bake a behavior class file.')
+		)->addArgument('name', [
+			'help' => __d('cake_console', 'Name of the Behavior to bake. Can use Plugin.name to bake controllers into plugins.')
+		])->addOption('plugin', [
+			'short' => 'p',
+			'help' => __d('cake_console', 'Plugin to bake the controller into.')
+		])->addOption('theme', [
+			'short' => 't',
+			'help' => __d('cake_console', 'Theme to use when baking code.')
+		])->addOption('no-test', [
+			'boolean' => true,
+			'help' => __d('cake_console', 'Do not generate a test skeleton.')
+		])->addOption('force', [
+			'short' => 'f',
+			'boolean' => true,
+			'help' => __d('cake_console', 'Force overwriting existing files without prompting.')
+		]);
+
+		return $parser;
+	}
+}

+ 25 - 0
src/Console/Templates/default/classes/behavior.ctp

@@ -0,0 +1,25 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+echo "<?php\n"; ?>
+namespace <?= $namespace ?>\Model\Behavior;
+
+use Cake\ORM\Behavior;
+
+/**
+ * <?= $name ?> behavior
+ */
+class <?= $name ?>Behavior extends Behavior {
+
+}