mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
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:
parent
dfefc2d97b
commit
1cf5e72054
2 changed files with 62 additions and 67 deletions
|
@ -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
|
||||
*
|
||||
|
@ -300,10 +310,10 @@ class View extends Object {
|
|||
*
|
||||
* ### 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
|
||||
|
@ -319,7 +329,6 @@ class View extends Object {
|
|||
if (isset($params['plugin'])) {
|
||||
$plugin = $params['plugin'];
|
||||
}
|
||||
|
||||
if (isset($this->plugin) && !$plugin) {
|
||||
$plugin = $this->plugin;
|
||||
}
|
||||
|
@ -327,32 +336,26 @@ class View extends Object {
|
|||
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);
|
||||
$file = $this->_getElementFilename($name, $plugin);
|
||||
|
||||
foreach ($paths as $path) {
|
||||
if (file_exists($path . 'elements' . DS . $name . $this->ext)) {
|
||||
$file = $path . 'elements' . DS . $name . $this->ext;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_file($file)) {
|
||||
if ($file) {
|
||||
if (!$this->_helpersLoaded) {
|
||||
$this->loadHelpers();
|
||||
}
|
||||
|
@ -368,10 +371,10 @@ class View extends Object {
|
|||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -778,6 +781,23 @@ class View extends Object {
|
|||
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
|
||||
*
|
||||
|
|
|
@ -515,61 +515,36 @@ class ViewTest extends CakeTestCase {
|
|||
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);
|
||||
$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'=>'+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);
|
||||
*/
|
||||
Cache::drop('test_view');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue