Skip to content
This repository
Browse code

Refactoring View::element()

  • Loading branch information...
commit ca9ff0283ce6d994cc4fafaeda4a6b104d29c5a9 1 parent 90ae2df
Florian Krämer authored July 04, 2012

Showing 1 changed file with 69 additions and 40 deletions. Show diff stats Hide diff stats

  1. 109  lib/Cake/View/View.php
109  lib/Cake/View/View.php
@@ -220,6 +220,14 @@ class View extends Object {
220 220
 	public $elementCache = 'default';
221 221
 
222 222
 /**
  223
+ * Element cache settings
  224
+ *
  225
+ * @see View::_elementCache();
  226
+ * @see View::_renderElement
  227
+ */
  228
+	public $elementCacheSettings = array();
  229
+
  230
+/**
223 231
  * List of variables to collect from the associated controller.
224 232
  *
225 233
  * @var array
@@ -375,62 +383,28 @@ public function getEventManager() {
375 383
  *   `Plugin.element_name` instead.
376 384
  */
377 385
 	public function element($name, $data = array(), $options = array()) {
378  
-		$file = $plugin = $key = null;
379  
-		$callbacks = false;
  386
+		$file = $plugin = null;
380 387
 
381 388
 		if (isset($options['plugin'])) {
382 389
 			$name = Inflector::camelize($options['plugin']) . '.' . $name;
383 390
 		}
384 391
 
385  
-		if (isset($options['callbacks'])) {
386  
-			$callbacks = $options['callbacks'];
  392
+		if (!isset($options['callbacks'])) {
  393
+			$options['callbacks'] = false;
387 394
 		}
388 395
 
389 396
 		if (isset($options['cache'])) {
390  
-			$underscored = null;
391  
-			if ($plugin) {
392  
-				$underscored = Inflector::underscore($plugin);
393  
-			}
394  
-			$keys = array_merge(array($underscored, $name), array_keys($options), array_keys($data));
395  
-			$caching = array(
396  
-				'config' => $this->elementCache,
397  
-				'key' => implode('_', $keys)
398  
-			);
399  
-			if (is_array($options['cache'])) {
400  
-				$defaults = array(
401  
-					'config' => $this->elementCache,
402  
-					'key' => $caching['key']
403  
-				);
404  
-				$caching = array_merge($defaults, $options['cache']);
405  
-			}
406  
-			$key = 'element_' . $caching['key'];
407  
-			$contents = Cache::read($key, $caching['config']);
  397
+			$contents = $this->_elementCache($name, $plugin, $options);
408 398
 			if ($contents !== false) {
409 399
 				return $contents;
410 400
 			}
411 401
 		}
412 402
 
413 403
 		$file = $this->_getElementFilename($name);
414  
-
415 404
 		if ($file) {
416  
-			if (!$this->_helpersLoaded) {
417  
-				$this->loadHelpers();
418  
-			}
419  
-			if ($callbacks) {
420  
-				$this->getEventManager()->dispatch(new CakeEvent('View.beforeRender', $this, array($file)));
421  
-			}
422  
-
423  
-			$this->_currentType = self::TYPE_ELEMENT;
424  
-			$element = $this->_render($file, array_merge($this->viewVars, $data));
425  
-
426  
-			if ($callbacks) {
427  
-				$this->getEventManager()->dispatch(new CakeEvent('View.afterRender', $this, array($file, $element)));
428  
-			}
429  
-			if (isset($options['cache'])) {
430  
-				Cache::write($key, $element, $caching['config']);
431  
-			}
432  
-			return $element;
  405
+			return $this->_renderElement($file, $data, $options);
433 406
 		}
  407
+
434 408
 		$file = 'Elements' . DS . $name . $this->ext;
435 409
 
436 410
 		if (Configure::read('debug') > 0) {
@@ -1126,4 +1100,59 @@ protected function _paths($plugin = null, $cached = true) {
1126 1100
 		return $this->_paths = $paths;
1127 1101
 	}
1128 1102
 
  1103
+/**
  1104
+ * Checks if an element is cached and returns the cached data if present
  1105
+ *
  1106
+ * @param string $name Element name
  1107
+ * @param string $plugin Plugin name
  1108
+ * @param array $options Element options
  1109
+ */
  1110
+	protected function _elementCache($name, $plugin, $options) {
  1111
+		$underscored = null;
  1112
+		if ($plugin) {
  1113
+			$underscored = Inflector::underscore($plugin);
  1114
+		}
  1115
+		$keys = array_merge(array($underscored, $name), array_keys($options), array_keys($data));
  1116
+		$this->elementCacheSettings = array(
  1117
+			'config' => $this->elementCache,
  1118
+			'key' => implode('_', $keys)
  1119
+		);
  1120
+		if (is_array($options['cache'])) {
  1121
+			$defaults = array(
  1122
+				'config' => $this->elementCache,
  1123
+				'key' => $this->elementCacheSettings['key']
  1124
+			);
  1125
+			$this->elementCacheSettings = array_merge($defaults, $options['cache']);
  1126
+		}
  1127
+		$key = 'element_' . $this->elementCacheSettings['key'];
  1128
+		return = Cache::read($key, $this->elementCacheSettings['config']);
  1129
+	}
  1130
+
  1131
+/**
  1132
+ * Renders an element and fires the before and afterRender callbacks for it
  1133
+ * and writes to the cache if a cache is used
  1134
+ *
  1135
+ * @param string $file Element file path
  1136
+ * @param array $data Data to render
  1137
+ * @param array $options Element options
  1138
+ */
  1139
+	protected function _renderElement($file, $data, $options) {
  1140
+		if (!$this->_helpersLoaded) {
  1141
+			$this->loadHelpers();
  1142
+		}
  1143
+		if ($callbacks) {
  1144
+			$this->getEventManager()->dispatch(new CakeEvent('View.beforeRender', $this, array($file)));
  1145
+		}
  1146
+
  1147
+		$this->_currentType = self::TYPE_ELEMENT;
  1148
+		$element = $this->_render($file, array_merge($this->viewVars, $data));
  1149
+
  1150
+		if (isset($options['callbacks'])) {
  1151
+			$this->getEventManager()->dispatch(new CakeEvent('View.afterRender', $this, array($file, $element)));
  1152
+		}
  1153
+		if (isset($options['cache'])) {
  1154
+			Cache::write($key, $element, $this->elementCacheSettings['config']);
  1155
+		}
  1156
+		return $element;
  1157
+	}
1129 1158
 }

0 notes on commit ca9ff02

Please sign in to comment.
Something went wrong with that request. Please try again.