Files.php 2.0 KB

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