move root directory detection into SemanticScuttle_Environment to make it testable

Conflicts:
	tests/SemanticScuttle/EnvironmentTest.php
This commit is contained in:
Christian Weiske 2011-08-15 18:20:59 +02:00
parent bbe708ce17
commit b0e4b4390b
3 changed files with 71 additions and 23 deletions

View File

@ -44,5 +44,41 @@ class SemanticScuttle_Environment
//fallback when no special path after the php file is given
return '';
}
/**
* Determines the root directory from the server environment.
* The root directory is the path that needs to be prepended
* to relative links.
*
* Returns $GLOBALS['root'] if set.
*
* @return string Base URL with trailing slash
*/
public static function getRoot()
{
if (isset($GLOBALS['root'])) {
return $GLOBALS['root'];
}
$pieces = explode('/', $_SERVER['SCRIPT_NAME']);
$rootTmp = '/';
foreach ($pieces as $piece) {
//we eliminate possible sscuttle subfolders (like gsearch for example)
if ($piece != '' && !strstr($piece, '.php')
&& $piece != 'gsearch' && $piece != 'ajax'
) {
$rootTmp .= $piece .'/';
}
}
if (($rootTmp != '/') && (substr($rootTmp, -1, 1) != '/')) {
$rootTmp .= '/';
}
//we do not prepend http since we also want to support https connections
// "http" is not required; it's automatically determined by the browser
// depending on the current connection.
return '//'. $_SERVER['HTTP_HOST'] . $rootTmp;
}
}
?>

View File

@ -25,29 +25,7 @@ if (isset($GLOBALS['debugMode'])) {
}
// Determine the base URL as ROOT
if (!isset($GLOBALS['root'])) {
$pieces = explode('/', $_SERVER['SCRIPT_NAME']);
$rootTmp = '/';
foreach ($pieces as $piece) {
//we eliminate possible sscuttle subfolders (like gsearch for example)
if ($piece != '' && !strstr($piece, '.php')
&& $piece != 'gsearch' && $piece != 'ajax'
) {
$rootTmp .= $piece .'/';
}
}
if (($rootTmp != '/') && (substr($rootTmp, -1, 1) != '/')) {
$rootTmp .= '/';
}
//we do not prepend http since we also want to support https connections
// "http" is not required; it's automatically determined by the browser
// depending on the current connection.
define('ROOT', '//'. $_SERVER['HTTP_HOST'] . $rootTmp);
} else {
define('ROOT', $GLOBALS['root']);
}
define('ROOT', SemanticScuttle_Environment::getRoot());
define('ROOT_JS', ROOT . 'js/jstree-1.0-rc2/');
// Error codes

View File

@ -229,6 +229,40 @@ class SemanticScuttle_EnvironmentTest extends PHPUnit_Framework_TestCase
'/dummy/foo', SemanticScuttle_Environment::getServerPathInfo()
);
}
public function testGetRootInRootDir()
{
$_SERVER = array(
'HTTP_HOST' => 'bm.bogo',
'DOCUMENT_ROOT' => '/etc/apache2/htdocs',
'SCRIPT_FILENAME' => '/home/cweiske/Dev/html/hosts/bm.bogo/index.php',
'QUERY_STRING' => '',
'REQUEST_URI' => '/',
'SCRIPT_NAME' => '/index.php',
'PHP_SELF' => '/index.php',
);
$this->assertEquals(
'//bm.bogo/',
SemanticScuttle_Environment::getRoot()
);
}
public function testGetRootInSubdir()
{
$_SERVER = array(
'HTTP_HOST' => 'bm-subdir.bogo',
'DOCUMENT_ROOT' => '/etc/apache2/htdocs',
'SCRIPT_FILENAME' => '/home/cweiske/Dev/html/hosts/bm-subdir.bogo/tools/scuttle/index.php',
'QUERY_STRING' => '',
'REQUEST_URI' => '/tools/scuttle/',
'SCRIPT_NAME' => '/tools/scuttle/index.php',
'PHP_SELF' => '/tools/scuttle/index.php',
);
$this->assertEquals(
'//bm-subdir.bogo/tools/scuttle/',
SemanticScuttle_Environment::getRoot()
);
}
}
?>