Merge pull request #6332 from lukanetconsult/hotfix/6028-realpath-on-webroot

Refactor webroot detection in resource locator
This commit is contained in:
Morris Jobke 2017-09-06 22:51:31 +02:00 committed by GitHub
commit d84a20e8fd
2 changed files with 66 additions and 62 deletions

View File

@ -117,25 +117,9 @@ class CSSResourceLocator extends ResourceLocator {
parent::append($root, $file, $webRoot, $throw); parent::append($root, $file, $webRoot, $throw);
} else { } else {
if (!$webRoot) { if (!$webRoot) {
$tmpRoot = realpath($root); $webRoot = $this->findWebRoot($root);
/*
* traverse the potential web roots upwards in the path
*
* example:
* - root: /srv/www/apps/myapp
* - available mappings: ['/srv/www']
*
* First we check if a mapping for /srv/www/apps/myapp is available,
* then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a
* valid web root
*/
do {
if (isset($this->mapping[$tmpRoot])) {
$webRoot = $this->mapping[$tmpRoot];
break;
}
if ($tmpRoot === '/') { if (!$webRoot) {
$webRoot = ''; $webRoot = '';
$this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [ $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
'app' => 'lib', 'app' => 'lib',
@ -144,11 +128,7 @@ class CSSResourceLocator extends ResourceLocator {
'webRoot' => $webRoot, 'webRoot' => $webRoot,
'throw' => $throw ? 'true' : 'false' 'throw' => $throw ? 'true' : 'false'
]); ]);
break;
} }
$tmpRoot = dirname($tmpRoot);
} while(true);
} }
if ($throw && $tmpRoot === '/') { if ($throw && $tmpRoot === '/') {

View File

@ -106,6 +106,50 @@ abstract class ResourceLocator {
return false; return false;
} }
/**
* Attempt to find the webRoot
*
* traverse the potential web roots upwards in the path
*
* example:
* - root: /srv/www/apps/myapp
* - available mappings: ['/srv/www']
*
* First we check if a mapping for /srv/www/apps/myapp is available,
* then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a
* valid web root
*
* @param string $root
* @return string|null The web root or null on failure
*/
protected function findWebRoot($root) {
$webRoot = null;
$tmpRoot = $root;
while ($webRoot === null) {
if (isset($this->mapping[$tmpRoot])) {
$webRoot = $this->mapping[$tmpRoot];
break;
}
if ($tmpRoot === '/') {
break;
}
$tmpRoot = dirname($tmpRoot);
}
if (!$webRoot) {
$realpath = realpath($root);
if ($realpath && ($realpath !== $root)) {
return $this->findWebRoot($realpath);
}
}
return $webRoot;
}
/** /**
* append the $file resource at $root * append the $file resource at $root
* *
@ -125,25 +169,9 @@ abstract class ResourceLocator {
} }
if (!$webRoot) { if (!$webRoot) {
$tmpRoot = realpath($root); $webRoot = $this->findWebRoot($root);
/*
* traverse the potential web roots upwards in the path
*
* example:
* - root: /srv/www/apps/myapp
* - available mappings: ['/srv/www']
*
* First we check if a mapping for /srv/www/apps/myapp is available,
* then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a
* valid web root
*/
do {
if (isset($this->mapping[$tmpRoot])) {
$webRoot = $this->mapping[$tmpRoot];
break;
}
if ($tmpRoot === '/') { if (!$webRoot) {
$webRoot = ''; $webRoot = '';
$this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [ $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
'app' => 'lib', 'app' => 'lib',
@ -152,11 +180,7 @@ abstract class ResourceLocator {
'webRoot' => $webRoot, 'webRoot' => $webRoot,
'throw' => $throw ? 'true' : 'false' 'throw' => $throw ? 'true' : 'false'
]); ]);
break;
} }
$tmpRoot = dirname($tmpRoot);
} while(true);
} }
$this->resources[] = array($root, $webRoot, $file); $this->resources[] = array($root, $webRoot, $file);