AsseticExtension.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2012 OpenSky Project Inc
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Assetic\Extension\Twig;
  11. use Assetic\Factory\AssetFactory;
  12. class AsseticExtension extends \Twig_Extension
  13. {
  14. protected $factory;
  15. protected $functions;
  16. public function __construct(AssetFactory $factory, $functions = array())
  17. {
  18. $this->factory = $factory;
  19. $this->functions = array();
  20. foreach ($functions as $function => $options) {
  21. if (is_integer($function) && is_string($options)) {
  22. $this->functions[$options] = array('filter' => $options);
  23. } else {
  24. $this->functions[$function] = $options + array('filter' => $function);
  25. }
  26. }
  27. }
  28. public function getTokenParsers()
  29. {
  30. return array(
  31. new AsseticTokenParser($this->factory, 'javascripts', 'js/*.js'),
  32. new AsseticTokenParser($this->factory, 'stylesheets', 'css/*.css'),
  33. new AsseticTokenParser($this->factory, 'image', 'images/*', true),
  34. );
  35. }
  36. public function getFunctions()
  37. {
  38. $functions = array();
  39. foreach ($this->functions as $function => $filter) {
  40. $functions[$function] = new AsseticFilterFunction($function);
  41. }
  42. return $functions;
  43. }
  44. public function getGlobals()
  45. {
  46. return array(
  47. 'assetic' => array('debug' => $this->factory->isDebug()),
  48. );
  49. }
  50. public function getFilterInvoker($function)
  51. {
  52. return new AsseticFilterInvoker($this->factory, $this->functions[$function]);
  53. }
  54. public function getName()
  55. {
  56. return 'assetic';
  57. }
  58. }