diff --git a/lib/Cake/Console/Shell.php b/lib/Cake/Console/Shell.php index 8301819498c..aff88e7bb73 100644 --- a/lib/Cake/Console/Shell.php +++ b/lib/Cake/Console/Shell.php @@ -174,6 +174,13 @@ class Shell extends Object { */ protected $_lastWritten = 0; +/** + * Contains helpers which have been previously instantiated + * + * @var array + */ + protected $_helpers = array(); + /** * Constructs this Shell instance. * @@ -779,6 +786,29 @@ public function createFile($path, $contents) { return false; } +/** + * Load given shell helper class + * + * @param string $name Name of the helper class. Supports plugin syntax. + * @return ShellHelper Instance of helper class + * @throws RunTimeException If invalid class name is provided + */ + public function helper($name) + { + list($plugin, $helperClassName) = pluginSplit($name, true); + $helperClassName = Inflector::camelize($name) . "ShellHelper"; + if (isset($this->_helpers[$name])) { + return $this->_helpers[$name]; + } + App::uses($helperClassName, $plugin . "Console/Helper"); + if (!class_exists($helperClassName)) { + throw new RuntimeException("Class " . $helperClassName . " not found"); + } + $helper = new $helperClassName($this->stdout); + $this->_helpers[$name] = $helper; + return $helper; + } + /** * Action to create a Unit Test * diff --git a/lib/Cake/Test/Case/Console/ShellTest.php b/lib/Cake/Test/Case/Console/ShellTest.php index c7f5d86f70d..53ca60b8899 100644 --- a/lib/Cake/Test/Case/Console/ShellTest.php +++ b/lib/Cake/Test/Case/Console/ShellTest.php @@ -21,6 +21,7 @@ App::uses('ShellDispatcher', 'Console'); App::uses('Shell', 'Console'); App::uses('Folder', 'Utility'); +App::uses("ProgressHelper", "Console/Helper"); /** * ShellTestShell class @@ -975,4 +976,25 @@ public function testQuietLog() { $this->Shell->runCommand('foo', array('--quiet')); } +/** + * Test getting an instance of a helper + * + * @return void + */ + public function testGetInstanceOfHelper() + { + $actual = $this->Shell->helper("progress"); + $this->assertInstanceOf("ProgressShellHelper", $actual); + } + +/** + * Test getting an invalid helper + * + * @expectedException RunTimeException + */ + public function testGetInvalidHelper() + { + $this->Shell->helper("tomato"); + } + }