Groups.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Class Minify_Controller_Groups
  4. * @package Minify
  5. */
  6. require_once 'Minify/Controller/Base.php';
  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. * Set up groups of files as sources
  32. *
  33. * @param array $options controller and Minify options
  34. *
  35. * 'groups': (required) array mapping PATH_INFO strings to arrays
  36. * of complete file paths. @see Minify_Controller_Groups
  37. *
  38. * @return array Minify options
  39. */
  40. public function setupSources($options) {
  41. // strip controller options
  42. $groups = $options['groups'];
  43. unset($options['groups']);
  44. // mod_fcgid places PATH_INFO in ORIG_PATH_INFO
  45. $pi = isset($_SERVER['ORIG_PATH_INFO'])
  46. ? substr($_SERVER['ORIG_PATH_INFO'], 1)
  47. : (isset($_SERVER['PATH_INFO'])
  48. ? substr($_SERVER['PATH_INFO'], 1)
  49. : false
  50. );
  51. if (false === $pi || ! isset($groups[$pi])) {
  52. // no PATH_INFO or not a valid group
  53. $this->log("Missing PATH_INFO or no group set for \"$pi\"");
  54. return $options;
  55. }
  56. $sources = array();
  57. $files = $groups[$pi];
  58. // if $files is a single object, casting will break it
  59. if (is_object($files)) {
  60. $files = array($files);
  61. } elseif (! is_array($files)) {
  62. $files = (array)$files;
  63. }
  64. foreach ($files as $file) {
  65. if ($file instanceof Minify_Source) {
  66. $sources[] = $file;
  67. continue;
  68. }
  69. if (0 === strpos($file, '//')) {
  70. $file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
  71. }
  72. $realPath = realpath($file);
  73. if (is_file($realPath)) {
  74. $sources[] = new Minify_Source(array(
  75. 'filepath' => $realPath
  76. ));
  77. } else {
  78. $this->log("The path \"{$file}\" could not be found (or was not a file)");
  79. return $options;
  80. }
  81. }
  82. if ($sources) {
  83. $this->sources = $sources;
  84. }
  85. return $options;
  86. }
  87. }