pquery.js.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * pQuery plugin for composing sets of JavaScript files.
  4. * The plugin has built-in cache control and supports the JShrink minifier.
  5. *
  6. * @package pQuery
  7. */
  8. __p::require_plugins('cache');
  9. __p::load_utils('jshrink');
  10. /**
  11. * pQuery extension class for the 'js' plugin.
  12. */
  13. class pQueryJs extends pQueryCache {
  14. static $accepts = array('array' => 'add_extensions', 'string' => 'make_array');
  15. /**
  16. * Make a single file into an array.
  17. *
  18. * @param string $file The file to put in an array.
  19. */
  20. function make_array($file) {
  21. return $this->add_extensions(array($file));
  22. }
  23. /**
  24. *
  25. *
  26. * @param array $files
  27. */
  28. function add_extensions($files) {
  29. foreach( $files as $i => $file )
  30. if( !preg_match('/\.js$/', $file) )
  31. $files[$i] = $file.'.js';
  32. return $this->get_modification_dates($files);
  33. }
  34. /**
  35. *
  36. */
  37. function minify() {
  38. $this->content = trim(JShrink::minify($this->content, array('flaggedComments' => false)));
  39. return $this;
  40. }
  41. /**
  42. *
  43. */
  44. function set_headers() {
  45. header('Content-Type: application/javascript');
  46. return $this;
  47. }
  48. }
  49. /**
  50. * Shortcut constructor for {@link pQueryJs}.
  51. *
  52. * @param array|string $scripts
  53. * @returns pQueryJs A new script cache instance.
  54. */
  55. function _js($scripts) {
  56. return pQuery::create('js', $scripts);
  57. }
  58. /*
  59. * Add plugin to pQuery
  60. */
  61. __p::extend('pQueryJs', 'js');
  62. ?>