From 7891143cd959645569c278f3b1a8e7c9cabb14c8 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 2 Mar 2011 05:46:27 -0500 Subject: [PATCH 1/6] Removing repetitive strtoupper call. --- cake/libs/route/cake_route.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cake/libs/route/cake_route.php b/cake/libs/route/cake_route.php index 798d26c92..ca96428ba 100644 --- a/cake/libs/route/cake_route.php +++ b/cake/libs/route/cake_route.php @@ -193,12 +193,13 @@ class CakeRoute { } else { $header = 'http_' . $header[1]; } + $header = strtoupper($header); $val = (array)$val; $h = false; foreach ($val as $v) { - if (env(strtoupper($header)) === $v) { + if (env($header) === $v) { $h = true; } } From 7526d017f0764522a3bcdbdfe85f29d15516bbc4 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 2 Mar 2011 21:00:53 -0500 Subject: [PATCH 2/6] Clarifying what tld means. --- cake/libs/cake_request.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cake/libs/cake_request.php b/cake/libs/cake_request.php index 90d42826f..d51e808d5 100644 --- a/cake/libs/cake_request.php +++ b/cake/libs/cake_request.php @@ -532,7 +532,8 @@ class CakeRequest implements ArrayAccess { /** * Get the domain name and include $tldLength segments of the tld. * - * @param int $tldLength Number of segments your tld contains + * @param int $tldLength Number of segments your tld contains. For example: `example.com` contains 1 tld. + * While `example.co.uk` contains 2. * @return string Domain name without subdomains. */ public function domain($tldLength = 1) { @@ -544,7 +545,8 @@ class CakeRequest implements ArrayAccess { /** * Get the subdomains for a host. * - * @param int $tldLength Number of segments your tld contains. + * @param int $tldLength Number of segments your tld contains. For example: `example.com` contains 1 tld. + * While `example.co.uk` contains 2. * @return array of subdomains. */ public function subdomains($tldLength = 1) { From 2145fd8078d395e71ff12b54ac36c155719bed70 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 2 Mar 2011 21:46:28 -0500 Subject: [PATCH 3/6] Adding CakeRequest::here() for getting complete 'here' values including query string parameters. $request->here does not include query string params. Adding test cases. --- cake/libs/cake_request.php | 17 +++++++++++++++ cake/tests/cases/libs/cake_request.test.php | 24 +++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/cake/libs/cake_request.php b/cake/libs/cake_request.php index d51e808d5..90435ed8b 100644 --- a/cake/libs/cake_request.php +++ b/cake/libs/cake_request.php @@ -497,6 +497,23 @@ class CakeRequest implements ArrayAccess { return $this; } +/** + * Get the value of the current requests url. Will include named parameters and querystring arguments. + * + * @param boolean $base Include the base path, set to false to trim the base path off. + * @return string the current request url including query string args. + */ + public function here($base = true) { + $url = $this->here; + if (!empty($this->query)) { + $url .= '?' . http_build_query($this->query); + } + if (!$base) { + $url = preg_replace('/^' . preg_quote($this->base, '/') . '/', '', $url, 1); + } + return $url; + } + /** * Read an HTTP header from the Request information. * diff --git a/cake/tests/cases/libs/cake_request.test.php b/cake/tests/cases/libs/cake_request.test.php index a28838a14..3df9a8713 100644 --- a/cake/tests/cases/libs/cake_request.test.php +++ b/cake/tests/cases/libs/cake_request.test.php @@ -1401,6 +1401,30 @@ class CakeRequestTestCase extends CakeTestCase { $this->assertFalse($result); } +/** + * test the here() method + * + * @return void + */ + function testHere() { + Configure::write('App.base', '/base_path'); + $_GET = array('test' => 'value'); + $request = new CakeRequest('/posts/add/1/name:value'); + + $result = $request->here(); + $this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result); + + $result = $request->here(false); + $this->assertEquals('/posts/add/1/name:value?test=value', $result); + + $request = new CakeRequest('/posts/base_path/1/name:value'); + $result = $request->here(); + $this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result); + + $result = $request->here(false); + $this->assertEquals('/posts/base_path/1/name:value?test=value', $result); + } + /** * loadEnvironment method * From af16b13e5f70b05b5d761c58a70d56990ff357dd Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 2 Mar 2011 21:47:11 -0500 Subject: [PATCH 4/6] Fixing issues with double base dir in FormHelper::create(). Fixes #1568 --- cake/libs/view/helpers/form.php | 2 +- .../cases/libs/view/helpers/form.test.php | 25 +++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 54cadd400..7b0b29b93 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -239,7 +239,7 @@ class FormHelper extends AppHelper { $this->_inputDefaults = $options['inputDefaults']; unset($options['inputDefaults']); if ($options['action'] === null && $options['url'] === null) { - $options['action'] = $this->request->here; + $options['action'] = $this->request->here(false); if (!isset($options['id'])) { $options['id'] = $this->domId($this->request['action'] . 'Form'); } diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index a94246ad5..532476960 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -662,12 +662,13 @@ class FormHelperTest extends CakeTestCase { function setUp() { parent::setUp(); + Configure::write('App.base', ''); $this->Controller = new ContactTestController(); $this->View = new View($this->Controller); $this->Form = new FormHelper($this->View); $this->Form->Html = new HtmlHelper($this->View); - $this->Form->request = new CakeRequest(null, false); + $this->Form->request = new CakeRequest('contacts/add', false); $this->Form->request->here = '/contacts/add'; $this->Form->request['action'] = 'add'; $this->Form->request->webroot = ''; @@ -5664,12 +5665,21 @@ class FormHelperTest extends CakeTestCase { '/div' ); $this->assertTags($result, $expected); + } - $this->Form->request->here = '/contacts/add/Contact:1'; - $result = $this->Form->create(); +/** + * test create() with automatic url generation + * + * @return void + */ + function testCreateAutoUrl() { + Router::setRequestInfo(array(array(), array('base' => '/base_url'))); + $this->Form->request->here = '/base_url/contacts/add/Contact:1'; + $this->Form->request->base = '/base_url'; + $result = $this->Form->create('Contact'); $expected = array( 'form' => array( - 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add/Contact:1', + 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1', 'accept-charset' => 'utf-8' ), 'div' => array('style' => 'display:none;'), @@ -5679,11 +5689,12 @@ class FormHelperTest extends CakeTestCase { $this->assertTags($result, $expected); $this->Form->request['action'] = 'delete'; - $this->Form->request->here = '/contacts/delete/10/User:42'; - $result = $this->Form->create(); + $this->Form->request->here = '/base_url/contacts/delete/10/User:42'; + $this->Form->request->base = '/base_url'; + $result = $this->Form->create('Contact'); $expected = array( 'form' => array( - 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/contacts/delete/10/User:42', + 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42', 'accept-charset' => 'utf-8' ), 'div' => array('style' => 'display:none;'), From a6abd61f3f419116b93eb13b3c9b339c36dd1f62 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 2 Mar 2011 21:52:25 -0500 Subject: [PATCH 5/6] Removing Dispatcher::$here, it wasn't really used outside of a cached() which had a param that it just ignored. Making Dispatcher::cached() pay attention to its parameter. Updating tests. --- cake/libs/dispatcher.php | 19 ++++-------------- cake/tests/cases/libs/dispatcher.test.php | 24 +++++++++++------------ 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/cake/libs/dispatcher.php b/cake/libs/dispatcher.php index f8c833bc9..387eac89c 100644 --- a/cake/libs/dispatcher.php +++ b/cake/libs/dispatcher.php @@ -37,14 +37,6 @@ App::import('Controller', 'Controller', false); */ class Dispatcher { -/** - * Current URL - * - * @var string - * @access public - */ - public $here = false; - /** * The request object * @@ -91,9 +83,7 @@ class Dispatcher { * are encountered. */ public function dispatch(CakeRequest $request, $additionalParams = array()) { - $this->here = $request->here; - - if ($this->asset($request->url) || $this->cached($request->url)) { + if ($this->asset($request->url) || $this->cached($request->here)) { return; } @@ -260,12 +250,11 @@ class Dispatcher { /** * Outputs cached dispatch view cache * - * @param string $url Requested URL + * @param string $path Requested URL path */ - public function cached($url) { + public function cached($path) { if (Configure::read('Cache.check') === true) { - $path = $this->here; - if ($this->here == '/') { + if ($path == '/') { $path = 'home'; } $path = strtolower(Inflector::slug($path)); diff --git a/cake/tests/cases/libs/dispatcher.test.php b/cake/tests/cases/libs/dispatcher.test.php index 0f9a6bbbb..e7e3ae8b9 100644 --- a/cake/tests/cases/libs/dispatcher.test.php +++ b/cake/tests/cases/libs/dispatcher.test.php @@ -1424,7 +1424,7 @@ class DispatcherTest extends CakeTestCase { $out = ob_get_clean(); ob_start(); - $dispatcher->cached($request); + $dispatcher->cached($request->here); $cached = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", $out); @@ -1433,7 +1433,7 @@ class DispatcherTest extends CakeTestCase { $this->assertEqual($result, $expected); - $filename = $this->__cachePath($dispatcher->here); + $filename = $this->__cachePath($request->here); unlink($filename); $request = new CakeRequest('test_cached_pages/index'); @@ -1446,7 +1446,7 @@ class DispatcherTest extends CakeTestCase { $out = ob_get_clean(); ob_start(); - $dispatcher->cached($request); + $dispatcher->cached($request->here); $cached = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", $out); @@ -1454,7 +1454,7 @@ class DispatcherTest extends CakeTestCase { $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached); $this->assertEqual($result, $expected); - $filename = $this->__cachePath($dispatcher->here); + $filename = $this->__cachePath($request->here); unlink($filename); $request = new CakeRequest('TestCachedPages/index'); @@ -1464,7 +1464,7 @@ class DispatcherTest extends CakeTestCase { $out = ob_get_clean(); ob_start(); - $dispatcher->cached($request); + $dispatcher->cached($request->here); $cached = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", $out); @@ -1472,7 +1472,7 @@ class DispatcherTest extends CakeTestCase { $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached); $this->assertEqual($result, $expected); - $filename = $this->__cachePath($dispatcher->here); + $filename = $this->__cachePath($request->here); unlink($filename); $request = new CakeRequest('TestCachedPages/test_nocache_tags'); @@ -1482,7 +1482,7 @@ class DispatcherTest extends CakeTestCase { $out = ob_get_clean(); ob_start(); - $dispatcher->cached($request); + $dispatcher->cached($request->here); $cached = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", $out); @@ -1490,7 +1490,7 @@ class DispatcherTest extends CakeTestCase { $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached); $this->assertEqual($result, $expected); - $filename = $this->__cachePath($dispatcher->here); + $filename = $this->__cachePath($request->here); unlink($filename); $request = new CakeRequest('test_cached_pages/view/param/param'); @@ -1500,7 +1500,7 @@ class DispatcherTest extends CakeTestCase { $out = ob_get_clean(); ob_start(); - $dispatcher->cached($request); + $dispatcher->cached($request->here); $cached = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", $out); @@ -1508,7 +1508,7 @@ class DispatcherTest extends CakeTestCase { $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached); $this->assertEqual($result, $expected); - $filename = $this->__cachePath($dispatcher->here); + $filename = $this->__cachePath($request->here); unlink($filename); $request = new CakeRequest('test_cached_pages/view/foo:bar/value:goo'); @@ -1518,7 +1518,7 @@ class DispatcherTest extends CakeTestCase { $out = ob_get_clean(); ob_start(); - $dispatcher->cached($request); + $dispatcher->cached($request->here); $cached = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", $out); @@ -1526,7 +1526,7 @@ class DispatcherTest extends CakeTestCase { $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached); $this->assertEqual($result, $expected); - $filename = $this->__cachePath($dispatcher->here); + $filename = $this->__cachePath($request->here); $this->assertTrue(file_exists($filename)); unlink($filename); From 5e3cd74b03b220cbfe7c1e64d3761bb5531af02f Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 2 Mar 2011 22:03:21 -0500 Subject: [PATCH 6/6] Replacing request->here with request->here(). Saves a call to Router::normalize() in a few places. --- cake/libs/error/exception_renderer.php | 6 +++--- cake/libs/view/helpers/cache.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cake/libs/error/exception_renderer.php b/cake/libs/error/exception_renderer.php index f62b75833..25adff3aa 100644 --- a/cake/libs/error/exception_renderer.php +++ b/cake/libs/error/exception_renderer.php @@ -172,7 +172,7 @@ class ExceptionRenderer { * @return void */ protected function _cakeError(CakeException $error) { - $url = Router::normalize($this->controller->request->here); + $url = $this->controller->request->here(); $code = $error->getCode(); $this->controller->response->statusCode($code); $this->controller->set(array( @@ -195,7 +195,7 @@ class ExceptionRenderer { if (Configure::read('debug') == 0 && $error instanceof CakeException) { $message = __('Not Found'); } - $url = Router::normalize($this->controller->request->here); + $url = $this->controller->request->here(); $this->controller->response->statusCode($error->getCode()); $this->controller->set(array( 'name' => $message, @@ -211,7 +211,7 @@ class ExceptionRenderer { * @param array $params Parameters for controller */ public function error500($error) { - $url = Router::normalize($this->controller->request->here); + $url = $this->controller->request->here(); $code = ($error->getCode() > 500) ? $error->getCode() : 500; $this->controller->response->statusCode($code); $this->controller->set(array( diff --git a/cake/libs/view/helpers/cache.php b/cake/libs/view/helpers/cache.php index 3ee75c3fd..ef63994e1 100644 --- a/cake/libs/view/helpers/cache.php +++ b/cake/libs/view/helpers/cache.php @@ -213,8 +213,8 @@ class CacheHelper extends AppHelper { } else { $cacheTime = strtotime($timestamp, $now); } - $path = $this->request->here; - if ($this->here == '/') { + $path = $this->request->here(); + if ($path == '/') { $path = 'home'; } $cache = strtolower(Inflector::slug($path));