Skip to content
Permalink
Browse files

Add helper method to shell class for loading/fetching helper instances

  • Loading branch information...
Gareth Ellis
Gareth Ellis committed Dec 1, 2015
1 parent 2854940 commit 53b9dc83f6dcc163e36aabffb0366f9691e45c46
Showing with 52 additions and 0 deletions.
  1. +30 −0 lib/Cake/Console/Shell.php
  2. +22 −0 lib/Cake/Test/Case/Console/ShellTest.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
*
@@ -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");
}
}

0 comments on commit 53b9dc8

Please sign in to comment.
You can’t perform that action at this time.