Browse Source

Use an abstract class instead of public properties.

Having an abstract class will make the bake task collector much simpler
as it we can detect base classes like SimpleBakeTask. Having methods
means implementations could use custom code if they wanted.
mark_story 12 years ago
parent
commit
13f6feb5df

+ 12 - 12
src/Console/Command/Task/BehaviorTask.php

@@ -29,24 +29,24 @@ class BehaviorTask extends SimpleBakeTask {
 	public $pathFragment = 'Model/Behavior/';
 
 /**
- * The name of the task used in menus and output.
- *
- * @var string
+ * {@inheritDoc}
  */
-	public $name = 'behavior';
+	public function name() {
+		return 'behavior';
+	}
 
 /**
- * The suffix appended to generated class files.
- *
- * @var string
+ * {@inheritDoc}
  */
-	public $suffix = 'Behavior';
+	public function fileName($name) {
+		return $name . 'Behavior.php';
+	}
 
 /**
- * Template name to use.
- *
- * @var string
+ * {@inheritDoc}
  */
-	public $template = 'behavior';
+	public function template() {
+		return 'behavior';
+	}
 
 }

+ 12 - 12
src/Console/Command/Task/ComponentTask.php

@@ -29,24 +29,24 @@ class ComponentTask extends SimpleBakeTask {
 	public $pathFragment = 'Controller/Component/';
 
 /**
- * The name of the task used in menus and output.
- *
- * @var string
+ * {@inheritDoc}
  */
-	public $name = 'component';
+	public function name() {
+		return 'component';
+	}
 
 /**
- * The suffix appended to generated class files.
- *
- * @var string
+ * {@inheritDoc}
  */
-	public $suffix = 'Component';
+	public function fileName($name) {
+		return $name . 'Component.php';
+	}
 
 /**
- * Template name to use.
- *
- * @var string
+ * {@inheritDoc}
  */
-	public $template = 'component';
+	public function template() {
+		return 'component';
+	}
 
 }

+ 12 - 12
src/Console/Command/Task/HelperTask.php

@@ -29,24 +29,24 @@ class HelperTask extends SimpleBakeTask {
 	public $pathFragment = 'View/Helper/';
 
 /**
- * The name of the task used in menus and output.
- *
- * @var string
+ * {@inheritDoc}
  */
-	public $name = 'helper';
+	public function name() {
+		return 'helper';
+	}
 
 /**
- * The suffix appended to generated class files.
- *
- * @var string
+ * {@inheritDoc}
  */
-	public $suffix = 'Helper';
+	public function fileName($name) {
+		return $name . 'Helper.php';
+	}
 
 /**
- * Template name to use.
- *
- * @var string
+ * {@inheritDoc}
  */
-	public $template = 'helper';
+	public function template() {
+		return 'helper';
+	}
 
 }

+ 30 - 27
src/Console/Command/Task/SimpleBakeTask.php

@@ -23,7 +23,7 @@ use Cake\Utility\Inflector;
 /**
  * Base class for simple bake tasks code generator.
  */
