Skip to content

Commit

Permalink
Adding in new CompilationException and removing cache file if it is c…
Browse files Browse the repository at this point in the history
…alled. Also putting in new relative path fix for CSS stylesheets.
  • Loading branch information
meenie committed Oct 17, 2013
1 parent 52c78f6 commit b83c399
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 28 deletions.
12 changes: 12 additions & 0 deletions src/Munee/Asset/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ public function getLastModifiedDate()
return $this->lastModifiedDate;
}

/**
* If an exception is handled this function will fire and clean up any files
* that have been cached as they have not properly compiled.
*/
public function cleanUpAfterError()
{
foreach ($this->request->files as $file) {
$cacheFile = $this->generateCacheFile($file);
unlink($cacheFile);
}
}

/**
* Callback method called before filters are run
*
Expand Down
18 changes: 18 additions & 0 deletions src/Munee/Asset/Type/CompilationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Munee: Optimising Your Assets
*
* @copyright Cody Lundquist 2013
* @license http://opensource.org/licenses/mit-license.php
*/

namespace Munee\Asset\Type;

use Munee\ErrorException;

/**
* Class CompilationException
*
* @author Cody Lundquist
*/
class CompilationException extends ErrorException {}
45 changes: 25 additions & 20 deletions src/Munee/Asset/Type/Css.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Munee\Asset\Type;

use Munee\ErrorException;
use Munee\Utils;
use Munee\Asset\Type;
use lessc;
Expand Down Expand Up @@ -77,7 +76,7 @@ protected function checkCache($originalFile, $cacheFile)
* @param string $originalFile
* @param string $cacheFile
*
* @throws ErrorException
* @throws CompilationException
*/
protected function beforeFilter($originalFile, $cacheFile)
{
Expand All @@ -86,9 +85,7 @@ protected function beforeFilter($originalFile, $cacheFile)
try {
$compiledLess = $less->cachedCompile($originalFile);
} catch (\Exception $e) {
// Remove the Cache File because it hasn't been properly compiled yet
unlink($cacheFile);
throw new ErrorException('Error in LESS Compiler', 0, $e);
throw new CompilationException('Error in LESS Compiler', 0, $e);
}
$compiledLess['compiled'] = $this->fixRelativeImagePaths($compiledLess['compiled'], $originalFile);
file_put_contents($cacheFile, serialize($compiledLess));
Expand All @@ -98,9 +95,7 @@ protected function beforeFilter($originalFile, $cacheFile)
try {
$compiled = $scss->compile(file_get_contents($originalFile));
} catch (\Exception $e) {
// Remove the Cache File because it hasn't been properly compiled yet
unlink($cacheFile);
throw new ErrorException('Error in SCSS Compiler', 0, $e);
throw new CompilationException('Error in SCSS Compiler', 0, $e);
}

$content = compact('compiled');
Expand Down Expand Up @@ -166,31 +161,41 @@ protected function isScss($file)
* @param $originalFile
*
* @return string
*
* @throws CompilationException
*/
protected function fixRelativeImagePaths($content, $originalFile)
{
$regEx = '%(url[\\s]*\\()[\\s\'"]*([^\\)\'"]*)[\\s\'"]*(\\))%';

$webroot = $this->request->webroot;
$changedContent = preg_replace_callback($regEx, function ($match) use ($originalFile, $webroot) {
$basePath = trim($match[2]);
// Skip conversion if the first character is a '/' since it's already an absolute path;
if ($basePath[0] !== '/') {
$basePathPrefix = str_replace($webroot, '', dirname($originalFile));
if (! empty($basePathPrefix)) {
$basePathPrefix .= '/';
$filePath = trim($match[2]);
// Skip conversion if the first character is a '/' since it's already an absolute path
if ($filePath[0] !== '/') {
$basePath = str_replace($webroot, '', dirname($originalFile));
$basePathParts = array_reverse(array_filter(explode('/', $basePath)));
$numOfRecursiveDirs = substr_count($filePath, '../');
if ($numOfRecursiveDirs > count($basePathParts)) {
throw new CompilationException(
'Error in stylesheet <strong>' . $originalFile .
'</strong>. The following URL goes above webroot: <strong>' . $filePath .
'</strong>'
);
}

$basePath = $basePathPrefix . $basePath;
$basePathParts = array_slice($basePathParts, $numOfRecursiveDirs);
$basePath = implode('/', array_reverse($basePathParts));

// Lets remove the relative path markers (../../) and the directory above them.
$count = 1;
while ($count > 0) {
$basePath = preg_replace('%([^/]+/\\.\\./|\\./)%', '', $basePath, -1, $count);
if (! empty($basePath) && $basePath[0] != '/') {
$basePath = '/' . $basePath;
}

$filePath = $basePath . '/' . $filePath;
$filePath = str_replace(array('../', './'), '', $filePath);
}

return $match[1] . $basePath . $match[3];
return $match[1] . $filePath . $match[3];
}, $content);

if (null !== $changedContent) {
Expand Down
36 changes: 28 additions & 8 deletions src/Munee/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,36 @@ public static function run(Request $Request, $options = array())
*/
return $Response->notModified ? null : $Response->render();
} catch (Asset\NotFoundException $e) {
$headerController->statusCode('HTTP/1.0', 404, 'Not Found');
$headerController->headerField('Status', '404 Not Found');
return 'Error: ' . $e->getMessage();
} catch (ErrorException $e) {
$errors = 'Error: ' . $e->getMessage();
while ($e = $e->getPrevious()) {
$errors .= "<br>" . $e->getMessage();
if (isset($headerController) && $headerController instanceof Asset\HeaderSetter) {
$headerController->statusCode('HTTP/1.0', 404, 'Not Found');
$headerController->headerField('Status', '404 Not Found');
}

return 'Not Found Error: ' . static::getErrors($e);
} catch (Asset\Type\CompilationException $e) {
if (isset($AssetType) && $AssetType instanceof Asset\Type) {
$AssetType->cleanUpAfterError();
}

return $errors;
return 'Compilation Error: ' . static::getErrors($e);
} catch (ErrorException $e) {
return 'Error: ' . static::getErrors($e);
}
}

/**
* Grabs all of the Exception messages in a chain
*
* @param \Exception $e
*
* @return string
*/
protected static function getErrors (\Exception $e) {
$errors = $e->getMessage();
while ($e = $e->getPrevious()) {
$errors .= "<br>" . $e->getMessage();
}

return $errors;
}
}

0 comments on commit b83c399

Please sign in to comment.