Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring View::element()

  • Loading branch information...
commit ca9ff0283ce6d994cc4fafaeda4a6b104d29c5a9 1 parent 90ae2df
@burzum burzum authored
Showing with 69 additions and 40 deletions.
  1. +69 −40 lib/Cake/View/View.php
View
109 lib/Cake/View/View.php
@@ -220,6 +220,14 @@ class View extends Object {
public $elementCache = 'default';
/**
+ * Element cache settings
+ *
+ * @see View::_elementCache();
+ * @see View::_renderElement
+ */
+ public $elementCacheSettings = array();
+
+/**
* List of variables to collect from the associated controller.
*
* @var array
@@ -375,62 +383,28 @@ public function getEventManager() {
* `Plugin.element_name` instead.
*/
public function element($name, $data = array(), $options = array()) {
- $file = $plugin = $key = null;
- $callbacks = false;
+ $file = $plugin = null;
if (isset($options['plugin'])) {
$name = Inflector::camelize($options['plugin']) . '.' . $name;
}
- if (isset($options['callbacks'])) {
- $callbacks = $options['callbacks'];
+ if (!isset($options['callbacks'])) {
+ $options['callbacks'] = false;
}
if (isset($options['cache'])) {
- $underscored = null;
- 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']);
+ $contents = $this->_elementCache($name, $plugin, $options);
if ($contents !== false) {
return $contents;
}
}
$file = $this->_getElementFilename($name);
-
if ($file) {
- if (!$this->_helpersLoaded) {
- $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;
+ return $this->_renderElement($file, $data, $options);
}
+
$file = 'Elements' . DS . $name . $this->ext;
if (Configure::read('debug') > 0) {
@@ -1126,4 +1100,59 @@ protected function _paths($plugin = null, $cached = true) {
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, $plugin, $options) {
+ $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']);
+ }
+ $key = 'element_' . $this->elementCacheSettings['key'];
+ return = Cache::read($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 ($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($key, $element, $this->elementCacheSettings['config']);
+ }
+ return $element;
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.