env = $env; $this->options = array_merge(array( 'noMinPattern' => '@[-\\.]min\\.(?:[a-zA-Z]+)$@i', // matched against basename 'fileChecker' => array($this, 'checkIsFile'), 'resolveDocRoot' => true, 'checkAllowDirs' => true, 'allowDirs' => array('//'), 'uploaderHoursBehind' => 0, ), $options); // resolve // in allowDirs $docRoot = $env->getDocRoot(); foreach ($this->options['allowDirs'] as $i => $dir) { if (0 === strpos($dir, '//')) { $this->options['allowDirs'][$i] = $docRoot . substr($dir, 1); } } if ($this->options['fileChecker'] && !is_callable($this->options['fileChecker'])) { throw new InvalidArgumentException("fileChecker option is not callable"); } $this->setHandler('~\.less$~i', function ($spec) use ($cache) { return new Minify_LessCssSource($spec, $cache); }); $this->setHandler('~\.scss~i', function ($spec) use ($cache) { return new Minify_ScssCssSource($spec, $cache); }); $this->setHandler('~\.(js|css)$~i', function ($spec) { return new Minify_Source($spec); }); } /** * @param string $basenamePattern A pattern tested against basename. E.g. "~\.css$~" * @param callable $handler Function that recieves a $spec array and returns a Minify_SourceInterface */ public function setHandler($basenamePattern, $handler) { $this->handlers[$basenamePattern] = $handler; } /** * @param string $file * @return string * * @throws Minify_Source_FactoryException */ public function checkIsFile($file) { $realpath = realpath($file); if (!$realpath) { throw new Minify_Source_FactoryException("File failed realpath(): $file"); } $basename = basename($file); if (0 === strpos($basename, '.')) { throw new Minify_Source_FactoryException("Filename starts with period (may be hidden): $basename"); } if (!is_file($realpath) || !is_readable($realpath)) { throw new Minify_Source_FactoryException("Not a file or isn't readable: $file"); } return $realpath; } /** * @param mixed $spec * * @return Minify_SourceInterface * * @throws Minify_Source_FactoryException */ public function makeSource($spec) { if (is_string($spec)) { $spec = array( 'filepath' => $spec, ); } elseif ($spec instanceof Minify_SourceInterface) { return $spec; } $source = null; if (empty($spec['filepath'])) { // not much we can check return new Minify_Source($spec); } if ($this->options['resolveDocRoot'] && 0 === strpos($spec['filepath'], '//')) { $spec['filepath'] = $this->env->getDocRoot() . substr($spec['filepath'], 1); } if (!empty($this->options['fileChecker'])) { $spec['filepath'] = call_user_func($this->options['fileChecker'], $spec['filepath']); } if ($this->options['checkAllowDirs']) { $allowDirs = (array)$this->options['allowDirs']; $inAllowedDir = false; $filePath = $this->env->normalizePath($spec['filepath']); foreach ($allowDirs as $allowDir) { if (strpos($filePath, $this->env->normalizePath($allowDir)) === 0) { $inAllowedDir = true; } } if (!$inAllowedDir) { $allowDirsStr = implode(';', $allowDirs); throw new Minify_Source_FactoryException("File '{$spec['filepath']}' is outside \$allowDirs " . "($allowDirsStr). If the path is resolved via an alias/symlink, look into the " . "\$min_symlinks option."); } } $basename = basename($spec['filepath']); if ($this->options['noMinPattern'] && preg_match($this->options['noMinPattern'], $basename)) { if (preg_match('~\.(css|less)$~i', $basename)) { $spec['minifyOptions']['compress'] = false; // we still want URI rewriting to work for CSS } else { $spec['minifier'] = 'Minify::nullMinifier'; } } $hoursBehind = $this->options['uploaderHoursBehind']; if ($hoursBehind != 0) { $spec['uploaderHoursBehind'] = $hoursBehind; } foreach ($this->handlers as $basenamePattern => $handler) { if (preg_match($basenamePattern, $basename)) { $source = call_user_func($handler, $spec); break; } } if (!$source) { throw new Minify_Source_FactoryException("Handler not found for file: $basename"); } return $source; } }