1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-13 01:24:51 +02:00

V1.9.0 overhaul

This commit is contained in:
Steve Clay
2008-02-28 18:42:56 +00:00
parent 5527771acf
commit 0a939d4f91
65 changed files with 8081 additions and 608 deletions

View File

@@ -0,0 +1,129 @@
<?php
/**
* Base class for Minify controller
*
* The controller class validates a request and uses it to create sources
* for minification and set options like contentType. It's also responsible
* for loading minifier code upon request.
*/
class Minify_Controller_Base {
/**
* @var array instances of Minify_Source, which provide content and
* any individual minification needs.
*
* @see Minify_Source
*/
public $sources = array();
/**
* @var array options to be read by read by Minify
*
* Any unspecified options will use the default values.
*
* 'minifiers': this is an array with content-types as keys and callbacks as
* values. Specify a custom minifier by setting this option. E.g.:
* $this->options['minifiers']['application/x-javascript'] = 'myJsPacker';
* Note that, when providing your own minifier, the controller must be able
* to load its code on demand. @see loadMinifier()
*
* 'perType' : this is an array of options to send to a particular content
* type minifier by using the content-type as key. E.g. To send the CSS
* minifier an option: $options['perType']['text/css']['foo'] = 'bar';
* When the CSS minifier is called, the 2nd argument will be
* array('foo' => 'bar').
*
* 'isPublic' : send "public" instead of "private" in Cache-Control headers,
* allowing shared caches to cache the output. (default true)
*
* 'encodeOutput' : to disable content encoding, set this to false
*
* 'encodeMethod' : generally you should let this be determined by
* HTTP_Encoder (the default null), but you can force a particular encoding
* to be returned, by setting this to 'gzip', 'deflate', 'compress', or ''
* (no encoding)
*
* 'encodeLevel' : level of encoding compression (0 to 9, default 9)
*
* 'contentTypeCharset' : if given, this will be appended to the Content-Type
* header sent, useful mainly for HTML docs.
*
* 'cacheUntil' : set this to a timestamp or GMT date to have Minify send
* an HTTP Expires header instead of checking for conditional GET.
* E.g. (time() + 86400 * 365) for 1yr (default null)
* This has nothing to do with server-side caching.
*
*/
public $options = array();
/**
* @var bool was the user request valid
*
* This must be explicity be set to true to process the request. This should
* be done by the child class constructor.
*/
public $requestIsValid = false;
/**
* Parent constructor for a controller class
*
* Generally you'll call this at the end of your child class constructor:
* <code>
* parent::__construct($sources, $options);
* </code>
*
* This function sets the sources and determines the 'contentType' and
* 'lastModifiedTime', if not given.
*
* If no sources are provided, $this->requestIsValid will be set to false.
*
* @param array $sources array of instances of Minify_Source
*
* @param array $options
*
* @return null
*/
public function __construct($sources, $options = array()) {
if (empty($sources)) {
$this->requestIsValid = false;
}
$this->sources = $sources;
if (! isset($options['contentType'])) {
$options['contentType'] = Minify_Source::getContentType($this->sources);
}
// last modified is needed for caching, even if cacheUntil is set
if (! isset($options['lastModifiedTime'])) {
$max = 0;
foreach ($sources as $source) {
$max = max($source->lastModified, $max);
}
$options['lastModifiedTime'] = $max;
}
$this->options = $options;
}
/**
* Load any code necessary to execute the given minifier callback.
*
* The controller is responsible for loading minification code on demand
* via this method. This built-in function will only load classes for
* static method callbacks where the class isn't already defined. It uses
* the PEAR convention, so, given array('Jimmy_Minifier', 'minCss'), this
* function will include 'Jimmy/Minifier.php'
*
* If you need code loaded on demand and this doesn't suit you, you'll need
* to override this function by extending the class.
*
* @return null
*/
public function loadMinifier($minifierCallback)
{
if (is_array($minifierCallback)
&& is_string($minifierCallback[0])
&& !class_exists($minifierCallback[0], false)) {
require str_replace('_', '/', $minifierCallback[0]) . '.php';
}
}
}

View File

