Merge pull request #3824 from NickBeeuwsaert/element_events

Added view type getter and ensured element render events would get the proper type
This commit is contained in:
Mark Story 2014-06-28 06:21:46 -04:00
commit 187272c06a
2 changed files with 97 additions and 8 deletions

View file

@ -22,6 +22,8 @@ App::uses('Controller', 'Controller');
App::uses('CacheHelper', 'View/Helper');
App::uses('HtmlHelper', 'View/Helper');
App::uses('ErrorHandler', 'Error');
App::uses('CakeEventManager', 'Event');
App::uses('CakeEventListener', 'Event');
/**
* ViewPostsController class
@ -238,6 +240,61 @@ class TestObjectWithToString {
class TestObjectWithoutToString {
}
/**
* Class TestViewEventListener
*
* An event listener to test cakePHP events
*/
class TestViewEventListener implements CakeEventListener {
/**
* type of view before rendering has occurred
*
* @var string
*/
public $beforeRenderViewType;
/**
* type of view after rendering has occurred
*
* @var string
*/
public $afterRenderViewType;
/**
* implementedEvents method
*
* @return array
*/
public function implementedEvents() {
return array(
'View.beforeRender' => 'beforeRender',
'View.afterRender' => 'afterRender'
);
}
/**
* beforeRender method
*
* @param CakeEvent $event the event being sent
* @return void
*/
public function beforeRender($event) {
$this->beforeRenderViewType = $event->subject()->getCurrentType();
}
/**
* afterRender method
*
* @param CakeEvent $event the event being sent
* @return void
*/
public function afterRender($event) {
$this->afterRenderViewType = $event->subject()->getCurrentType();
}
}
/**
* ViewTest class
*
@ -806,6 +863,30 @@ class ViewTest extends CakeTestCase {
Cache::clear(true, 'test_view');
Cache::drop('test_view');
}
/**
* Test element events
*
* @return void
*/
public function testViewEvent() {
$View = new View($this->PostsController);
$View->autoLayout = false;
$listener = new TestViewEventListener();
$View->getEventManager()->attach($listener);
$View->render('index');
$this->assertEquals(View::TYPE_VIEW, $listener->beforeRenderViewType);
$this->assertEquals(View::TYPE_VIEW, $listener->afterRenderViewType);
$this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
$View->element('test_element', array(), array('callbacks' => true));
$this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
$this->assertEquals(View::TYPE_ELEMENT, $listener->beforeRenderViewType);
$this->assertEquals(View::TYPE_ELEMENT, $listener->afterRenderViewType);
}
/**

View file

@ -813,7 +813,14 @@ class View extends Object {
}
$this->viewVars = $data + $this->viewVars;
}
/**
* Retrieve the current view type
*
* @return string
*/
public function getCurrentType() {
return $this->_currentType;
}
/**
* Magic accessor for helpers. Provides access to attributes that were deprecated.
*
@ -1191,22 +1198,23 @@ class View extends Object {
* @return string
*/
protected function _renderElement($file, $data, $options) {
$current = $this->_current;
$restore = $this->_currentType;
$this->_currentType = self::TYPE_ELEMENT;
if ($options['callbacks']) {
$this->getEventManager()->dispatch(new CakeEvent('View.beforeRender', $this, array($file)));
}
$current = $this->_current;
$restore = $this->_currentType;
$this->_currentType = self::TYPE_ELEMENT;
$element = $this->_render($file, array_merge($this->viewVars, $data));
$this->_currentType = $restore;
$this->_current = $current;
if ($options['callbacks']) {
$this->getEventManager()->dispatch(new CakeEvent('View.afterRender', $this, array($file, $element)));
}
$this->_currentType = $restore;
$this->_current = $current;
if (isset($options['cache'])) {
Cache::write($this->elementCacheSettings['key'], $element, $this->elementCacheSettings['config']);
}