mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merge branch '2.2-lazy-helpers' into 2.2
This commit is contained in:
commit
ed0c5a4746
7 changed files with 96 additions and 21 deletions
|
@ -38,12 +38,6 @@ class PagesController extends AppController {
|
|||
*/
|
||||
public $name = 'Pages';
|
||||
|
||||
/**
|
||||
* Default helper
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $helpers = array('Html', 'Session');
|
||||
|
||||
/**
|
||||
* This controller does not use a model
|
||||
|
|
|
@ -29,13 +29,6 @@
|
|||
*/
|
||||
class PagesController extends AppController {
|
||||
|
||||
/**
|
||||
* Default helper
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $helpers = array('Html');
|
||||
|
||||
/**
|
||||
* This controller does not use a model
|
||||
*
|
||||
|
|
|
@ -92,7 +92,7 @@ class Controller extends Object implements CakeEventListener {
|
|||
* @var mixed A single name as a string or a list of names as an array.
|
||||
* @link http://book.cakephp.org/2.0/en/controllers.html#components-helpers-and-uses
|
||||
*/
|
||||
public $helpers = array('Session', 'Html', 'Form');
|
||||
public $helpers = array();
|
||||
|
||||
/**
|
||||
* An instance of a CakeRequest object that contains information about the current request.
|
||||
|
|
|
@ -67,6 +67,35 @@ class HelperCollectionTest extends CakeTestCase {
|
|||
$this->assertTrue($this->Helpers->enabled('Html'));
|
||||
}
|
||||
|
||||
/**
|
||||
* test lazy loading of helpers
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testLazyLoad() {
|
||||
$result = $this->Helpers->Html;
|
||||
$this->assertInstanceOf('HtmlHelper', $result);
|
||||
|
||||
$result = $this->Helpers->Form;
|
||||
$this->assertInstanceOf('FormHelper', $result);
|
||||
|
||||
App::build(array('Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)));
|
||||
$this->View->plugin = 'TestPlugin';
|
||||
CakePlugin::load(array('TestPlugin'));
|
||||
$result = $this->Helpers->OtherHelper;
|
||||
$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
|
||||
*
|
||||
|
@ -149,8 +178,8 @@ class HelperCollectionTest extends CakeTestCase {
|
|||
$this->assertEquals(array('Form', 'Html'), $result, 'loaded helpers is wrong');
|
||||
|
||||
$this->Helpers->unload('Html');
|
||||
$this->assertFalse(isset($this->Helpers->Html));
|
||||
$this->assertTrue(isset($this->Helpers->Form));
|
||||
$this->assertNotContains('Html', $this->Helpers->attached());
|
||||
$this->assertContains('Form', $this->Helpers->attached());
|
||||
|
||||
$result = $this->Helpers->attached();
|
||||
$this->assertEquals(array('Form'), $result, 'loaded helpers is wrong');
|
||||
|
|
|
@ -798,6 +798,20 @@ class ViewTest extends CakeTestCase {
|
|||
$this->assertInstanceOf('FormHelper', $View->Form, 'Object type is wrong.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* test lazy loading helpers
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testLazyLoadHelpers() {
|
||||
$View = new View($this->PostsController);
|
||||
|
||||
$View->helpers = array();
|
||||
$this->assertInstanceOf('HtmlHelper', $View->Html, 'Object type is wrong.');
|
||||
$this->assertInstanceOf('FormHelper', $View->Form, 'Object type is wrong.');
|
||||
}
|
||||
|
||||
/**
|
||||
* test the correct triggering of helper callbacks
|
||||
*
|
||||
|
|
|
@ -43,6 +43,51 @@ class HelperCollection extends ObjectCollection implements CakeEventListener {
|
|||
$this->_View = $view;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to lazy load a helper based on its name, if it cannot be found
|
||||
* in the application folder, then it tries looking under the current plugin
|
||||
* if any
|
||||
*
|
||||
* @param string $helper The helper name to be loaded
|
||||
* @return boolean wheter the helper could be loaded or not
|
||||
**/
|
||||
public function __isset($helper) {
|
||||
if (parent::__isset($helper)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide public read access to the loaded objects
|
||||
*
|
||||
* @param string $name Name of property to read
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($name) {
|
||||
if ($result = parent::__get($name)) {
|
||||
return $result;
|
||||
}
|
||||
if ($this->__isset($name)) {
|
||||
return $this->_loaded[$name];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
|
|
@ -784,9 +784,6 @@ class View extends Object {
|
|||
* @return mixed
|
||||
*/
|
||||
public function __get($name) {
|
||||
if (isset($this->Helpers->{$name})) {
|
||||
return $this->Helpers->{$name};
|
||||
}
|
||||
switch ($name) {
|
||||
case 'base':
|
||||
case 'here':
|
||||
|
@ -799,9 +796,12 @@ class View extends Object {
|
|||
return $this->request;
|
||||
case 'output':
|
||||
return $this->Blocks->get('content');
|
||||
default:
|
||||
return $this->{$name};
|
||||
}
|
||||
if (isset($this->Helpers->{$name})) {
|
||||
$this->{$name} = $this->Helpers->{$name};
|
||||
return $this->Helpers->{$name};
|
||||
}
|
||||
return $this->{$name};
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue