Removing duplicated calls to slug()

Extracting getElementFilename() into a method, this should allow developers to more easily replace how elements are found.
Refs #1268
This commit is contained in:
mark_story 2010-11-09 22:16:45 -05:00
parent dfefc2d97b
commit 1cf5e72054
2 changed files with 62 additions and 67 deletions

View file

@ -240,6 +240,16 @@ class View extends Object {
*/ */
public $request; public $request;
/**
* The Cache configuration View will use to store cached elements. Changing this will change
* the default configuration elements are stored under. You can also choose a cache config
* per element.
*
* @var string
* @see View::element()
*/
public $elementCache = 'default';
/** /**
* List of variables to collect from the associated controller * List of variables to collect from the associated controller
* *
@ -300,10 +310,10 @@ class View extends Object {
* *
* ### Special params * ### Special params
* *
* - `cache` - enable caching for this element accepts boolean or strtotime compatible string. * - `cache` - Can either be `true`, to enable caching using the config in View::$elementCache. Or an array
* Can also be an array. If `cache` is an array, * If an array, the following keys can be used:
* `time` is used to specify duration of cache. * - `config` - Used to store the cached element in a custom cache configuration.
* `key` can be used to create unique cache files. * - `key` - Used to define the key used in the Cache::write(). It will be prefixed with `element_`
* - `plugin` - Load an element from a specific plugin. * - `plugin` - Load an element from a specific plugin.
* *
* @param string $name Name of template file in the/app/views/elements/ folder * @param string $name Name of template file in the/app/views/elements/ folder
@ -319,7 +329,6 @@ class View extends Object {
if (isset($params['plugin'])) { if (isset($params['plugin'])) {
$plugin = $params['plugin']; $plugin = $params['plugin'];
} }
if (isset($this->plugin) && !$plugin) { if (isset($this->plugin) && !$plugin) {
$plugin = $this->plugin; $plugin = $this->plugin;
} }
@ -327,32 +336,26 @@ class View extends Object {
if (isset($params['cache'])) { if (isset($params['cache'])) {
if (is_array($params['cache'])) { if (is_array($params['cache'])) {
$defaults = array( $defaults = array(
'config' => 'default', 'config' => $this->elementCache,
'key' => '', 'key' => $plugin . '_' . $name,
); );
$caching = array_merge($defaults, $params['cache']); $caching = array_merge($defaults, $params['cache']);
} else { } else {
$keys = array_merge(array($plugin, $name), array_keys($params));
$caching = array( $caching = array(
'config' => 'default', 'config' => $this->elementCache,
'key' => implode('_', array_keys($params)) 'key' => implode('_', $keys)
); );
} }
$key = 'element_' . $caching['key'] . '_' . $plugin . Inflector::slug($name); $key = 'element_' . $caching['key'];
$contents = Cache::read($key, $caching['config']); $contents = Cache::read($key, $caching['config']);
if ($contents !== false) { if ($contents !== false) {
return $contents; return $contents;
} }
} }
$paths = $this->_paths($plugin); $file = $this->_getElementFilename($name, $plugin);
foreach ($paths as $path) { if ($file) {
if (file_exists($path . 'elements' . DS . $name . $this->ext)) {
$file = $path . 'elements' . DS . $name . $this->ext;
break;
}
}
if (is_file($file)) {
if (!$this->_helpersLoaded) { if (!$this->_helpersLoaded) {
$this->loadHelpers(); $this->loadHelpers();
} }
@ -368,10 +371,10 @@ class View extends Object {
} }
return $element; return $element;
} }
$file = $paths[0] . 'elements' . DS . $name . $this->ext; $file = 'elements' . DS . $name . $this->ext;
if (Configure::read('debug') > 0) { if (Configure::read('debug') > 0) {
return "Not Found: " . $file; return "Element Not Found: " . $file;
} }
} }
@ -778,6 +781,23 @@ class View extends Object {
throw new MissingLayoutException(array('file' => $paths[0] . $file . $this->ext)); throw new MissingLayoutException(array('file' => $paths[0] . $file . $this->ext));
} }
/**
* Finds an element filename, returns false on failure.
*
* @param string $name The name of the element to find.
* @param string $plugin The plugin name the element is in.
* @return mixed Either a string to the element filename or false when one can't be found.
*/
protected function _getElementFileName($name, $plugin = null) {
$paths = $this->_paths($plugin);
foreach ($paths as $path) {
if (file_exists($path . 'elements' . DS . $name . $this->ext)) {
return $path . 'elements' . DS . $name . $this->ext;
}
}
return false;
}
/** /**
* Return all possible paths to find view files in order * Return all possible paths to find view files in order
* *

View file

@ -515,61 +515,36 @@ class ViewTest extends CakeTestCase {
Cache::config('test_view', array( Cache::config('test_view', array(
'engine' => 'File', 'engine' => 'File',
'duration' => '+1 day', 'duration' => '+1 day',
'path' => CACHE . 'views' . DS 'path' => CACHE . 'views' . DS,
'prefix' => ''
)); ));
Cache::clear('test_view');
$View = new TestView($this->PostsController); $View = new TestView($this->PostsController);
$result = $View->element('test_element', array('cache' => array('config' => 'test_view'))); $View->elementCache = 'test_view';
$result = $View->element('test_element', array('cache' => true));
$expected = 'this is the test element'; $expected = 'this is the test element';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = Cache::read('element__test_element', 'test_view'); $result = Cache::read('element_test_element_cache', 'test_view');
$this->assertEquals($expected, $result);
$result = $View->element('test_element', array('cache' => true, 'param' => 'one', 'foo' => 'two'));
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
/* $result = Cache::read('element_test_element_cache_param_foo', 'test_view');
$writable = is_writable(CACHE . 'views' . DS); $this->assertEquals($expected, $result);
if ($this->skipIf(!$writable, 'CACHE/views dir is not writable, cannot test elementCache. %s')) {
return; $result = $View->element('test_element', array(
} 'cache' => array('key' => 'custom_key'),
$View = new TestView($this->PostsController); 'param' => 'one',
$element = 'test_element'; 'foo' => 'two'
$expected = 'this is the test element'; ));
$result = $View->element($element); $result = Cache::read('element_custom_key', 'test_view');
$this->assertEqual($result, $expected); $this->assertEquals($expected, $result);
$cached = false; Cache::drop('test_view');
$result = $View->element($element, array('cache'=>'+1 second'));
if (file_exists(CACHE . 'views' . DS . 'element_cache_'.$element)) {
$cached = true;
unlink(CACHE . 'views' . DS . 'element_cache_'.$element);
}
$this->assertTrue($cached);
$cached = false;
$result = $View->element($element, array('cache'=>'+1 second', 'other_param'=> true, 'anotherParam'=> true));
if (file_exists(CACHE . 'views' . DS . 'element_cache_other_param_anotherParam_'.$element)) {
$cached = true;
unlink(CACHE . 'views' . DS . 'element_cache_other_param_anotherParam_'.$element);
}
$this->assertTrue($cached);
$cached = false;
$result = $View->element($element, array('cache'=>array('time'=>'+1 second', 'key'=>'/whatever/here')));
if (file_exists(CACHE . 'views' . DS . 'element_'.Inflector::slug('/whatever/here').'_'.$element)) {
$cached = true;
unlink(CACHE . 'views' . DS . 'element_'.Inflector::slug('/whatever/here').'_'.$element);
}
$this->assertTrue($cached);
$cached = false;
$result = $View->element($element, array('cache'=>array('time'=>'+1 second', 'key'=>'whatever_here')));
if (file_exists(CACHE . 'views' . DS . 'element_whatever_here_'.$element)) {
$cached = true;
unlink(CACHE . 'views' . DS . 'element_whatever_here_'.$element);
}
$this->assertTrue($cached);
$this->assertEqual($result, $expected);
*/
} }
/** /**