| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Minify_Controller_Files |
| 4 |
* @package Minify |
| 5 |
*/ |
| 6 |
|
| 7 |
use Monolog\Logger; |
| 8 |
|
| 9 |
/** |
| 10 |
* Controller class for minifying a set of files |
| 11 |
* |
| 12 |
* E.g. the following would serve the minified Javascript for a site |
| 13 |
* <code> |
| 14 |
* $options = [ |
| 15 |
* 'checkAllowDirs' => false, // allow files to be anywhere |
| 16 |
* ]; |
| 17 |
* $sourceFactory = new Minify_Source_Factory($env, $options, $cache); |
| 18 |
* $controller = new Minify_Controller_Files($env, $sourceFactory); |
| 19 |
* $minify->serve($controller, [ |
| 20 |
* 'files' => [ |
| 21 |
* '//js/jquery.js', |
| 22 |
* '//js/plugins.js', |
| 23 |
* '/home/username/file.js', |
| 24 |
* ], |
| 25 |
* ]); |
| 26 |
* </code> |
| 27 |
* |
| 28 |
* @package Minify |
| 29 |
* @author Stephen Clay <steve@mrclay.org> |
| 30 |
*/ |
| 31 |
class Minify_Controller_Files extends Minify_Controller_Base |
| 32 |
{ |
| 33 |
|
| 34 |
/** |
| 35 |
* Set up file sources |
| 36 |
* |
| 37 |
* @param array $options controller and Minify options |
| 38 |
* @return Minify_ServeConfiguration |
| 39 |
* |
| 40 |
* Controller options: |
| 41 |
* |
| 42 |
* 'files': (required) array of complete file paths, or a single path |
| 43 |
*/ |
| 44 |
public function createConfiguration(array $options) |
| 45 |
{ |
| 46 |
// strip controller options |
| 47 |
|
| 48 |
$files = $options['files']; |
| 49 |
// if $files is a single object, casting will break it |
| 50 |
if (is_object($files)) { |
| 51 |
$files = array($files); |
| 52 |
} elseif (! is_array($files)) { |
| 53 |
$files = (array)$files; |
| 54 |
} |
| 55 |
unset($options['files']); |
| 56 |
|
| 57 |
$sources = array(); |
| 58 |
foreach ($files as $file) { |
| 59 |
try { |
| 60 |
$sources[] = $this->sourceFactory->makeSource($file); |
| 61 |
} catch (Minify_Source_FactoryException $e) { |
| 62 |
$this->logger->error($e->getMessage()); |
| 63 |
|
| 64 |
return new Minify_ServeConfiguration($options); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return new Minify_ServeConfiguration($options, $sources); |
| 69 |
} |
| 70 |
} |
| 71 |
|