initial commit
[namibia] / public / min / lib / Minify / Controller / Groups.php
1 <?php
2 /**
3  * Class Minify_Controller_Groups  
4  * @package Minify
5  */
6
7 /**
8  * Controller class for serving predetermined groups of minimized sets, selected
9  * by PATH_INFO
10  * 
11  * <code>
12  * Minify::serve('Groups', array( 
13  *     'groups' => array(
14  *         'css' => array('//css/type.css', '//css/layout.css')
15  *        ,'js' => array('//js/jquery.js', '//js/site.js')
16  *     )
17  * ));
18  * </code>
19  * 
20  * If the above code were placed in /serve.php, it would enable the URLs
21  * /serve.php/js and /serve.php/css
22  * 
23  * As a shortcut, the controller will replace "//" at the beginning
24  * of a filename with $_SERVER['DOCUMENT_ROOT'] . '/'.
25  * 
26  * @package Minify
27  * @author Stephen Clay <steve@mrclay.org>
28  */
29 class Minify_Controller_Groups extends Minify_Controller_Base {
30     
31     /**
32      * Set up groups of files as sources
33      * 
34      * @param array $options controller and Minify options
35      *
36      * 'groups': (required) array mapping PATH_INFO strings to arrays
37      * of complete file paths. @see Minify_Controller_Groups
38      *
39      * @return array Minify options
40      */
41     public function setupSources($options) {
42         // strip controller options
43         $groups = $options['groups'];
44         unset($options['groups']);
45         
46         // mod_fcgid places PATH_INFO in ORIG_PATH_INFO
47         $pi = isset($_SERVER['ORIG_PATH_INFO'])
48             ? substr($_SERVER['ORIG_PATH_INFO'], 1) 
49             : (isset($_SERVER['PATH_INFO'])
50                 ? substr($_SERVER['PATH_INFO'], 1) 
51                 : false
52             );
53         if (false === $pi || ! isset($groups[$pi])) {
54             // no PATH_INFO or not a valid group
55             $this->log("Missing PATH_INFO or no group set for \"$pi\"");
56             return $options;
57         }
58         $sources = array();
59         
60         $files = $groups[$pi];
61         // if $files is a single object, casting will break it
62         if (is_object($files)) {
63             $files = array($files);
64         } elseif (! is_array($files)) {
65             $files = (array)$files;
66         }
67         foreach ($files as $file) {
68             if ($file instanceof Minify_Source) {
69                 $sources[] = $file;
70                 continue;
71             }
72             if (0 === strpos($file, '//')) {
73                 $file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
74             }
75             $realPath = realpath($file);
76             if (is_file($realPath)) {
77                 $sources[] = new Minify_Source(array(
78                     'filepath' => $realPath
79                 ));    
80             } else {
81                 $this->log("The path \"{$file}\" could not be found (or was not a file)");
82                 return $options;
83             }
84         }
85         if ($sources) {
86             $this->sources = $sources;
87         }
88         return $options;
89     }
90 }
91