Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.0' into 2.0-class-loading
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 5, 2011
2 parents 3ee49c0 + 5e3cd74 commit f8a0843
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 43 deletions.
6 changes: 3 additions & 3 deletions lib/Cake/Error/ExceptionRenderer.php
Expand Up @@ -173,7 +173,7 @@ public function render() {
* @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(
Expand All @@ -196,7 +196,7 @@ public function error400($error) {
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,
Expand All @@ -212,7 +212,7 @@ public function error400($error) {
* @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(
Expand Down
23 changes: 21 additions & 2 deletions lib/Cake/Network/CakeRequest.php
Expand Up @@ -500,6 +500,23 @@ public function addPaths($paths) {
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.
*
Expand Down Expand Up @@ -535,7 +552,8 @@ public function host() {
/**
* 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) {
Expand All @@ -547,7 +565,8 @@ public function domain($tldLength = 1) {
/**
* 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) {
Expand Down
19 changes: 4 additions & 15 deletions lib/Cake/Routing/Dispatcher.php
Expand Up @@ -40,14 +40,6 @@
*/
class Dispatcher {

/**
* Current URL
*
* @var string
* @access public
*/
public $here = false;

/**
* The request object
*
Expand Down Expand Up @@ -94,9 +86,7 @@ public function __construct($url = null, $base = false) {
* 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;
}

Expand Down Expand Up @@ -264,12 +254,11 @@ protected function _loadRoutes() {
/**
* 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));
Expand Down
3 changes: 2 additions & 1 deletion lib/Cake/Routing/Route/CakeRoute.php
Expand Up @@ -193,12 +193,13 @@ public function parse($url) {
} 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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/View/Helper/CacheHelper.php
Expand Up @@ -215,8 +215,8 @@ protected function _writeFile($content, $timestamp, $useCallbacks = false) {
} 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));
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -241,7 +241,7 @@ function create($model = null, $options = array()) {
$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');
}
Expand Down
24 changes: 24 additions & 0 deletions lib/Cake/tests/cases/libs/cake_request.test.php
Expand Up @@ -1402,6 +1402,30 @@ function testAcceptLanguage() {
$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
*
Expand Down
24 changes: 12 additions & 12 deletions lib/Cake/tests/cases/libs/dispatcher.test.php
Expand Up @@ -1424,7 +1424,7 @@ public function testFullPageCachingDispatch() {
$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);
Expand All @@ -1433,7 +1433,7 @@ public function testFullPageCachingDispatch() {

$this->assertEqual($result, $expected);

$filename = $this->__cachePath($dispatcher->here);
$filename = $this->__cachePath($request->here);
unlink($filename);

$request = new CakeRequest('test_cached_pages/index');
Expand All @@ -1446,15 +1446,15 @@ public function testFullPageCachingDispatch() {
$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);
$cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
$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');
Expand All @@ -1464,15 +1464,15 @@ public function testFullPageCachingDispatch() {
$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);
$cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
$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');
Expand All @@ -1482,15 +1482,15 @@ public function testFullPageCachingDispatch() {
$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);
$cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
$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');
Expand All @@ -1500,15 +1500,15 @@ public function testFullPageCachingDispatch() {
$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);
$cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
$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');
Expand All @@ -1518,15 +1518,15 @@ public function testFullPageCachingDispatch() {
$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);
$cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
$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);
Expand Down
25 changes: 18 additions & 7 deletions lib/Cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -668,12 +668,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 = '';
Expand Down Expand Up @@ -5670,12 +5671,21 @@ function testCreate() {
'/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;'),
Expand All @@ -5685,11 +5695,12 @@ function testCreate() {
$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;'),
Expand Down

0 comments on commit f8a0843

Please sign in to comment.