Skip to content

Commit

Permalink
Removing duplicated calls to slug()
Browse files Browse the repository at this point in the history
Extracting getElementFilename() into a method, this should allow developers to more easily replace how elements are found.
Refs #1268
  • Loading branch information
markstory committed Nov 10, 2010
1 parent dfefc2d commit 1cf5e72
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 67 deletions.
62 changes: 41 additions & 21 deletions cake/libs/view/view.php
Expand Up @@ -240,6 +240,16 @@ class View extends Object {
*/
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
*
Expand Down Expand Up @@ -300,10 +310,10 @@ function __construct(&$controller) {
*
* ### Special params
*
* - `cache` - enable caching for this element accepts boolean or strtotime compatible string.
* Can also be an array. If `cache` is an array,
* `time` is used to specify duration of cache.
* `key` can be used to create unique cache files.
* - `cache` - Can either be `true`, to enable caching using the config in View::$elementCache. Or an array
* If an array, the following keys can be used:
* - `config` - Used to store the cached element in a custom cache configuration.
* - `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.
*
* @param string $name Name of template file in the/app/views/elements/ folder
Expand All @@ -319,40 +329,33 @@ public function element($name, $params = array(), $callbacks = false) {
if (isset($params['plugin'])) {
$plugin = $params['plugin'];
}

if (isset($this->plugin) && !$plugin) {
$plugin = $this->plugin;
}

if (isset($params['cache'])) {
if (is_array($params['cache'])) {
$defaults = array(
'config' => 'default',
'key' => '',
'config' => $this->elementCache,
'key' => $plugin . '_' . $name,
);
$caching = array_merge($defaults, $params['cache']);
} else {
$keys = array_merge(array($plugin, $name), array_keys($params));
$caching = array(
'config' => 'default',
'key' => implode('_', array_keys($params))
'config' => $this->elementCache,
'key' => implode('_', $keys)
);
}
$key = 'element_' . $caching['key'] . '_' . $plugin . Inflector::slug($name);
$key = 'element_' . $caching['key'];
$contents = Cache::read($key, $caching['config']);
if ($contents !== false) {
return $contents;
}
}
$paths = $this->_paths($plugin);

foreach ($paths as $path) {
if (file_exists($path . 'elements' . DS . $name . $this->ext)) {
$file = $path . 'elements' . DS . $name . $this->ext;
break;
}
}
$file = $this->_getElementFilename($name, $plugin);

if (is_file($file)) {
if ($file) {
if (!$this->_helpersLoaded) {
$this->loadHelpers();
}
Expand All @@ -368,10 +371,10 @@ public function element($name, $params = array(), $callbacks = false) {
}
return $element;
}
$file = $paths[0] . 'elements' . DS . $name . $this->ext;
$file = 'elements' . DS . $name . $this->ext;

if (Configure::read('debug') > 0) {
return "Not Found: " . $file;
return "Element Not Found: " . $file;
}
}

Expand Down Expand Up @@ -778,6 +781,23 @@ protected function _getLayoutFileName($name = null) {
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
*
Expand Down
67 changes: 21 additions & 46 deletions cake/tests/cases/libs/view/view.test.php
Expand Up @@ -515,61 +515,36 @@ function testElementCache() {
Cache::config('test_view', array(
'engine' => 'File',
'duration' => '+1 day',
'path' => CACHE . 'views' . DS
'path' => CACHE . 'views' . DS,
'prefix' => ''
));
Cache::clear('test_view');

$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';
$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);

/*
$writable = is_writable(CACHE . 'views' . DS);
if ($this->skipIf(!$writable, 'CACHE/views dir is not writable, cannot test elementCache. %s')) {
return;
}
$View = new TestView($this->PostsController);
$element = 'test_element';
$expected = 'this is the test element';
$result = $View->element($element);
$this->assertEqual($result, $expected);
$cached = false;
$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);
$result = Cache::read('element_test_element_cache_param_foo', 'test_view');
$this->assertEquals($expected, $result);

$result = $View->element('test_element', array(
'cache' => array('key' => 'custom_key'),
'param' => 'one',
'foo' => 'two'
));
$result = Cache::read('element_custom_key', 'test_view');
$this->assertEquals($expected, $result);

$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);
*/
Cache::drop('test_view');
}

/**
Expand Down

0 comments on commit 1cf5e72

Please sign in to comment.