initial commit
[namibia] / public / min / lib / Minify / Controller / Files.php
1 <?php
2 /**
3  * Class Minify_Controller_Files  
4  * @package Minify
5  */
6
7 /**
8  * Controller class for minifying a set of files
9  * 
10  * E.g. the following would serve the minified Javascript for a site
11  * <code>
12  * Minify::serve('Files', array(
13  *     'files' => array(
14  *         '//js/jquery.js'
15  *         ,'//js/plugins.js'
16  *         ,'/home/username/file.js'
17  *     )
18  * ));
19  * </code>
20  * 
21  * As a shortcut, the controller will replace "//" at the beginning
22  * of a filename with $_SERVER['DOCUMENT_ROOT'] . '/'.
23  *
24  * @package Minify
25  * @author Stephen Clay <steve@mrclay.org>
26  */
27 class Minify_Controller_Files extends Minify_Controller_Base {
28     
29     /**
30      * Set up file sources
31      * 
32      * @param array $options controller and Minify options
33      * @return array Minify options
34      * 
35      * Controller options:
36      * 
37      * 'files': (required) array of complete file paths, or a single path
38      */
39     public function setupSources($options) {
40         // strip controller options
41         
42         $files = $options['files'];
43         // if $files is a single object, casting will break it
44         if (is_object($files)) {
45             $files = array($files);
46         } elseif (! is_array($files)) {
47             $files = (array)$files;
48         }
49         unset($options['files']);
50         
51         $sources = array();
52         foreach ($files as $file) {
53             if ($file instanceof Minify_Source) {
54                 $sources[] = $file;
55                 continue;
56             }
57             if (0 === strpos($file, '//')) {
58                 $file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
59             }
60             $realPath = realpath($file);
61             if (is_file($realPath)) {
62                 $sources[] = new Minify_Source(array(
63                     'filepath' => $realPath
64                 ));    
65             } else {
66                 $this->log("The path \"{$file}\" could not be found (or was not a file)");
67                 return $options;
68             }
69         }
70         if ($sources) {
71             $this->sources = $sources;
72         }
73         return $options;
74     }
75 }
76