summaryrefslogtreecommitdiff
path: root/min/lib/Minify/Controller/Page.php
blob: fa4599abd9e1d721436df7e01bea3553c9ddb698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
 * Class Minify_Controller_Page  
 * @package Minify
 */

require_once 'Minify/Controller/Base.php';

/**
 * Controller class for serving a single HTML page
 * 
 * @link http://code.google.com/p/minify/source/browse/trunk/web/examples/1/index.php#59
 * @package Minify
 * @author Stephen Clay <steve@mrclay.org>
 */
class Minify_Controller_Page extends Minify_Controller_Base {
    
    /**
     * Set up source of HTML content
     * 
     * @param array $options controller and Minify options
     * @return array Minify options
     * 
     * Controller options:
     * 
     * 'content': (required) HTML markup
     * 
     * 'id': (required) id of page (string for use in server-side caching)
     * 
     * 'lastModifiedTime': timestamp of when this content changed. This
     * is recommended to allow both server and client-side caching.
     * 
     * 'minifyAll': should all CSS and Javascript blocks be individually 
     * minified? (default false) 
     *
     * @todo Add 'file' option to read HTML file.
     */
    public function setupSources($options) {
        if (isset($options['file'])) {
            $sourceSpec = array(
                'filepath' => $options['file']
            );
        } else {
            // strip controller options
            $sourceSpec = array(
                'content' => $options['content']
                ,'id' => $options['id']
            );
            unset($options['content'], $options['id']);
        }
        if (isset($options['minifyAll'])) {
            // this will be the 2nd argument passed to Minify_HTML::minify()
            $sourceSpec['minifyOptions'] = array(
                'cssMinifier' => array('Minify_CSS', 'minify')
                ,'jsMinifier' => array('JSMin', 'minify')
            );
            $this->_loadCssJsMinifiers = true;
            unset($options['minifyAll']);
        }
        $this->sources[] = new Minify_Source($sourceSpec);
        
        $options['contentType'] = Minify::TYPE_HTML;
        return $options;
    }
    
    protected $_loadCssJsMinifiers = false;
    
    /**
     * @see Minify_Controller_Base::loadMinifier()
     */
    public function loadMinifier($minifierCallback)
    {
        if ($this->_loadCssJsMinifiers) {
            // Minify will not call for these so we must manually load
            // them when Minify/HTML.php is called for.
            require_once 'Minify/CSS.php';
            require_once 'JSMin.php';
        }
        parent::loadMinifier($minifierCallback); // load Minify/HTML.php
    }
}