Groups.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Class Minify_Controller_Groups
  4. * @package Minify
  5. */
  6. /**
  7. * Controller class for serving predetermined groups of minimized sets, selected
  8. * by PATH_INFO
  9. *
  10. * <code>
  11. * Minify::serve('Groups', array(
  12. * 'groups' => array(
  13. * 'css' => array('//css/type.css', '//css/layout.css')
  14. * ,'js' => array('//js/jquery.js', '//js/site.js')
  15. * )
  16. * ));
  17. * </code>
  18. *
  19. * If the above code were placed in /serve.php, it would enable the URLs
  20. * /serve.php/js and /serve.php/css
  21. *
  22. * As a shortcut, the controller will replace "//" at the beginning
  23. * of a filename with $_SERVER['DOCUMENT_ROOT'] . '/'.
  24. *
  25. * @package Minify
  26. * @author Stephen Clay <steve@mrclay.org>
  27. */
  28. class Minify_Controller_Groups extends Minify_Controller_Base {
  29. /**
  30. * Set up groups of files as sources
  31. *
  32. * @param array $options controller and Minify options
  33. *
  34. * 'groups': (required) array mapping PATH_INFO strings to arrays
  35. * of complete file paths. @see Minify_Controller_Groups
  36. *
  37. * @return array Minify options
  38. */
  39. public function setupSources($options) {
  40. // strip controller options
  41. $groups = $options['groups'];
  42. unset($options['groups']);
  43. // mod_fcgid places PATH_INFO in ORIG_PATH_INFO
  44. $pi = isset($_SERVER['ORIG_PATH_INFO'])
  45. ? substr($_SERVER['ORIG_PATH_INFO'], 1)
  46. : (isset($_SERVER['PATH_INFO'])
  47. ? substr($_SERVER['PATH_INFO'], 1)
  48. : false
  49. );
  50. if (false === $pi || ! isset($groups[$pi])) {
  51. // no PATH_INFO or not a valid group
  52. $this->log("Missing PATH_INFO or no group set for \"$pi\"");
  53. return $options;
  54. }
  55. $sources = array();
  56. $files = $groups[$pi];
  57. // if $files is a single object, casting will break it
  58. if (is_object($files)) {
  59. $files = array($files);
  60. } elseif (! is_array($files)) {
  61. $files = (array)$files;
  62. }
  63. foreach ($files as $file) {
  64. if ($file instanceof Minify_Source) {
  65. $sources[] = $file;
  66. continue;
  67. }
  68. if (0 === strpos($file, '//')) {
  69. $file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
  70. }
  71. $realPath = realpath($file);
  72. if (is_file($realPath)) {
  73. $sources[] = new Minify_Source(array(
  74. 'filepath' => $realPath
  75. ));
  76. } else {
  77. $this->log("The path \"{$file}\" could not be found (or was not a file)");
  78. return $options;
  79. }
  80. }
  81. if ($sources) {
  82. $this->sources = $sources;
  83. }
  84. return $options;
  85. }
  86. }