Page.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Class Minify_Controller_Page
  4. * @package Minify
  5. */
  6. require_once 'Minify/Controller/Base.php';
  7. /**
  8. * Controller class for serving a single HTML page
  9. *
  10. * @link http://code.google.com/p/minify/source/browse/trunk/web/examples/1/index.php#59
  11. * @package Minify
  12. * @author Stephen Clay <steve@mrclay.org>
  13. */
  14. class Minify_Controller_Page extends Minify_Controller_Base {
  15. /**
  16. * Set up source of HTML content
  17. *
  18. * @param array $options controller and Minify options
  19. * @return array Minify options
  20. *
  21. * Controller options:
  22. *
  23. * 'content': (required) HTML markup
  24. *
  25. * 'id': (required) id of page (string for use in server-side caching)
  26. *
  27. * 'lastModifiedTime': timestamp of when this content changed. This
  28. * is recommended to allow both server and client-side caching.
  29. *
  30. * 'minifyAll': should all CSS and Javascript blocks be individually
  31. * minified? (default false)
  32. *
  33. * @todo Add 'file' option to read HTML file.
  34. */
  35. public function setupSources($options) {
  36. if (isset($options['file'])) {
  37. $sourceSpec = array(
  38. 'filepath' => $options['file']
  39. );
  40. $f = $options['file'];
  41. } else {
  42. // strip controller options
  43. $sourceSpec = array(
  44. 'content' => $options['content']
  45. ,'id' => $options['id']
  46. );
  47. $f = $options['id'];
  48. unset($options['content'], $options['id']);
  49. }
  50. // something like "builder,index.php" or "directory,file.html"
  51. $this->selectionId = strtr(substr($f, 1 + strlen(dirname(dirname($f)))), '/\\', ',,');
  52. if (isset($options['minifyAll'])) {
  53. // this will be the 2nd argument passed to Minify_HTML::minify()
  54. $sourceSpec['minifyOptions'] = array(
  55. 'cssMinifier' => array('Minify_CSS', 'minify')
  56. ,'jsMinifier' => array('JSMin', 'minify')
  57. );
  58. $this->_loadCssJsMinifiers = true;
  59. unset($options['minifyAll']);
  60. }
  61. $this->sources[] = new Minify_Source($sourceSpec);
  62. $options['contentType'] = Minify::TYPE_HTML;
  63. return $options;
  64. }
  65. protected $_loadCssJsMinifiers = false;
  66. /**
  67. * @see Minify_Controller_Base::loadMinifier()
  68. */
  69. public function loadMinifier($minifierCallback)
  70. {
  71. if ($this->_loadCssJsMinifiers) {
  72. // Minify will not call for these so we must manually load
  73. // them when Minify/HTML.php is called for.
  74. require_once 'Minify/CSS.php';
  75. require_once 'JSMin.php';
  76. }
  77. parent::loadMinifier($minifierCallback); // load Minify/HTML.php
  78. }
  79. }