Making lazy loader throw an exception for missing helpers

This commit is contained in:
Jose Lorenzo Rodriguez 2012-03-28 22:12:46 -04:30
parent 5a41024cf3
commit 18b843467f
2 changed files with 23 additions and 19 deletions

View file

@ -86,6 +86,16 @@ class HelperCollectionTest extends CakeTestCase {
$this->assertInstanceOf('OtherHelperHelper', $result); $this->assertInstanceOf('OtherHelperHelper', $result);
} }
/**
* test lazy loading of helpers
*
* @expectedException MissingHelperException
* @return void
*/
public function testLazyLoadException() {
$result = $this->Helpers->NotAHelper;
}
/** /**
* Tests loading as an alias * Tests loading as an alias
* *

View file

@ -55,9 +55,20 @@ class HelperCollection extends ObjectCollection implements CakeEventListener {
if (parent::__isset($helper)) { if (parent::__isset($helper)) {
return true; return true;
} }
if (!$this->_loadSandbox($helper)) {
return $this->_View->plugin && $this->_loadSandbox($this->_View->plugin . '.' . $helper); try {
$this->load($helper);
} catch (MissingHelperException $exception) {
if ($this->_View->plugin) {
$this->load($this->_View->plugin . '.' . $helper);
return true;
} }
}
if (!empty($exception)) {
throw $exception;
}
return true; return true;
} }
@ -77,23 +88,6 @@ class HelperCollection extends ObjectCollection implements CakeEventListener {
return null; return null;
} }
/**
* Auxiliary function used for lazy loading helpers
* catches any MissingHelperException and converts it into
* a boolean return
*
* @param string $helper The helper name to be loaded
* @return boolean wheter the helper could be loaded or not
**/
protected function _loadSandbox($helper) {
try {
$this->load($helper);
} catch (MissingHelperException $e) {
return false;
}
return true;
}
/** /**
* Loads/constructs a helper. Will return the instance in the registry if it already exists. * Loads/constructs a helper. Will return the instance in the registry if it already exists.
* By setting `$enable` to false you can disable callbacks for a helper. Alternatively you * By setting `$enable` to false you can disable callbacks for a helper. Alternatively you