diff --git a/composer.json b/composer.json index 54facc5..1a27fea 100644 --- a/composer.json +++ b/composer.json @@ -25,5 +25,8 @@ "imagine/Imagine": "*", "coffeescript/coffeescript": "*", "meenie/javascript-packer": "*" + }, + "require-dev": { + "phpunit/phpunit": "3.7.*" } } \ No newline at end of file diff --git a/src/Munee/Asset/Filter.php b/src/Munee/Asset/Filter.php index c72fb1b..2f0f38d 100644 --- a/src/Munee/Asset/Filter.php +++ b/src/Munee/Asset/Filter.php @@ -18,14 +18,14 @@ abstract class Filter /** * @var array */ - protected $_allowedParams = array(); + protected $allowedParams = array(); /** * @return array */ public function getAllowedParams() { - return $this->_allowedParams; + return $this->allowedParams; } /** diff --git a/src/Munee/Asset/Filter/Css/Minify.php b/src/Munee/Asset/Filter/Css/Minify.php index 3cbf648..9e3aaad 100644 --- a/src/Munee/Asset/Filter/Css/Minify.php +++ b/src/Munee/Asset/Filter/Css/Minify.php @@ -21,7 +21,7 @@ class Minify extends Filter /** * @var array */ - protected $_allowedParams = array( + protected $allowedParams = array( 'minify' => array( 'regex' => 'true|false|t|f|yes|no|y|n', 'default' => 'false', @@ -46,10 +46,10 @@ public function doFilter($file, $arguments, $cssOptions) $content = file_get_contents($file); if (Utils::isSerialized($content, $content)) { - $content['compiled'] = $this->_minify($content['compiled']); + $content['compiled'] = $this->minify($content['compiled']); $content = serialize($content); } else { - $content = $this->_minify($content); + $content = $this->minify($content); } file_put_contents($file, $content); @@ -62,7 +62,7 @@ public function doFilter($file, $arguments, $cssOptions) * * @return string */ - protected function _minify($content) + protected function minify($content) { $regexs = array( // Remove Comments diff --git a/src/Munee/Asset/Filter/Image/Colorize.php b/src/Munee/Asset/Filter/Image/Colorize.php index 394c712..f4e6580 100644 --- a/src/Munee/Asset/Filter/Image/Colorize.php +++ b/src/Munee/Asset/Filter/Image/Colorize.php @@ -22,7 +22,7 @@ class Colorize extends Filter /** * @var array */ - protected $_allowedParams = array( + protected $allowedParams = array( 'colorize' => array( 'regex' => '[A-Fa-f0-9]{3}$|^[A-Fa-f0-9]{6}', 'cast' => 'string' @@ -34,10 +34,11 @@ class Colorize extends Filter * * @param string $file * @param array $arguments + * @param array $typeOptions * * @return void */ - public function doFilter($file, $arguments) + public function doFilter($file, $arguments, $typeOptions) { $Imagine = new Imagine(); $image = $Imagine->open($file); diff --git a/src/Munee/Asset/Filter/Image/Grayscale.php b/src/Munee/Asset/Filter/Image/Grayscale.php index e5ae83a..6299df6 100644 --- a/src/Munee/Asset/Filter/Image/Grayscale.php +++ b/src/Munee/Asset/Filter/Image/Grayscale.php @@ -21,7 +21,7 @@ class Grayscale extends Filter /** * @var array */ - protected $_allowedParams = array( + protected $allowedParams = array( 'grayscale' => array( 'regex' => 'true|false|t|f|yes|no|y|n', 'default' => 'false', @@ -34,10 +34,11 @@ class Grayscale extends Filter * * @param string $file * @param array $arguments + * @param array $typeOptions * * @return void */ - public function doFilter($file, $arguments) + public function doFilter($file, $arguments, $typeOptions) { if (! $arguments['grayscale']) { return; diff --git a/src/Munee/Asset/Filter/Image/Negative.php b/src/Munee/Asset/Filter/Image/Negative.php index 0de9a60..bee6fae 100644 --- a/src/Munee/Asset/Filter/Image/Negative.php +++ b/src/Munee/Asset/Filter/Image/Negative.php @@ -21,7 +21,7 @@ class Negative extends Filter /** * @var array */ - protected $_allowedParams = array( + protected $allowedParams = array( 'negative' => array( 'regex' => 'true|false|t|f|yes|no|y|n', 'cast' => 'string' @@ -33,10 +33,11 @@ class Negative extends Filter * * @param string $file * @param array $arguments + * @param array $typeOptions * * @return void */ - public function doFilter($file, $arguments) + public function doFilter($file, $arguments, $typeOptions) { $Imagine = new Imagine(); $image = $Imagine->open($file); diff --git a/src/Munee/Asset/Filter/Image/Resize.php b/src/Munee/Asset/Filter/Image/Resize.php index 899a705..d209ebb 100644 --- a/src/Munee/Asset/Filter/Image/Resize.php +++ b/src/Munee/Asset/Filter/Image/Resize.php @@ -25,7 +25,7 @@ class Resize extends Filter /** * @var array */ - protected $_allowedParams = array( + protected $allowedParams = array( 'resize' => array( 'arguments' => array( 'width' => array( diff --git a/src/Munee/Asset/Filter/JavaScript/Minify.php b/src/Munee/Asset/Filter/JavaScript/Minify.php index afd572f..98078de 100644 --- a/src/Munee/Asset/Filter/JavaScript/Minify.php +++ b/src/Munee/Asset/Filter/JavaScript/Minify.php @@ -21,7 +21,7 @@ class Minify extends Filter /** * @var array */ - protected $_allowedParams = array( + protected $allowedParams = array( 'minify' => array( 'regex' => 'true|false|t|f|yes|no|y|n', 'default' => 'false', diff --git a/src/Munee/Asset/Filter/JavaScript/Packer.php b/src/Munee/Asset/Filter/JavaScript/Packer.php index f0f2cce..c9254d8 100644 --- a/src/Munee/Asset/Filter/JavaScript/Packer.php +++ b/src/Munee/Asset/Filter/JavaScript/Packer.php @@ -21,7 +21,7 @@ class Packer extends Filter /** * @var array */ - protected $_allowedParams = array( + protected $allowedParams = array( 'packer' => array( 'regex' => 'true|false|t|f|yes|no|y|n', 'default' => 'false', diff --git a/src/Munee/Asset/Type.php b/src/Munee/Asset/Type.php index 076f32f..6166d26 100644 --- a/src/Munee/Asset/Type.php +++ b/src/Munee/Asset/Type.php @@ -8,7 +8,9 @@ namespace Munee\Asset; +use Munee\ErrorException; use Munee\Request; +use Munee\Response; use Munee\Utils; use Munee\Asset\NotFoundException; @@ -23,42 +25,47 @@ abstract class Type /** * @var array */ - protected $_options = array(); + protected $options = array(); /** * @var array */ - protected $_params = array(); + protected $params = array(); /** * @var array */ - protected $_filters = array(); + protected $filters = array(); /** * @var string */ - protected $_cacheDir; + protected $cacheDir; /** * @var integer */ - protected $_lastModifiedDate = 0; + protected $lastModifiedDate = 0; /** * @var string */ - protected $_content; + protected $content; /** - * @var object + * @var \Munee\Request */ - protected $_request; + protected $request; /** - * @var object + * @var \Munee\Response */ - public $_response; + protected $response; + + /** + * All Type Sub-Classes must create this method to set their additional headers + */ + abstract public function getHeaders(); /** * Constructor @@ -69,7 +76,7 @@ abstract class Type */ public function __construct(Request $Request) { - $this->_request = $Request; + $this->request = $Request; // Pull in filters based on the raw params that were passed in $rawParams = $Request->getRawParams(); @@ -80,26 +87,26 @@ public function __construct(Request $Request) if (class_exists($filterClass)) { $Filter = new $filterClass(); $allowedParams += $Filter->getAllowedParams(); - $this->_filters[$filterName] = $Filter; + $this->filters[$filterName] = $Filter; } } // Parse the raw params based on a map of allowedParams for those filters - $this->_request->parseParams($allowedParams); + $this->request->parseParams($allowedParams); - $this->_cacheDir = MUNEE_CACHE . DS . $assetShortName; + $this->cacheDir = MUNEE_CACHE . DS . $assetShortName; $optionsKey = strtolower($assetShortName); // Set the AssetType options if someone were passed in through the Request Class - if (isset($this->_request->options[$optionsKey])) { - $this->_options = array_merge( - $this->_options, - $this->_request->options[$optionsKey] + if (isset($this->request->options[$optionsKey])) { + $this->options = array_merge( + $this->options, + $this->request->options[$optionsKey] ); } // Create cache dir if needed - Utils::createDir($this->_cacheDir); + Utils::createDir($this->cacheDir); } /** @@ -108,34 +115,45 @@ public function __construct(Request $Request) public function init() { $content = array(); - foreach ($this->_request->files as $file) { - $cacheFile = $this->_generateCacheFile($file); + foreach ($this->request->files as $file) { + $cacheFile = $this->generateCacheFile($file); - if (! $fileContent = $this->_checkCache($file, $cacheFile)) { - $this->_setupFile($file, $cacheFile); - $fileContent = $this->_getFileContent($file, $cacheFile); + if (! $fileContent = $this->checkCache($file, $cacheFile)) { + $this->setupFile($file, $cacheFile); + $fileContent = $this->getFileContent($file, $cacheFile); } - $content[] = $this->_afterGetFileContent($fileContent); + $content[] = $this->afterGetFileContent($fileContent); } - $this->_content = implode("\n", $content); + $this->content = implode("\n", $content); } /** - * Grabs the content for the Response class + * Sets the Munee\Response class to the AssetType * - * @return string + * @param $Response + * + * @throws \Munee\ErrorException */ - public function getContent() + public function setResponse($Response) { - return $this->_content; + if (! $Response instanceof Response) { + throw new ErrorException('Response class must be an instance of Munee\Response'); + } + + $this->response = $Response; } /** - * All Type Sub-Classes must create this method to set their additional headers + * Grabs the content for the Response class + * + * @return string */ - abstract public function getHeaders(); + public function getContent() + { + return $this->content; + } /** * Return a this requests Last Modified Date. @@ -144,7 +162,7 @@ abstract public function getHeaders(); */ public function getLastModifiedDate() { - return $this->_lastModifiedDate; + return $this->lastModifiedDate; } /** @@ -153,7 +171,7 @@ public function getLastModifiedDate() * @param string $originalFile * @param string $cacheFile */ - protected function _beforeFilter($originalFile, $cacheFile) {} + protected function beforeFilter($originalFile, $cacheFile) {} /** * Callback function called after filters are run @@ -161,7 +179,7 @@ protected function _beforeFilter($originalFile, $cacheFile) {} * @param string $originalFile * @param string $cacheFile */ - protected function _afterFilter($originalFile, $cacheFile) {} + protected function afterFilter($originalFile, $cacheFile) {} /** * Callback function called after _getFileContent() is called @@ -170,7 +188,7 @@ protected function _afterFilter($originalFile, $cacheFile) {} * * @return string */ - protected function _afterGetFileContent($content) + protected function afterGetFileContent($content) { return $content; } @@ -183,11 +201,11 @@ protected function _afterGetFileContent($content) * * @throws NotFoundException */ - protected function _setupFile($originalFile, $cacheFile) + protected function setupFile($originalFile, $cacheFile) { // Check if the file exists if (! file_exists($originalFile)) { - throw new NotFoundException('File does not exist: ' . str_replace($this->_request->docroot, '', $originalFile)); + throw new NotFoundException('File does not exist: ' . str_replace($this->request->webroot, '', $originalFile)); } // Copy the original file to the cache location @@ -204,21 +222,21 @@ protected function _setupFile($originalFile, $cacheFile) * * @throws NotFoundException */ - protected function _getFileContent($originalFile, $cacheFile) + protected function getFileContent($originalFile, $cacheFile) { - $this->_beforeFilter($originalFile, $cacheFile); + $this->beforeFilter($originalFile, $cacheFile); // Run through each filter - foreach ($this->_filters as $filterName => $Filter) { - $arguments = isset($this->_request->params[$filterName]) ? - $this->_request->params[$filterName] : array(); + foreach ($this->filters as $filterName => $Filter) { + $arguments = isset($this->request->params[$filterName]) ? + $this->request->params[$filterName] : array(); if (! is_array($arguments)) { $arguments = array($filterName => $arguments); } - $Filter->doFilter($cacheFile, $arguments, $this->_options); + $Filter->doFilter($cacheFile, $arguments, $this->options); } - $this->_afterFilter($originalFile, $cacheFile); - $this->_lastModifiedDate = time(); + $this->afterFilter($originalFile, $cacheFile); + $this->lastModifiedDate = time(); return file_get_contents($cacheFile); } @@ -231,7 +249,7 @@ protected function _getFileContent($originalFile, $cacheFile) * * @return bool|string */ - protected function _checkCache($originalFile, $cacheFile) + protected function checkCache($originalFile, $cacheFile) { if (! file_exists($cacheFile) || ! file_exists($originalFile)) { return false; @@ -242,8 +260,8 @@ protected function _checkCache($originalFile, $cacheFile) return false; } - if ($this->_lastModifiedDate < $cacheFileLastModified) { - $this->_lastModifiedDate = $cacheFileLastModified; + if ($this->lastModifiedDate < $cacheFileLastModified) { + $this->lastModifiedDate = $cacheFileLastModified; } return file_get_contents($cacheFile); @@ -256,16 +274,16 @@ protected function _checkCache($originalFile, $cacheFile) * * @return string */ - protected function _generateCacheFile($file) + protected function generateCacheFile($file) { - $requestOptions = serialize($this->_request->options); - $params = serialize($this->_request->params); + $requestOptions = serialize($this->request->options); + $params = serialize($this->request->params); $ext = pathinfo($file, PATHINFO_EXTENSION); $fileHash = md5($file); $optionsHash = md5($params . $requestOptions); - $cacheDir = $this->_cacheDir . DS . substr($fileHash, 0, 2); + $cacheDir = $this->cacheDir . DS . substr($fileHash, 0, 2); Utils::createDir($cacheDir); diff --git a/src/Munee/Asset/Type/Css.php b/src/Munee/Asset/Type/Css.php index fc7410c..d715fe8 100644 --- a/src/Munee/Asset/Type/Css.php +++ b/src/Munee/Asset/Type/Css.php @@ -22,7 +22,7 @@ class Css extends Type /** * @var array */ - protected $_options = array( + protected $options = array( 'lessifyAllCss' => false, 'scssifyAllCss' => false ); @@ -32,7 +32,7 @@ class Css extends Type */ public function getHeaders() { - $this->_response->headerController->headerField('Content-Type', 'text/css'); + $this->response->headerController->headerField('Content-Type', 'text/css'); } /** @@ -44,9 +44,9 @@ public function getHeaders() * * @return bool|string|array */ - protected function _checkCache($originalFile, $cacheFile) + protected function checkCache($originalFile, $cacheFile) { - if (! $ret = parent::_checkCache($originalFile, $cacheFile)) { + if (! $ret = parent::checkCache($originalFile, $cacheFile)) { return false; } @@ -73,14 +73,14 @@ protected function _checkCache($originalFile, $cacheFile) * @param string $originalFile * @param string $cacheFile */ - protected function _beforeFilter($originalFile, $cacheFile) + protected function beforeFilter($originalFile, $cacheFile) { - if ($this->_isLess($originalFile)) { + if ($this->isLess($originalFile)) { $less = new lessc(); $compiledLess = $less->cachedCompile($originalFile); - $compiledLess['compiled'] = $this->_fixRelativeImagePaths($compiledLess['compiled'], $originalFile); + $compiledLess['compiled'] = $this->fixRelativeImagePaths($compiledLess['compiled'], $originalFile); file_put_contents($cacheFile, serialize($compiledLess)); - } elseif ($this->_isScss($originalFile)) { + } elseif ($this->isScss($originalFile)) { $scss = new \scssc(); $scss->addImportPath(pathinfo($originalFile, PATHINFO_DIRNAME)); $compiled = $scss->compile(file_get_contents($originalFile)); @@ -94,7 +94,7 @@ protected function _beforeFilter($originalFile, $cacheFile) file_put_contents($cacheFile, serialize($content)); } else { $content = file_get_contents($originalFile); - file_put_contents($cacheFile, $this->_fixRelativeImagePaths($content, $originalFile)); + file_put_contents($cacheFile, $this->fixRelativeImagePaths($content, $originalFile)); } } @@ -107,7 +107,7 @@ protected function _beforeFilter($originalFile, $cacheFile) * * @return string */ - protected function _afterGetFileContent($content) + protected function afterGetFileContent($content) { if (Utils::isSerialized($content, $content)) { $content = $content['compiled']; @@ -123,9 +123,9 @@ protected function _afterGetFileContent($content) * * @return boolean */ - protected function _isLess($file) + protected function isLess($file) { - return 'less' == pathinfo($file, PATHINFO_EXTENSION) || $this->_options['lessifyAllCss']; + return 'less' == pathinfo($file, PATHINFO_EXTENSION) || $this->options['lessifyAllCss']; } /** @@ -135,9 +135,9 @@ protected function _isLess($file) * * @return boolean */ - protected function _isScss($file) + protected function isScss($file) { - return 'scss' == pathinfo($file, PATHINFO_EXTENSION) || $this->_options['scssifyAllCss']; + return 'scss' == pathinfo($file, PATHINFO_EXTENSION) || $this->options['scssifyAllCss']; } /** @@ -148,17 +148,18 @@ protected function _isScss($file) * * @return string */ - protected function _fixRelativeImagePaths($content, $originalFile) + protected function fixRelativeImagePaths($content, $originalFile) { $regEx = '%((?:background(?:-image)?|list-style-image):.*?url[\\s]*\()[\\s\'"]*(\.\.[^\\)\'"]*)[\\s\'"]*(\\)[\\s]*)%'; - $changedContent = preg_replace_callback($regEx, function($match) use ($originalFile) { - - $basePathPrefix = str_replace($this->_request->docroot, '', dirname($originalFile)); - - if($basePathPrefix) + $changedContent = preg_replace_callback($regEx, function ($match) use ($originalFile) { + + $basePathPrefix = str_replace($this->request->webroot, '', dirname($originalFile)); + + if (! empty($basePathPrefix)) { $basePathPrefix .= '/'; - + } + $basePath = $basePathPrefix . trim($match[2]); $count = 1; while ($count > 0) { diff --git a/src/Munee/Asset/Type/Image.php b/src/Munee/Asset/Type/Image.php index d82420f..e9257f6 100644 --- a/src/Munee/Asset/Type/Image.php +++ b/src/Munee/Asset/Type/Image.php @@ -22,7 +22,7 @@ class Image extends Type /** * @var array */ - protected $_options = array( + protected $options = array( // How many filters can be done within the `allowedFiltersTimeLimit` 'numberOfAllowedFilters' => 3, // Number of seconds - default is 5 minutes @@ -33,10 +33,16 @@ class Image extends Type 'placeholders' => false, 'maxAllowedResizeWidth' => 1920, 'maxAllowedResizeHeight' => 1080, + /** + * Can easily change which image processor to use. Values can be: + * GD - Default + * Imagick + * Gmagick + */ 'imageProcessor' => 'GD' ); - protected $_placeholder = false; + protected $placeholder = false; /** * Checks to see if cache exists and is the latest, if it does, return it @@ -48,30 +54,30 @@ class Image extends Type * * @return bool|string */ - protected function _checkCache($originalFile, $cacheFile) + protected function checkCache($originalFile, $cacheFile) { - if (! $return = parent::_checkCache($originalFile, $cacheFile)) { + if (! $return = parent::checkCache($originalFile, $cacheFile)) { /** * If using the placeholder when the original file doesn't exist * and it has already been cached, return the cached contents. * Also make sure the placeholder hasn't been modified since being cached. */ - $this->_placeholder = $this->_parsePlaceholders($originalFile); + $this->placeholder = $this->parsePlaceholders($originalFile); if ( ! file_exists($originalFile) && - $this->_placeholder && - file_exists($this->_placeholder) && + $this->placeholder && + file_exists($this->placeholder) && file_exists($cacheFile) && - filemtime($cacheFile) > filemtime($this->_placeholder) + filemtime($cacheFile) > filemtime($this->placeholder) ) { return file_get_contents($cacheFile); } - if ($this->_options['checkReferrer']) { - $this->_checkReferrer(); + if ($this->options['checkReferrer']) { + $this->checkReferrer(); } - $this->_checkNumberOfAllowedFilters($cacheFile); + $this->checkNumberOfAllowedFilters($cacheFile); } return $return; @@ -84,16 +90,16 @@ protected function _checkCache($originalFile, $cacheFile) * @param string $originalFile * @param string $cacheFile */ - protected function _setupFile($originalFile, $cacheFile) + protected function setupFile($originalFile, $cacheFile) { if (! file_exists($originalFile)) { // If we are using a placeholder and that exists, use it! - if ($this->_placeholder && file_exists($this->_placeholder)) { - $originalFile = $this->_placeholder; + if ($this->placeholder && file_exists($this->placeholder)) { + $originalFile = $this->placeholder; } } - parent::_setupFile($originalFile, $cacheFile); + parent::setupFile($originalFile, $cacheFile); } @@ -102,16 +108,16 @@ protected function _setupFile($originalFile, $cacheFile) */ public function getHeaders() { - switch ($this->_request->ext) { + switch ($this->request->ext) { case 'jpg': case 'jpeg': - $this->_response->headerController->headerField('Content-Type', 'image/jpg'); + $this->response->headerController->headerField('Content-Type', 'image/jpg'); break; case 'png': - $this->_response->headerController->headerField('Content-Type', 'image/png'); + $this->response->headerController->headerField('Content-Type', 'image/png'); break; case 'gif': - $this->_response->headerController->headerField('Content-Type', 'image/gif'); + $this->response->headerController->headerField('Content-Type', 'image/gif'); break; } } @@ -121,7 +127,7 @@ public function getHeaders() * * @throws ErrorException */ - protected function _checkReferrer() + protected function checkReferrer() { if (! isset($_SERVER['HTTP_REFERER'])) { throw new ErrorException('Direct image manipulation is not allowed.'); @@ -140,7 +146,7 @@ protected function _checkReferrer() * * @throws ErrorException */ - protected function _checkNumberOfAllowedFilters($checkImage) + protected function checkNumberOfAllowedFilters($checkImage) { $pathInfo = pathinfo($checkImage); $fileNameHash = preg_replace('%-.*$%', '', $pathInfo['filename']); @@ -148,12 +154,12 @@ protected function _checkNumberOfAllowedFilters($checkImage) $cachedImages = glob($pathInfo['dirname'] . DS . $fileNameHash . '*'); // Loop through and remove the ones that are older than the time limit foreach ($cachedImages as $k => $image) { - if (filemtime($image) < time() - $this->_options['allowedFiltersTimeLimit']) { + if (filemtime($image) < time() - $this->options['allowedFiltersTimeLimit']) { unset($cachedImages[$k]); } } // Check and see if we've reached the maximum allowed resizes within the current time limit. - if (count($cachedImages) >= $this->_options['numberOfAllowedFilters']) { + if (count($cachedImages) >= $this->options['numberOfAllowedFilters']) { throw new ErrorException('You cannot create anymore resizes/manipulations at this time.'); } } @@ -165,21 +171,21 @@ protected function _checkNumberOfAllowedFilters($checkImage) * * @throws ErrorException */ - protected function _parsePlaceholders($file) + protected function parsePlaceholders($file) { $ret = false; - if (! empty($this->_options['placeholders'])) { + if (! empty($this->options['placeholders'])) { // If it's a string, use the image for all missing images. - if (is_string($this->_options['placeholders'])) { - $this->_options['placeholders'] = array('*' => $this->_options['placeholders']); + if (is_string($this->options['placeholders'])) { + $this->options['placeholders'] = array('*' => $this->options['placeholders']); } - foreach ($this->_options['placeholders'] as $path => $placeholder) { + foreach ($this->options['placeholders'] as $path => $placeholder) { // Setup path for regex - $regex = '^' . $this->_request->docroot . str_replace(array('*', $this->_request->docroot), array('.*?', ''), $path) . '$'; + $regex = '^' . $this->request->webroot . str_replace(array('*', $this->request->webroot), array('.*?', ''), $path) . '$'; if (preg_match("%{$regex}%", $file)) { if ('http' == substr($placeholder, 0, 4)) { - $ret = $this->_getImageByUrl($placeholder); + $ret = $this->getImageByUrl($placeholder); } else { $ret = $placeholder; } @@ -198,12 +204,12 @@ protected function _parsePlaceholders($file) * * @return string */ - protected function _getImageByUrl($url) + protected function getImageByUrl($url) { $cacheFolder = MUNEE_CACHE . DS . 'placeholders'; Utils::createDir($cacheFolder); - $requestOptions = serialize($this->_request->options); - $originalFile = array_shift($this->_request->files); + $requestOptions = serialize($this->request->options); + $originalFile = array_shift($this->request->files); $fileName = $cacheFolder . DS . md5($url) . '-' . md5($requestOptions . $originalFile); if (! file_exists($fileName)) { diff --git a/src/Munee/Asset/Type/JavaScript.php b/src/Munee/Asset/Type/JavaScript.php index 38a2986..bfd6d9b 100644 --- a/src/Munee/Asset/Type/JavaScript.php +++ b/src/Munee/Asset/Type/JavaScript.php @@ -23,7 +23,7 @@ class JavaScript extends Type */ public function getHeaders() { - $this->_response->headerController->headerField('Content-Type', 'text/javascript'); + $this->response->headerController->headerField('Content-Type', 'text/javascript'); } @@ -35,7 +35,7 @@ public function getHeaders() * @param string $originalFile * @param string $cacheFile */ - protected function _beforeFilter($originalFile, $cacheFile) + protected function beforeFilter($originalFile, $cacheFile) { if ('coffee' == pathinfo($originalFile, PATHINFO_EXTENSION)) { $coffeeScript = CoffeeScript\Compiler::compile(file_get_contents($originalFile), array('header' => false)); diff --git a/src/Munee/Dispatcher.php b/src/Munee/Dispatcher.php index ad055ca..8613454 100644 --- a/src/Munee/Dispatcher.php +++ b/src/Munee/Dispatcher.php @@ -9,7 +9,7 @@ namespace Munee; /** - * The outermost layer of Munee that wraps everything in a Try/Catch block and also instantiates the Render Class + * The outermost layer of Munee that wraps everything in a Try/Catch block * * @author Cody Lundquist */ @@ -20,7 +20,7 @@ class Dispatcher * * @var array */ - static $_defaultOptions = array('setHeaders' => true); + static $defaultOptions = array('setHeaders' => true); /** * 1) Initialise the Request @@ -39,18 +39,11 @@ class Dispatcher */ public static function run(Request $Request, $options = array()) { - /** - * Set the header controller. - */ - $header_controller = (isset($options['headerController']) && $options['headerController'] instanceof Asset\HeaderSetter) - ? $options['headerController'] - : new Asset\HeaderSetter; - try { /** * Merge in default options */ - $options = array_merge(self::$_defaultOptions, $options); + $options = array_merge(self::$defaultOptions, $options); /** * Initialise the Request */ @@ -67,14 +60,22 @@ public static function run(Request $Request, $options = array()) * Create a response */ $Response = new Response($AssetType); + /** + * Set the header controller. Can be overwritten by the dispatcher options + */ + if ( + isset($options['headerController']) && + $options['headerController'] instanceof Asset\HeaderSetter + ) { + $headerController = $options['headerController']; + } else { + $headerController = new Asset\HeaderSetter; + } + $Response->setHeaderController($headerController); /** * Set the headers if told to do so */ if ($options['setHeaders']) { - /** - * Set the header controller for response. - */ - $Response->setHeaderController($header_controller); /** * Set the headers. */ @@ -86,8 +87,8 @@ public static function run(Request $Request, $options = array()) */ return $Response->notModified ? null : $Response->render(); } catch (Asset\NotFoundException $e) { - $header_controller->statusCode('HTTP/1.0', 404, 'Not Found'); - $header_controller->headerField('Status', 404, 'Not Found'); + $headerController->statusCode('HTTP/1.0', 404, 'Not Found'); + $headerController->headerField('Status', '404 Not Found'); return 'Error: ' . $e->getMessage(); } catch (ErrorException $e) { return 'Error: ' . $e->getMessage(); diff --git a/src/Munee/Request.php b/src/Munee/Request.php index 8f97ee1..35200a7 100644 --- a/src/Munee/Request.php +++ b/src/Munee/Request.php @@ -18,6 +18,11 @@ */ class Request { + /** + * @var string + */ + public $webroot = WEBROOT; + /** * @var string */ @@ -41,23 +46,18 @@ class Request /** * @var array */ - protected $_rawParams = array(); + protected $rawParams = array(); /** * @var array */ - protected $_allowedParams = array(); + protected $allowedParams = array(); /** * @var string */ - protected $file_query; + protected $rawFiles; - /** - * @var string - */ - public $docroot = WEBROOT; - /** * Constructor * @@ -66,15 +66,10 @@ class Request public function __construct($options = array()) { $this->options = $options; + $this->rawFiles = isset($_GET['files']) ? $_GET['files'] : ''; + unset($_GET['files']); - $this->file_query = isset($_GET['files']) - ? $_GET['files'] - : ''; - - $this->_rawParams = $_GET; - - if(isset($this->_rawParams['files'])) - unset($this->_rawParams['files']); + $this->rawParams = $_GET; } /** @@ -84,9 +79,9 @@ public function __construct($options = array()) * * @return object */ - public function setDocroot($path) + public function setWebroot($path) { - $this->docroot = $path; + $this->webroot = $path; return $this; } @@ -101,16 +96,27 @@ public function setDocroot($path) */ public function setRawParam($key, $value = null) { - if(is_array($key)) - $this->_rawParams = $key; - else - $this->_rawParams[$key] = $value; - + if (is_array($key)) { + $this->rawParams = $key; + } else { + $this->rawParams[$key] = $value; + } + return $this; } + + /** + * Returns the pre-parsed raw params + * + * @return array + */ + public function getRawParams() + { + return $this->rawParams; + } /** - * Sets the $file_query. + * Sets the $rawFiles. * * @param string $files * @@ -118,30 +124,30 @@ public function setRawParam($key, $value = null) */ public function setFiles($files) { - $this->file_query = $files; + $this->rawFiles = $files; return $this; } /** - * Parses the $file_query and does sanity checks + * Parses the $rawFiles and does sanity checks * * @throws ErrorException * @throws Asset\NotFoundException */ public function init() { - if (empty($this->file_query)) { + if (empty($this->rawFiles)) { throw new ErrorException('No file specified; make sure you are using the correct .htaccess rules.'); } // Handle legacy code for minifying - if (preg_match('%^/minify/%', $this->file_query)) { - $this->file_query = substr($this->file_query, 7); + if (preg_match('%^/minify/%', $this->rawFiles)) { + $this->rawFiles = substr($this->rawFiles, 7); $this->setRawParam('minify', 'true'); } - $this->ext = pathinfo($this->file_query, PATHINFO_EXTENSION); + $this->ext = pathinfo($this->rawFiles, PATHINFO_EXTENSION); $supportedExtensions = Registry::getSupportedExtensions($this->ext); // Suppressing errors because Exceptions thrown in the callback cause Warnings. $this->files = @array_map(function($v) use ($supportedExtensions) { @@ -159,20 +165,8 @@ public function init() } } - return $this->docroot . $v; - }, explode(',', $this->file_query)); - - //unset($_GET['files']); - } - - /** - * Returns the pre-parsed raw params - * - * @return array - */ - public function getRawParams() - { - return $this->_rawParams; + return $this->webroot . $v; + }, explode(',', $this->rawFiles)); } /** @@ -182,18 +176,18 @@ public function getRawParams() */ public function parseParams($allowedParams) { - $this->_allowedParams = $allowedParams; - $this->_setDefaultParams(); + $this->allowedParams = $allowedParams; + $this->setDefaultParams(); - foreach ($this->_rawParams as $checkParam => $value) { - if (! $paramOptions = $this->_getParamOptions($checkParam)) { + foreach ($this->rawParams as $checkParam => $value) { + if (! $paramOptions = $this->getParamOptions($checkParam)) { continue; } $param = $paramOptions['param']; $options = $paramOptions['options']; - $paramValue = $this->_getParamValue($param, $options, $value); + $paramValue = $this->getParamValue($param, $options, $value); if (isset($this->params[$param]) && is_array($this->params[$param])) { $this->params[$param] = array_merge($this->params[$param], $paramValue); } else { @@ -205,21 +199,21 @@ public function parseParams($allowedParams) /** * Setup the default values for the allowed parameters */ - protected function _setDefaultParams() + protected function setDefaultParams() { - foreach ($this->_allowedParams as $param => $options) { + foreach ($this->allowedParams as $param => $options) { $this->params[$param] = null; if (! empty($options['arguments'])) { $this->params[$param] = array(); foreach ($options['arguments'] as $arg => $opts) { if (! empty($opts['default'])) { $cast = ! empty($opts['cast']) ? $opts['cast'] : 'string'; - $this->params[$param][$arg] = $this->_castValue($cast, $opts['default']); + $this->params[$param][$arg] = $this->castValue($cast, $opts['default']); } } } elseif (! empty($options['default'])) { $cast = ! empty($options['cast']) ? $options['cast'] : 'string'; - $this->params[$param] = $this->_castValue($cast, $options['default']); + $this->params[$param] = $this->castValue($cast, $options['default']); } } } @@ -232,12 +226,12 @@ protected function _setDefaultParams() * * @return bool|array */ - protected function _getParamOptions($checkParam) + protected function getParamOptions($checkParam) { - if (isset($this->_allowedParams[$checkParam])) { - return array('param' => $checkParam, 'options' => $this->_allowedParams[$checkParam]); + if (isset($this->allowedParams[$checkParam])) { + return array('param' => $checkParam, 'options' => $this->allowedParams[$checkParam]); } else { - foreach ($this->_allowedParams as $param => $options) { + foreach ($this->allowedParams as $param => $options) { if (! empty($options['alias']) && in_array($checkParam, (array) $options['alias'])) { return compact('param', 'options'); } @@ -259,7 +253,7 @@ protected function _getParamOptions($checkParam) * * @throws \Munee\ErrorException */ - protected function _getParamValue($param, $paramOptions, $value) + protected function getParamValue($param, $paramOptions, $value) { if (! empty($paramOptions['arguments'])) { $ret = array(); @@ -271,7 +265,7 @@ protected function _getParamValue($param, $paramOptions, $value) } $regex = "(\\b{$p})\\[(.*?)\\]"; if (preg_match("%{$regex}%", $value, $match)) { - $ret[$arg] = $this->_getParamValue($arg, $opts, $match[2]); + $ret[$arg] = $this->getParamValue($arg, $opts, $match[2]); } } @@ -286,7 +280,7 @@ protected function _getParamValue($param, $paramOptions, $value) $cast = ! empty($paramOptions['cast']) ? $paramOptions['cast'] : 'string'; - return $this->_castValue($cast, $value); + return $this->castValue($cast, $value); } } @@ -299,7 +293,7 @@ protected function _getParamValue($param, $paramOptions, $value) * * @return bool|int|string */ - protected function _castValue($cast, $value) + protected function castValue($cast, $value) { switch ($cast) { case 'integer'; diff --git a/src/Munee/Response.php b/src/Munee/Response.php index bd022b9..1123fc8 100644 --- a/src/Munee/Response.php +++ b/src/Munee/Response.php @@ -20,16 +20,16 @@ class Response */ public $notModified = false; - /** - * @var Object - */ - protected $_assetType; - /** * @var object */ public $headerController; + /** + * @var Object + */ + protected $assetType; + /** * Constructor * @@ -39,7 +39,7 @@ class Response */ public function __construct($AssetType) { - // Must be a Sub-Class of \Munee\asset\Type + // Must be a Sub-Class of \Munee\Asset\Type $baseClass = '\\Munee\\asset\\Type'; if (! is_subclass_of($AssetType, $baseClass)) { throw new ErrorException( @@ -47,57 +47,42 @@ public function __construct($AssetType) ); } - $this->_assetType = $AssetType; - - $this->setHeaderController(new Asset\HeaderSetter); + $this->assetType = $AssetType; - $AssetType->_response = $this; + $AssetType->setResponse($this); } - /** - * Returns the Asset Types content. - * It will try and use Gzip to compress the content and save bandwidth - * - * @return string - */ - public function render() - { - $content = $this->_assetType->getContent(); - if (! $ret = ob_gzhandler($content, PHP_OUTPUT_HANDLER_START | PHP_OUTPUT_HANDLER_END)) { - $ret = $content; - } - - return $ret; - } - /** * Set controller for setting headers. * - * @param string $header_controller + * @param object $headerController * - * @return onject + * @return self * * @throws ErrorException */ - public function setHeaderController($header_controller) + public function setHeaderController($headerController) { - if(!($header_controller instanceof Asset\HeaderSetter)) - throw new ErrorException('Header controller must be an instance of HeaderSetter.'); - - $this->headerController = $header_controller; + if(! $headerController instanceof Asset\HeaderSetter) { + throw new ErrorException('Header controller must be an instance of Asset\HeaderSetter.'); + } + + $this->headerController = $headerController; return $this; } /** * Set Headers for Response - * + * + * @return self + * * @throws ErrorException */ public function setHeaders() { - $lastModifiedDate = $this->_assetType->getLastModifiedDate(); - $eTag = md5($lastModifiedDate . $this->_assetType->getContent()); + $lastModifiedDate = $this->assetType->getLastModifiedDate(); + $eTag = md5($lastModifiedDate . $this->assetType->getContent()); $checkModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false; $checkETag = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? @@ -114,7 +99,25 @@ public function setHeaders() $this->headerController->headerField('Cache-Control', 'must-revalidate'); $this->headerController->headerField('Last-Modified', gmdate('D, d M Y H:i:s', $lastModifiedDate) . ' GMT'); $this->headerController->headerField('ETag', $eTag); - $this->_assetType->getHeaders(); + $this->assetType->getHeaders(); + } + + return $this; + } + + /** + * Returns the Asset Types content. + * It will try and use Gzip to compress the content and save bandwidth + * + * @return string + */ + public function render() + { + $content = $this->assetType->getContent(); + if (! $ret = ob_gzhandler($content, PHP_OUTPUT_HANDLER_START | PHP_OUTPUT_HANDLER_END)) { + $ret = $content; } + + return $ret; } } \ No newline at end of file diff --git a/tests/Munee/Cases/ResponseTest.php b/tests/Munee/Cases/ResponseTest.php index 1efaa7b..70432c8 100644 --- a/tests/Munee/Cases/ResponseTest.php +++ b/tests/Munee/Cases/ResponseTest.php @@ -8,6 +8,7 @@ namespace Munee\Cases; +use Munee\Asset\HeaderSetter; use Munee\Response; use Munee\Mocks\MockAssetType; @@ -21,7 +22,7 @@ class ResponseTest extends \PHPUnit_Framework_TestCase /** * @var int */ - protected $_lastModifiedTime = 123456789; + protected $lastModifiedTime = 123456789; /** * Make sure the constructor is getting the correct Object passed to it. @@ -40,16 +41,17 @@ public function testConstruct() public function testNonCachedResponse() { $Response = new Response(new MockAssetType()); + $Response->setHeaderController(new HeaderSetter()); $Response->setHeaders(); $checkHeaders = array(); $checkHeaders['Cache-Control'] = 'must-revalidate'; $checkHeaders['Content-Type'] = 'text/test'; - $checkHeaders['Last-Modified'] = gmdate('D, d M Y H:i:s', $this->_lastModifiedTime) . ' GMT'; + $checkHeaders['Last-Modified'] = gmdate('D, d M Y H:i:s', $this->lastModifiedTime) . ' GMT'; // ETag is MD5 Hash of the content + last modified date $checkHeaders['ETag'] = '00403b660c8f869d9f50c429f6dceb72'; - $setHeaders = $this->_getHeaders(); + $setHeaders = $this->getHeaders(); $this->assertSame($checkHeaders['Cache-Control'], $setHeaders['Cache-Control']); unset($setHeaders['Cache-Control']); @@ -79,14 +81,15 @@ public function testNonCachedResponse() */ public function testCachedResponse() { - $_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s', $this->_lastModifiedTime) . ' GMT'; + $_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s', $this->lastModifiedTime) . ' GMT'; $_SERVER['HTTP_IF_NONE_MATCH'] = '00403b660c8f869d9f50c429f6dceb72'; $Response = new Response(new MockAssetType()); + $Response->setHeaderController(new HeaderSetter()); $Response->setHeaders(); $checkHeaders = array(); - $setHeaders = $this->_getHeaders(); + $setHeaders = $this->getHeaders(); $this->assertSame($checkHeaders, $setHeaders); @@ -105,21 +108,22 @@ public function testCachedResponse() */ public function testExpiredRequestResponse() { - $expiredTime = $this->_lastModifiedTime - 100; // removing 100 seconds + $expiredTime = $this->lastModifiedTime - 100; // removing 100 seconds $_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s', $expiredTime) . ' GMT'; $_SERVER['HTTP_IF_NONE_MATCH'] = '00000000000000000000000000000000'; $Response = new Response(new MockAssetType()); + $Response->setHeaderController(new HeaderSetter()); $Response->setHeaders(); $checkHeaders = array(); $checkHeaders['Cache-Control'] = 'must-revalidate'; $checkHeaders['Content-Type'] = 'text/test'; - $checkHeaders['Last-Modified'] = gmdate('D, d M Y H:i:s', $this->_lastModifiedTime) . ' GMT'; + $checkHeaders['Last-Modified'] = gmdate('D, d M Y H:i:s', $this->lastModifiedTime) . ' GMT'; // ETag is MD5 Hash of the content + last modified date $checkHeaders['ETag'] = '00403b660c8f869d9f50c429f6dceb72'; - $setHeaders = $this->_getHeaders(); + $setHeaders = $this->getHeaders(); $this->assertSame($checkHeaders['Cache-Control'], $setHeaders['Cache-Control']); unset($setHeaders['Cache-Control']); @@ -155,7 +159,7 @@ public function testRender() * * @return array */ - protected function _getHeaders() + protected function getHeaders() { $rawHeaders = xdebug_get_headers(); $ret = array(); diff --git a/tests/Munee/Mocks/MockAssetType.php b/tests/Munee/Mocks/MockAssetType.php index 40f7c75..a55e479 100644 --- a/tests/Munee/Mocks/MockAssetType.php +++ b/tests/Munee/Mocks/MockAssetType.php @@ -21,7 +21,7 @@ class MockAssetType extends Type * Don't want to do any constructing */ public function __construct() { - $this->_content = 'foo'; + $this->content = 'foo'; } /**