-class SimpleBakeTask extends BakeTask {
+abstract class SimpleBakeTask extends BakeTask {
 
 /**
  * Tasks to be loaded by this Task
@@ -33,32 +33,39 @@ class SimpleBakeTask extends BakeTask {
 	public $tasks = ['Test', 'Template'];
 
 /**
- * Task name used in path generation.
+ * Get the generated object's name.
  *
- * @var string
+ * @return string
  */
-	public $pathFragment;
+	abstract public function name();
 
 /**
- * The name of the task used in menus and output.
+ * Get the generated object's filename without the leading path.
  *
- * @var string
+ * @param string $name The name of the object being generated
+ * @return string
  */
-	public $name;
+	abstract public function fileName($name);
 
 /**
- * The suffix appended to generated class files.
+ * Get the template name.
  *
- * @var string
+ * @return string
  */
-	public $suffix;
+	abstract public function template();
 
 /**
- * Template name to use.
+ * Get template data.
  *
- * @var string
+ * @return array
  */
-	public $template;
+	public function templateData() {
+		$namespace = Configure::read('App.namespace');
+		if ($this->plugin) {
+			$namespace = Plugin::getNamespace($this->plugin);
+		}
+		return ['namespace' => $namespace];
+	}
 
 /**
  * Execute method
@@ -79,16 +86,11 @@ class SimpleBakeTask extends BakeTask {
  * @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', $this->template);
+		$this->Template->set('name', $name);
+		$this->Template->set($this->templateData());
+		$contents = $this->Template->generate('classes', $this->template());
 
-		$path = $this->getPath();
-		$filename = $path . $name . $this->suffix . '.php';
+		$filename = $this->getPath() . $this->fileName($name);
 		$this->createFile($filename, $contents);
 		return $contents;
 	}
@@ -103,7 +105,7 @@ class SimpleBakeTask extends BakeTask {
 			return;
 		}
 		$this->Test->plugin = $this->plugin;
-		return $this->Test->bake($this->name, $className);
+		return $this->Test->bake($this->name(), $className);
 	}
 
 /**
@@ -113,18 +115,19 @@ class SimpleBakeTask extends BakeTask {
  */
 	public function getOptionParser() {
 		$parser = parent::getOptionParser();
+		$name = $this->name();
 		$parser->description(
-			__d('cake_console', 'Bake a %s class file.', $this->name)
+			__d('cake_console', 'Bake a %s class file.', $name)
 		)->addArgument('name', [
 			'help' => __d(
 				'cake_console',
 				'Name of the %s to bake. Can use Plugin.name to bake %s files into plugins.',
-				$this->name,
-				$this->name
+				$name,
+				$name
 			)
 		])->addOption('plugin', [
 			'short' => 'p',
-			'help' => __d('cake_console', 'Plugin to bake the %s into.', $this->name)
+			'help' => __d('cake_console', 'Plugin to bake the %s into.', $name)
 		])->addOption('theme', [
 			'short' => 't',
 			'help' => __d('cake_console', 'Theme to use when baking code.')

+ 43 - 6
tests/TestCase/Console/Command/Task/SimpleBakeTaskTest.php

@@ -21,7 +21,6 @@ use Cake\TestSuite\TestCase;
 
 /**
  * SimpleBakeTaskTest class
- *
  */
 class SimpleBakeTaskTest extends TestCase {
 
@@ -35,8 +34,9 @@ class SimpleBakeTaskTest extends TestCase {
 		$out = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
 		$in = $this->getMock('Cake\Console\ConsoleInput', [], [], '', false);
 
-		$this->Task = $this->getMock('Cake\Console\Command\Task\SimpleBakeTask',
-			['in', 'err', 'createFile', '_stop', 'clear'],
+		$this->Task = $this->getMock(
+			'Cake\Console\Command\Task\SimpleBakeTask',
+			['in', 'err', 'createFile', '_stop', 'name', 'template', 'fileName'],
 			[$out, $out, $in]
 		);
 		$this->Task->Test = $this->getMock('Cake\Console\Command\Task\TestTask',
@@ -47,9 +47,18 @@ class SimpleBakeTaskTest extends TestCase {
 		$this->Task->Template->initialize();
 
 		$this->Task->pathFragment = 'Model/Behavior/';
-		$this->Task->suffix = 'Behavior';
-		$this->Task->template = 'behavior';
-		$this->Task->name = 'behavior';
+
+		$this->Task->expects($this->any())
+			->method('name')
+			->will($this->returnValue('behavior'));
+
+		$this->Task->expects($this->any())
+			->method('template')
+			->will($this->returnValue('behavior'));
+
+		$this->Task->expects($this->any())
+			->method('fileName')
+			->will($this->returnValue('ExampleBehavior.php'));
 	}
 
 /**
@@ -145,4 +154,32 @@ class SimpleBakeTaskTest extends TestCase {
 		$this->assertContains('class ExampleBehavior extends Behavior {', $result);
 	}
 
+/**
+ * Provider for subclasses.
+ *
+ * @return array
+ */
+	public function subclassProvider() {
+		return [
+			['Cake\Console\Command\Task\BehaviorTask'],
+			['Cake\Console\Command\Task\ComponentTask'],
+			['Cake\Console\Command\Task\HelperTask'],
+		];
+	}
+
+/**
+ * Test that the various implementations are sane.
+ *
+ * @dataProvider subclassProvider
+ * @return void
+ */
+	public function testImplementations($class) {
+		$out = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
+		$in = $this->getMock('Cake\Console\ConsoleInput', [], [], '', false);
+		$task = new $class($out, $out, $in);
+		$this->assertInternalType('string', $task->name());
+		$this->assertInternalType('string', $task->fileName('Example'));
+		$this->assertInternalType('string', $task->template());
+	}
+
 }