From c6b4b267b7a792be6f40fd8bf491484ec4579456 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 14 Aug 2011 12:01:54 -0400 Subject: [PATCH] Add RootController::MakeURL() --- http/root_controller.php | 34 +++++++++++++++++++++ testing/tests/http/root_controller_test.php | 34 +++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/http/root_controller.php b/http/root_controller.php index 4a0832f..85a7174 100644 --- a/http/root_controller.php +++ b/http/root_controller.php @@ -185,6 +185,40 @@ class RootController return $pattern; } } + + /*! + Given a relative path, return an absolute path from the root controller. + @param string The relative path for which a URL will be created + @param bool Include the HTTP scheme and host to create an RFC URL if true; + if false an absolute path will be returned. + */ + public function MakeURL($new_path, $url = FALSE) + { + // Detect the common paths between the REQUEST_URI and the PATH_INFO. That + // common piece will be the path to the root controller. + $request_uri = $this->request()->data['_SERVER']['REQUEST_URI']; + $path_info = $this->request()->data['_SERVER']['PATH_INFO']; + $common_uri = strstr($request_uri, $path_info, TRUE); + + // If just constructing an absolute path, return that now. + if (!$url) + return $common_uri . $new_path; + + // Otherwise, build the host part. + $url = 'http'; + if (isset($this->request()->data['_SERVER']['HTTPS']) && + $this->request()->data['_SERVER']['HTTPS'] == 'on') { + $url .= 's'; + } + $url .= '://' . $this->request()->data['_SERVER']['HTTP_HOST']; + + $port = $this->request()->data['_SERVER']['SERVER_PORT']; + if ($port != 80 && $port != 443) + $url .= ':' . $port; + + $url .= $common_uri; + return $url . $new_path; + } } /*! diff --git a/testing/tests/http/root_controller_test.php b/testing/tests/http/root_controller_test.php index 2135db5..3324545 100644 --- a/testing/tests/http/root_controller_test.php +++ b/testing/tests/http/root_controller_test.php @@ -160,4 +160,38 @@ class RootControllerTest extends \PHPUnit_Framework_TestCase $mock->set_output_filter($output_filter); $mock->Stop(); } + + public function testAbsolutify() + { + $globals = array( + '_SERVER' => array( + 'HTTP_HOST' => 'www.bluestatic.org', + 'REQUEST_URI' => '/hoplite/webapp/test/path', + 'PATH_INFO' => '/test/path', + 'SERVER_PORT' => 80, + ), + ); + $mock = new \hoplite\http\RootController($globals); + + $this->assertEquals($mock->MakeURL('/'), '/hoplite/webapp/'); + $this->assertEquals($mock->MakeURL('/', TRUE), 'http://www.bluestatic.org/hoplite/webapp/'); + + $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2'); + $this->assertEquals($mock->MakeURL('/path/3', TRUE), 'http://www.bluestatic.org/hoplite/webapp/path/3'); + + $globals['_SERVER']['HTTPS'] = 'on'; + $globals['_SERVER']['SERVER_PORT'] = 443; + $mock = new \hoplite\http\RootController($globals); + $this->assertEquals($mock->MakeURL('/'), '/hoplite/webapp/'); + $this->assertEquals($mock->MakeURL('/', TRUE), 'https://www.bluestatic.org/hoplite/webapp/'); + + $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2'); + $this->assertEquals($mock->MakeURL('/path/3', TRUE), 'https://www.bluestatic.org/hoplite/webapp/path/3'); + + $globals['_SERVER']['SERVER_PORT'] = 8080; + $mock = new \hoplite\http\RootController($globals); + $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2'); + $this->assertEquals($mock->MakeURL('/', TRUE), 'https://www.bluestatic.org:8080/hoplite/webapp/'); + $this->assertEquals($mock->MakeURL('/path/3', TRUE), 'https://www.bluestatic.org:8080/hoplite/webapp/path/3'); + } } -- 2.22.5