@@ -0,0 +1,46 @@
<?php
require_once 'Minify/Controller/Base.php';
/**
* Controller class for minifying a set of files
*
* E.g. the following would serve minified Javascript for a site
* <code>
* $dr = $_SERVER['DOCUMENT_ROOT'];
* Minify::minify('Files', array(
* $dr . '/js/jquery.js'
* ,$dr . '/js/plugins.js'
* ,$dr . '/js/site.js'
* ));
* </code>
*
*/
class Minify_Controller_Files extends Minify_Controller_Base {
/**
* @param array $spec array or full paths of files to be minified
*
* @param array $options optional options to pass to Minify
*
* @return null
*/
public function __construct($spec, $options = array()) {
$sources = array();
foreach ($spec as $file) {
$file = realpath($file);
if (file_exists($file)) {
$sources[] = new Minify_Source(array(
'filepath' => $file
));
} else {
return;
}
}
if ($sources) {
$this->requestIsValid = true;
}
parent::__construct($sources, $options);
}
}

View File

@@ -0,0 +1,59 @@
<?php
require_once 'Minify/Controller/Base.php';
/**
* Controller class for serving predetermined groups of minimized sets, selected
* by PATH_INFO
*
* <code>
* $dr = $_SERVER['DOCUMENT_ROOT'];
* Minify::minify('Groups', array(
* 'css' => array(
* $dr . '/css/type.css'
* ,$dr . '/css/layout.css'
* )
* ,'js' => array(
* $dr . '/js/jquery.js'
* ,$dr . '/js/plugins.js'
* ,$dr . '/js/site.js'
* )
* ));
* </code>
*
* If the above code were placed in /serve.php, it would enable the URLs
* /serve.php/js and /serve.php/css
*/
class Minify_Controller_Groups extends Minify_Controller_Base {
/**
* @param array $spec associative array of keys to arrays of file paths.
*
* @param array $options optional options to pass to Minify
*
* @return null
*/
public function __construct($spec, $options = array()) {
$pi = substr($_SERVER['PATH_INFO'], 1);
if (! isset($spec[$pi])) {
// not a valid group
return;
}
$sources = array();
foreach ($spec[$pi] as $file) {
$file = realpath($file);
if (file_exists($file)) {
$sources[] = new Minify_Source(array(
'filepath' => $file
));
} else {
return;
}
}
if ($sources) {
$this->requestIsValid = true;
}
parent::__construct($sources, $options);
}
}

View File

@@ -0,0 +1,61 @@
<?php
require_once 'Minify/Controller/Base.php';
/**
* Controller class for minifying a set of files
*
* E.g. the following would serve minified Javascript for a site
* <code>
* $dr = $_SERVER['DOCUMENT_ROOT'];
* Minify::minify('Files', array(
* $dr . '/js/jquery.js'
* ,$dr . '/js/plugins.js'
* ,$dr . '/js/site.js'
* ));
* </code>
*
*/
class Minify_Controller_Page extends Minify_Controller_Base {
/**
*
*
* @param array $options optional options to pass to Minify
*
* @return null
*/
public function __construct($spec, $options = array()) {
$sourceSpec = array(
'content' => $spec['content']
,'id' => $spec['id']
,'minifier' => array('Minify_HTML', 'minify')
);
if (isset($spec['minifyAll'])) {
$sourceSpec['minifyOptions'] = array(
'cssMinifier' => array('Minify_CSS', 'minify')
,'jsMinifier' => array('Minify_Javascript', 'minify')
);
$this->_loadCssJsMinifiers = true;
}
$sources[] = new Minify_Source($sourceSpec);
if (isset($spec['lastModifiedTime'])) {
$options['lastModifiedTime'] = $spec['lastModifiedTime'];
}
$options['contentType'] = 'text/html';
$this->requestIsValid = true;
parent::__construct($sources, $options);
}
private $_loadCssJsMinifiers = false;
public function loadMinifier($minifierCallback)
{
if ($this->_loadCssJsMinifiers) {
require 'Minify/CSS.php';
require 'Minify/Javascript.php';
}
parent::loadMinifier($minifierCallback);
}
}