Merge pull request #706 from burzum/2.3-element-refactor

2.3 element refactor
This commit is contained in:
José Lorenzo Rodríguez 2012-07-05 17:49:16 -07:00
commit 1758dd14c4

View file

@ -219,6 +219,14 @@ class View extends Object {
*/ */
public $elementCache = 'default'; public $elementCache = 'default';
/**
* Element cache settings
*
* @see View::_elementCache();
* @see View::_renderElement
*/
public $elementCacheSettings = array();
/** /**
* List of variables to collect from the associated controller. * List of variables to collect from the associated controller.
* *
@ -375,62 +383,28 @@ class View extends Object {
* `Plugin.element_name` instead. * `Plugin.element_name` instead.
*/ */
public function element($name, $data = array(), $options = array()) { public function element($name, $data = array(), $options = array()) {
$file = $plugin = $key = null; $file = $plugin = null;
$callbacks = false;
if (isset($options['plugin'])) { if (isset($options['plugin'])) {
$name = Inflector::camelize($options['plugin']) . '.' . $name; $name = Inflector::camelize($options['plugin']) . '.' . $name;
} }
if (isset($options['callbacks'])) { if (!isset($options['callbacks'])) {
$callbacks = $options['callbacks']; $options['callbacks'] = false;
} }
if (isset($options['cache'])) { if (isset($options['cache'])) {
$underscored = null; $contents = $this->_elementCache($name, $data, $options);
if ($plugin) {
$underscored = Inflector::underscore($plugin);
}
$keys = array_merge(array($underscored, $name), array_keys($options), array_keys($data));
$caching = array(
'config' => $this->elementCache,
'key' => implode('_', $keys)
);
if (is_array($options['cache'])) {
$defaults = array(
'config' => $this->elementCache,
'key' => $caching['key']
);
$caching = array_merge($defaults, $options['cache']);
}
$key = 'element_' . $caching['key'];
$contents = Cache::read($key, $caching['config']);
if ($contents !== false) { if ($contents !== false) {
return $contents; return $contents;
} }
} }
$file = $this->_getElementFilename($name); $file = $this->_getElementFilename($name);
if ($file) { if ($file) {
if (!$this->_helpersLoaded) { return $this->_renderElement($file, $data, $options);
$this->loadHelpers();
}
if ($callbacks) {
$this->getEventManager()->dispatch(new CakeEvent('View.beforeRender', $this, array($file)));
} }
$this->_currentType = self::TYPE_ELEMENT;
$element = $this->_render($file, array_merge($this->viewVars, $data));
if ($callbacks) {
$this->getEventManager()->dispatch(new CakeEvent('View.afterRender', $this, array($file, $element)));
}
if (isset($options['cache'])) {
Cache::write($key, $element, $caching['config']);
}
return $element;
}
$file = 'Elements' . DS . $name . $this->ext; $file = 'Elements' . DS . $name . $this->ext;
if (Configure::read('debug') > 0) { if (Configure::read('debug') > 0) {
@ -1126,4 +1100,62 @@ class View extends Object {
return $this->_paths = $paths; return $this->_paths = $paths;
} }
/**
* Checks if an element is cached and returns the cached data if present
*
* @param string $name Element name
* @param string $plugin Plugin name
* @param array $options Element options
*/
protected function _elementCache($name, $data, $options) {
$plugin = null;
list($plugin, $name) = $this->pluginSplit($name);
$underscored = null;
if ($plugin) {
$underscored = Inflector::underscore($plugin);
}
$keys = array_merge(array($underscored, $name), array_keys($options), array_keys($data));
$this->elementCacheSettings = array(
'config' => $this->elementCache,
'key' => implode('_', $keys)
);
if (is_array($options['cache'])) {
$defaults = array(
'config' => $this->elementCache,
'key' => $this->elementCacheSettings['key']
);
$this->elementCacheSettings = array_merge($defaults, $options['cache']);
}
$this->elementCacheSettings['key'] = 'element_' . $this->elementCacheSettings['key'];
return Cache::read($this->elementCacheSettings['key'], $this->elementCacheSettings['config']);
}
/**
* Renders an element and fires the before and afterRender callbacks for it
* and writes to the cache if a cache is used
*
* @param string $file Element file path
* @param array $data Data to render
* @param array $options Element options
*/
protected function _renderElement($file, $data, $options) {
if (!$this->_helpersLoaded) {
$this->loadHelpers();
}
if ($options['callbacks']) {
$this->getEventManager()->dispatch(new CakeEvent('View.beforeRender', $this, array($file)));
}
$this->_currentType = self::TYPE_ELEMENT;
$element = $this->_render($file, array_merge($this->viewVars, $data));
if (isset($options['callbacks'])) {
$this->getEventManager()->dispatch(new CakeEvent('View.afterRender', $this, array($file, $element)));
}
if (isset($options['cache'])) {
Cache::write($this->elementCacheSettings['key'], $element, $this->elementCacheSettings['config']);
}
return $element;
}
} }