Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Starting refactoring of cachehelper to use Router::reverse to generat…

…e check/match strings. Removing dead code as well.
  • Loading branch information...
commit 45a3eb250f6323123ec0718d18447ac5713dc8a3 1 parent df8914b
@markstory markstory authored
View
36 cake/libs/view/helpers/cache.php
@@ -67,25 +67,10 @@ function cache($file, $out, $cache = false) {
$cacheTime = 0;
$useCallbacks = false;
if (is_array($this->cacheAction)) {
- $controller = Inflector::underscore($this->controllerName);
- $controllerAlternate = Inflector::variable($this->controllerName);
-
- $check = str_replace('/', '_', $this->here);
- $basePath = str_replace('/', '_', $this->base);
-
- $match = str_replace($this->base, '', $this->here);
- $match = str_replace('//', '/', $match);
- $match = str_replace('/' . $controller . '/', '', $match);
- $match = str_replace('/' . $controllerAlternate . '/', '', $match);
- $match = str_replace('/' . $this->controllerName . '/', '', $match);
-
- $check = str_replace($basePath, '', $check);
- $check = str_replace('_' . $controller . '_', '', $check);
- $check = str_replace('_' . $this->controllerName . '_', '', $check);
- $check = str_replace('_' . $controllerAlternate . '_', '', $match);
-
- $check = Inflector::slug($check);
- $check = trim($check, '_');
+ $check = Inflector::slug(Router::reverse($this->params));
+ $base = trim(str_replace('/', '_', $this->base), '_');
+ $check = trim(str_replace($base, '', $check), '_');
+ $match = $check;
$keys = str_replace('/', '_', array_keys($this->cacheAction));
$found = array_keys($this->cacheAction);
@@ -93,23 +78,14 @@ function cache($file, $out, $cache = false) {
$count = 0;
foreach ($keys as $key => $value) {
- if (strpos($check, rtrim($value, '_')) === 0) {
+ if (strpos($check, rtrim($value, '_')) !== false) {
$index = $found[$count];
break;
}
$count++;
}
- if (isset($index)) {
- $pos1 = strrpos($match, '/');
- $char = strlen($match) - 1;
-
- if ($pos1 == $char) {
- $match = substr($match, 0, $char);
- }
-
- $key = $match;
- } elseif ($this->action == 'index') {
+ if (!isset($index) && $this->action == 'index') {
$index = 'index';
}
View
69 cake/tests/cases/libs/view/helpers/cache.test.php
@@ -127,6 +127,13 @@ function tearDown() {
*/
function testLayoutCacheParsingNoTagsInView() {
$this->Controller->cache_parsing();
+ $this->Controller->params = array(
+ 'controller' => 'cache_test',
+ 'action' => 'cache_parsing',
+ 'url' => array(),
+ 'pass' => array(),
+ 'named' => array()
+ );
$this->Controller->cacheAction = 21600;
$this->Controller->here = '/cacheTest/cache_parsing';
$this->Controller->action = 'cache_parsing';
@@ -155,6 +162,13 @@ function testLayoutCacheParsingNoTagsInView() {
*/
function testCacheNonLatinCharactersInRoute() {
$this->Controller->cache_parsing();
+ $this->Controller->params = array(
+ 'controller' => 'cache_test',
+ 'action' => 'cache_parsing',
+ 'url' => array(),
+ 'pass' => array('風街ろまん'),
+ 'named' => array()
+ );
$this->Controller->cacheAction = 21600;
$this->Controller->here = '/posts/view/風街ろまん';
$this->Controller->action = 'view';
@@ -175,6 +189,13 @@ function testCacheNonLatinCharactersInRoute() {
*/
function testLayoutCacheParsingWithTagsInView() {
$this->Controller->cache_parsing();
+ $this->Controller->params = array(
+ 'controller' => 'cache_test',
+ 'action' => 'cache_parsing',
+ 'url' => array(),
+ 'pass' => array(),
+ 'named' => array()
+ );
$this->Controller->cacheAction = 21600;
$this->Controller->here = '/cacheTest/cache_parsing';
$this->Controller->action = 'cache_parsing';
@@ -203,6 +224,13 @@ function testLayoutCacheParsingWithTagsInView() {
*/
function testMultipleNoCacheTagsInViewfile() {
$this->Controller->cache_parsing();
+ $this->Controller->params = array(
+ 'controller' => 'cache_test',
+ 'action' => 'cache_parsing',
+ 'url' => array(),
+ 'pass' => array(),
+ 'named' => array()
+ );
$this->Controller->cacheAction = 21600;
$this->Controller->here = '/cacheTest/cache_parsing';
$this->Controller->action = 'cache_parsing';
@@ -229,6 +257,13 @@ function testMultipleNoCacheTagsInViewfile() {
*/
function testComplexNoCache () {
$this->Controller->cache_parsing();
+ $this->Controller->params = array(
+ 'controller' => 'cache_test',
+ 'action' => 'cache_complex',
+ 'url' => array(),
+ 'pass' => array(),
+ 'named' => array()
+ );
$this->Controller->cacheAction = array('cache_complex' => 21600);
$this->Controller->here = '/cacheTest/cache_complex';
$this->Controller->action = 'cache_complex';
@@ -283,6 +318,13 @@ function testComplexNoCache () {
*/
function testCacheActionArray() {
$this->Controller->cache_parsing();
+ $this->Controller->params = array(
+ 'controller' => 'cache_test',
+ 'action' => 'cache_parsing',
+ 'url' => array(),
+ 'pass' => array(),
+ 'named' => array()
+ );
$this->Controller->cacheAction = array(
'cache_parsing' => 21600
);
@@ -319,6 +361,13 @@ function testCacheActionArray() {
$this->Controller->cache_parsing();
+ $this->Controller->params = array(
+ 'controller' => 'cache_test',
+ 'action' => 'cache_parsing',
+ 'url' => array(),
+ 'pass' => array(33),
+ 'named' => array()
+ );
$this->Controller->cacheAction = array(
'cache_parsing/33' => 21600
);
@@ -334,8 +383,15 @@ function testCacheActionArray() {
$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing_33.php';
$this->assertTrue(file_exists($filename));
@unlink($filename);
-
+
$this->Controller->cache_parsing();
+ $this->Controller->params = array(
+ 'controller' => 'cache_test',
+ 'action' => 'cache_parsing',
+ 'url' => array(),
+ 'pass' => array(),
+ 'named' => array()
+ );
$this->Controller->cacheAction = array(
'cache_parsing/33' => 21600
);
@@ -351,6 +407,17 @@ function testCacheActionArray() {
$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
$this->assertFalse(file_exists($filename));
}
+
+/**
+ * test that custom routes are respected when generating cache files.
+ *
+ * @return void
+ */
+ function testCacheWithCustomRoutes() {
+ Router::reload();
+ Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));
+
+ }
/**
* testCacheEmptySections method
*
Please sign in to comment.
Something went wrong with that request. Please try again.