mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Making lazy loader throw an exception for missing helpers
This commit is contained in:
parent
5a41024cf3
commit
18b843467f
2 changed files with 23 additions and 19 deletions
|
@ -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
|
||||||
*
|
*
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue