/** * Make sure we can un-register extensions */ public function testUnRegisterExtension() { Registry::unRegister('bar'); $checkExtensions = array('foo'); $supportedExtensions = Registry::getSupportedExtensions('foo'); $this->assertSame($checkExtensions, $supportedExtensions); }
/** * Process files with Munee library * http://mun.ee * * @param string $files * @param array $options Array with options for Munee class * * @return string */ public function Munee($files, $options = array()) { if (!defined('WEBROOT')) { define('WEBROOT', MODX_BASE_PATH); } if (!defined('MUNEE_CACHE')) { define('MUNEE_CACHE', $this->getTmpDir()); } require_once $this->config['corePath'] . 'munee/vendor/autoload.php'; try { $Request = new \Munee\Request($options); $Request->setFiles($files); foreach ($options as $k => $v) { $Request->setRawParam($k, $v); } $Request->init(); /** @var \Munee\Asset\Type $AssetType */ $AssetType = \Munee\Asset\Registry::getClass($Request); $AssetType->init(); if (!empty($options['setHeaders'])) { if (isset($options['headerController']) && $options['headerController'] instanceof \Munee\Asset\HeaderSetter) { $headerController = $options['headerController']; } else { $headerController = new \Munee\Asset\HeaderSetter(); } /** @var \Munee\Response $Response */ $Response = new \Munee\Response($AssetType); $Response->setHeaderController($headerController); $Response->setHeaders(isset($options['maxAge']) ? $options['maxAge'] : 0); } return $AssetType->getContent(); } catch (\Munee\ErrorException $e) { $error = $e->getMessage(); if ($prev = $e->getPrevious()) { $error .= ': ' . $e->getPrevious()->getMessage(); } $this->modx->log(modX::LOG_LEVEL_ERROR, '[MinifyX] ' . $error); return ''; } }
// Define the cache path defined('MUNEE_CACHE') || define('MUNEE_CACHE', MUNEE_FOLDER . DS . 'cache'); // Define default character encoding defined('MUNEE_CHARACTER_ENCODING') || define('MUNEE_CHARACTER_ENCODING', 'UTF-8'); // Are we using Munee with URL Rewrite (.htaccess file)? $requestUri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; defined('MUNEE_USING_URL_REWRITE') || define('MUNEE_USING_URL_REWRITE', strpos($requestUri, 'files=') === false); // Munee dispatcher file if not using URL Rewrite defined('MUNEE_DISPATCHER_FILE') || define('MUNEE_DISPATCHER_FILE', !MUNEE_USING_URL_REWRITE ? $_SERVER['SCRIPT_NAME'] : ''); // If mbstring is installed, set the encoding default if (function_exists('mb_internal_encoding')) { mb_internal_encoding(MUNEE_CHARACTER_ENCODING); } /** * Register the CSS Asset Class with the extensions .css, .less, and .scss */ Registry::register(array('css', 'less', 'scss'), function (\Munee\Request $Request) { return new \Munee\Asset\Type\Css($Request); }); /** * Register the JavaScript Asset Class with the extension .js */ Registry::register(array('js', 'coffee'), function (\Munee\Request $Request) { return new \Munee\Asset\Type\JavaScript($Request); }); /** * Register the Image Asset Class with the extensions .jpg, .jpeg, .gif, and .png */ Registry::register(array('jpg', 'jpeg', 'gif', 'png'), function (\Munee\Request $Request) { return new \Munee\Asset\Type\Image($Request); });
/** * Parses the $rawFiles and does sanity checks * * @throws ErrorException * @throws Asset\NotFoundException */ public function init() { 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->rawFiles)) { $this->rawFiles = substr($this->rawFiles, 7); $this->setRawParam('minify', 'true'); } $this->ext = strtolower(pathinfo($this->rawFiles, PATHINFO_EXTENSION)); $supportedExtensions = Registry::getSupportedExtensions($this->ext); // Suppressing errors because Exceptions thrown in the callback cause Warnings. $webroot = $this->webroot; $this->files = @array_map(function ($v) use($supportedExtensions, $webroot) { // Make sure all the file extensions are supported if (!in_array(strtolower(pathinfo($v, PATHINFO_EXTENSION)), $supportedExtensions)) { throw new ErrorException('All requested files need to be: ' . implode(', ', $supportedExtensions)); } // Strip any parent directory slugs (../) - loop through until they are all gone $count = 1; while ($count > 0) { $v = preg_replace('%(/\\.\\.?|\\.\\.?/)%', '', $v, -1, $count); // If there is no slash prefix, add it back in if (substr($v, 0, 1) != '/') { $v = '/' . $v; } } // Remove sub-folder if in the path, it shouldn't be there. $v = str_replace(SUB_FOLDER, '', $v); return $webroot . $v; }, explode(',', $this->rawFiles)); }