3 * Class Minify_Controller_Base
8 * Base class for Minify controller
10 * The controller class validates a request and uses it to create sources
11 * for minification and set options like contentType. It's also responsible
12 * for loading minifier code upon request.
15 * @author Stephen Clay <steve@mrclay.org>
17 abstract class Minify_Controller_Base {
20 * Setup controller sources and set an needed options for Minify::source
22 * You must override this method in your subclass controller to set
23 * $this->sources. If the request is NOT valid, make sure $this->sources
24 * is left an empty array. Then strip any controller-specific options from
25 * $options and return it. To serve files, $this->sources must be an array of
26 * Minify_Source objects.
28 * @param array $options controller and Minify options
30 * return array $options Minify::serve options
32 abstract public function setupSources($options);
35 * Get default Minify options for this controller.
37 * Override in subclass to change defaults
39 * @return array options for Minify
41 public function getDefaultMinifyOptions() {
44 ,'encodeOutput' => function_exists('gzdeflate')
45 ,'encodeMethod' => null // determine later
47 ,'minifierOptions' => array() // no minifier options
48 ,'contentTypeCharset' => 'utf-8'
49 ,'maxAge' => 1800 // 30 minutes
50 ,'rewriteCssUris' => true
51 ,'bubbleCssImports' => false
52 ,'quiet' => false // serve() will send headers and output
55 // if you override this, the response code MUST be directly after
57 ,'badRequestHeader' => 'HTTP/1.0 400 Bad Request'
59 // callback function to see/modify content of all sources
60 ,'postprocessor' => null
61 // file to require to load preprocessor
62 ,'postprocessorRequire' => null
67 * Get default minifiers for this controller.
69 * Override in subclass to change defaults
71 * @return array minifier callbacks for common types
73 public function getDefaultMinifers() {
74 $ret[Minify::TYPE_JS] = array('JSMin', 'minify');
75 $ret[Minify::TYPE_CSS] = array('Minify_CSS', 'minify');
76 $ret[Minify::TYPE_HTML] = array('Minify_HTML', 'minify');
81 * Load any code necessary to execute the given minifier callback.
83 * The controller is responsible for loading minification code on demand
84 * via this method. This built-in function will only load classes for
85 * static method callbacks where the class isn't already defined. It uses
86 * the PEAR convention, so, given array('Jimmy_Minifier', 'minCss'), this
87 * function will include 'Jimmy/Minifier.php'.
89 * If you need code loaded on demand and this doesn't suit you, you'll need
90 * to override this function in your subclass.
91 * @see Minify_Controller_Page::loadMinifier()
93 * @param callback $minifierCallback callback of minifier function
97 public function loadMinifier($minifierCallback)
99 if (is_array($minifierCallback)
100 && is_string($minifierCallback[0])
101 && !class_exists($minifierCallback[0], false)) {
103 require str_replace('_', '/', $minifierCallback[0]) . '.php';
108 * Is a user-given file within an allowable directory, existing,
109 * and having an extension js/css/html/txt ?
111 * This is a convenience function for controllers that have to accept
114 * @param string $file full file path (already processed by realpath())
116 * @param array $safeDirs directories where files are safe to serve. Files can also
117 * be in subdirectories of these directories.
119 * @return bool file is safe
121 public static function _fileIsSafe($file, $safeDirs)
124 foreach ((array)$safeDirs as $safeDir) {
125 if (strpos($file, $safeDir) === 0) {
130 $base = basename($file);
131 if (! $pathOk || ! is_file($file) || $base[0] === '.') {
134 list($revExt) = explode('.', strrev($base));
135 return in_array(strrev($revExt), array('js', 'css', 'html', 'txt'));
139 * @var array instances of Minify_Source, which provide content and
140 * any individual minification needs.
144 public $sources = array();
147 * Mix in default controller options with user-given options
149 * @param array $options user options
151 * @return array mixed options
153 public final function mixInDefaultOptions($options)
156 $this->getDefaultMinifyOptions(), $options
158 if (! isset($options['minifiers'])) {
159 $options['minifiers'] = array();
161 $ret['minifiers'] = array_merge(
162 $this->getDefaultMinifers(), $options['minifiers']
168 * Analyze sources (if there are any) and set $options 'contentType'
169 * and 'lastModifiedTime' if they already aren't.
171 * @param array $options options for Minify
173 * @return array options for Minify
175 public final function analyzeSources($options = array())
177 if ($this->sources) {
178 if (! isset($options['contentType'])) {
179 $options['contentType'] = Minify_Source::getContentType($this->sources);
181 // last modified is needed for caching, even if setExpires is set
182 if (! isset($options['lastModifiedTime'])) {
184 foreach ($this->sources as $source) {
185 $max = max($source->lastModified, $max);
187 $options['lastModifiedTime'] = $max;
194 * Send message to the Minify logger
198 protected function log($msg) {
199 require_once 'Minify/Logger.php';
200 Minify_Logger::log($msg);