Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handle DOCUMENT_ROOT (Alias ...) #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/sacy/fragment-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ function get($key){

function set($key, $value){
$p = $this->key2file($key);
if (!@mkdir(dirname($p), 0755, true)){
throw new Exception("Failed to create fragment cache dir: $p");
$dir = dirname($p);
if (!is_dir($dir)) {
if (!@mkdir($dir, 0755, true)){
throw new Exception("Failed to create fragment cache dir: $p");
}
}
return @file_put_contents($p, $value);
}
Expand Down
23 changes: 14 additions & 9 deletions src/sacy/sacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,11 @@ private function urlToFile($ref){
if (isset($u['query'])) return false;

$ref = $u['path'];
$path = array($_SERVER['DOCUMENT_ROOT']);
$path = array($this->_cfg->getDocumentRoot());
if ($ref[0] != '/')
$path[] = $_SERVER['PHP_SELF'];
$path[] = $ref;
return realpath(implode(DIRECTORY_SEPARATOR, $path));

}


Expand Down Expand Up @@ -223,6 +222,11 @@ public function __construct($params = null){
$this->setParams($params);
}

public function getDocumentRoot()
{
return realpath(str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']));
}

public function getDebugMode(){
if ($this->params['debug_toggle'] === false)
return 0;
Expand Down Expand Up @@ -555,7 +559,7 @@ function writeHeader($fh, $work_units){
fwrite($fh, "/*\nsacy javascript cache dump \n\n");
fwrite($fh, "This dump has been created from the following files:\n");
foreach($work_units as $file){
fprintf($fh, " - %s\n", str_replace($_SERVER['DOCUMENT_ROOT'], '<root>', $file['file']));
fprintf($fh, " - %s\n", str_replace($this->getConfig()->getDocumentRoot(), '<root>', $file['file']));
}
fwrite($fh, "*/\n\n");
}
Expand Down Expand Up @@ -595,7 +599,7 @@ function getOutput($work_unit){

function processFile($fh, $work_unit){
if ($this->getConfig()->get('write_headers'))
fprintf($fh, "\n/* %s */\n", str_replace($_SERVER['DOCUMENT_ROOT'], '<root>', $work_unit['file']));
fprintf($fh, "\n/* %s */\n", str_replace($this->getConfig()->getDocumentRoot(), '<root>', $work_unit['file']));
fwrite($fh, $this->getOutput($work_unit));
}

Expand Down Expand Up @@ -631,7 +635,7 @@ function writeHeader($fh, $work_units){
fwrite($fh, "/*\nsacy css cache dump \n\n");
fwrite($fh, "This dump has been created from the following files:\n");
foreach($work_units as $file){
fprintf($fh, " - %s\n", str_replace($_SERVER['DOCUMENT_ROOT'], '<root>', $file['file']));
fprintf($fh, " - %s\n", str_replace($this->getConfig()->getDocumentRoot(), '<root>', $file['file']));
}
fwrite($fh, "*/\n\n");
}
Expand All @@ -648,7 +652,7 @@ function processFile($fh, $work_unit){
$content = \Minify_CSS_UriRewriter::rewrite(
$content,
dirname($work_unit['file']),
$_SERVER['DOCUMENT_ROOT'],
$this->getConfig()->getDocumentRoot(),
array(),
true
);
Expand All @@ -660,7 +664,7 @@ function processFile($fh, $work_unit){
);
}else{
if ($this->getConfig()->get('write_headers'))
fprintf($fh, "\n/* %s */\n", str_replace($_SERVER['DOCUMENT_ROOT'], '<root>', $work_unit['file']));
fprintf($fh, "\n/* %s */\n", str_replace($this->getConfig()->getDocumentRoot(), '<root>', $work_unit['file']));

fwrite($fh, $this->getOutput($work_unit));
}
Expand Down Expand Up @@ -728,12 +732,13 @@ function getOutput($work_unit){
return \Minify_CSS_UriRewriter::rewrite(
$css,
dirname($source_file),
$_SERVER['DOCUMENT_ROOT'],
$this->getConfig()->getDocumentRoot(),
array()
);
}else{
return \Minify_CSS::minify($css, array(
'currentDir' => dirname($source_file)
'currentDir' => dirname($source_file),
'docRoot' => $this->getConfig()->getDocumentRoot()
));
}
}
Expand Down