Skip to content

Commit

Permalink
Making lazy loader throw an exception for missing helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 29, 2012
1 parent 5a41024 commit 18b8434
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
10 changes: 10 additions & 0 deletions lib/Cake/Test/Case/View/HelperCollectionTest.php
Expand Up @@ -86,6 +86,16 @@ public function testLazyLoad() {
$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
*
Expand Down
32 changes: 13 additions & 19 deletions lib/Cake/View/HelperCollection.php
Expand Up @@ -55,9 +55,20 @@ public function __isset($helper) {
if (parent::__isset($helper)) {
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;
}

Expand All @@ -77,23 +88,6 @@ public function __get($name) {
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.
* By setting `$enable` to false you can disable callbacks for a helper. Alternatively you
Expand Down

0 comments on commit 18b8434

Please sign in to comment.