Files.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Class Minify_Controller_Files
  4. * @package Minify
  5. */
  6. require_once 'Minify/Controller/Base.php';
  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. * Set up file sources
  30. *
  31. * @param array $options controller and Minify options
  32. * @return array Minify options
  33. *
  34. * Controller options:
  35. *
  36. * 'files': (required) array of complete file paths, or a single path
  37. */
  38. public function setupSources($options) {
  39. // strip controller options
  40. $files = $options['files'];
  41. // if $files is a single object, casting will break it
  42. if (is_object($files)) {
  43. $files = array($files);
  44. } elseif (! is_array($files)) {
  45. $files = (array)$files;
  46. }
  47. unset($options['files']);
  48. $sources = array();
  49. foreach ($files as $file) {
  50. if ($file instanceof Minify_Source) {
  51. $sources[] = $file;
  52. continue;
  53. }
  54. if (0 === strpos($file, '//')) {
  55. $file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
  56. }
  57. $realPath = realpath($file);
  58. if (is_file($realPath)) {
  59. $sources[] = new Minify_Source(array(
  60. 'filepath' => $realPath
  61. ));
  62. } else {
  63. $this->log("The path \"{$file}\" could not be found (or was not a file)");
  64. return $options;
  65. }
  66. }
  67. if ($sources) {
  68. $this->sources = $sources;
  69. }
  70. return $options;
  71. }
  72. }