Browse Source

Add InstanceConfig to ConsoleHelpers.

Implement InstanceConfig makes ConsoleHelpers work the same as many
other objects in CakePHP. This consistency is nice for developers.
Mark Story 11 years ago
parent
commit
9791dcf159

+ 4 - 3
src/Console/ConsoleIo.php

@@ -381,12 +381,13 @@ class ConsoleIo
      * object has not already been loaded, it will be loaded and constructed.
      *
      * @param string $name The name of the helper to render
-     * @return Cake\Console\Helper The created helper instance.
+     * @param array $settings Configuration data for the helper.
+     * @return \Cake\Console\Helper The created helper instance.
      */
-    public function helper($name)
+    public function helper($name, array $settings = [])
     {
         $name = ucfirst($name);
-        return $this->_helpers->load($name);
+        return $this->_helpers->load($name, $settings);
     }
 
     /**

+ 15 - 3
src/Console/Helper.php

@@ -15,6 +15,7 @@
 namespace Cake\Console;
 
 use Cake\Console\ConsoleIo;
+use Cake\Core\InstanceConfigTrait;
 
 /**
  * Base class for Helpers.
@@ -25,14 +26,25 @@ use Cake\Console\ConsoleIo;
  */
 abstract class Helper
 {
+    use InstanceConfigTrait;
+
+    /**
+     * Default config for this helper.
+     *
+     * @var array
+     */
+    protected $_defaultConfig = [];
+
     /**
-     * Constructor
+     * Constructor.
      *
-     * @param \Cake\Console\ConsoleIo $io An io instance.
+     * @param \Cake\Console\ConsoleIo $io The ConsoleIo instance to use.
+     * @param array $config The settings for this helper.
      */
-    public function __construct(ConsoleIo $io)
+    public function __construct(ConsoleIo $io, array $config = [])
     {
         $this->_io = $io;
+        $this->config($config);
     }
 
     /**

+ 1 - 1
src/Console/HelperRegistry.php

@@ -86,6 +86,6 @@ class HelperRegistry extends ObjectRegistry
      */
     protected function _create($class, $alias, $settings)
     {
-        return new $class($this->_io);
+        return new $class($this->_io, $settings);
     }
 }

+ 13 - 2
tests/TestCase/Console/HelperRegistryTest.php

@@ -51,7 +51,7 @@ class HelperRegistryTest extends TestCase
     }
 
     /**
-     * test triggering callbacks on loaded tasks
+     * test loading helpers.
      *
      * @return void
      */
@@ -66,7 +66,18 @@ class HelperRegistryTest extends TestCase
     }
 
     /**
-     * test missingtask exception
+     * test triggering callbacks on loaded helpers
+     *
+     * @return void
+     */
+    public function testLoadWithConfig()
+    {
+        $result = $this->helpers->load('Simple', ['key' => 'value']);
+        $this->assertEquals('value', $result->config('key'));
+    }
+
+    /**
+     * test missing helper exception
      *
      * @expectedException \Cake\Console\Exception\MissingHelperException
      * @return void