TwigFormulaLoader.php 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2011 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\Loader\FormulaLoaderInterface;
  12. use Assetic\Factory\Resource\ResourceInterface;
  13. /**
  14. * Loads asset formulae from Twig templates.
  15. *
  16. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  17. */
  18. class TwigFormulaLoader implements FormulaLoaderInterface
  19. {
  20. private $twig;
  21. public function __construct(\Twig_Environment $twig)
  22. {
  23. $this->twig = $twig;
  24. }
  25. public function load(ResourceInterface $resource)
  26. {
  27. try {
  28. $tokens = $this->twig->tokenize($resource->getContent(), (string) $resource);
  29. $nodes = $this->twig->parse($tokens);
  30. } catch (\Exception $e) {
  31. return array();
  32. }
  33. return $this->loadNode($nodes);
  34. }
  35. /**
  36. * Loads assets from the supplied node.
  37. *
  38. * @return array An array of asset formulae indexed by name
  39. */
  40. private function loadNode(\Twig_Node $node)
  41. {
  42. $formulae = array();
  43. if ($node instanceof AsseticNode) {
  44. $formulae[$node->getAttribute('name')] = array(
  45. $node->getAttribute('inputs'),
  46. $node->getAttribute('filters'),
  47. array(
  48. 'output' => $node->getAttribute('asset')->getTargetPath(),
  49. 'name' => $node->getAttribute('name'),
  50. 'debug' => $node->getAttribute('debug'),
  51. 'combine' => $node->getAttribute('combine'),
  52. ),
  53. );
  54. } elseif ($node instanceof \Twig_Node_Expression_Function) {
  55. $name = version_compare(\Twig_Environment::VERSION, '1.2.0-DEV', '<')
  56. ? $node->getNode('name')->getAttribute('name')
  57. : $node->getAttribute('name');
  58. if ($this->twig->getFunction($name) instanceof AsseticFilterFunction) {
  59. $arguments = array();
  60. foreach ($node->getNode('arguments') as $argument) {
  61. $arguments[] = eval('return '.$this->twig->compile($argument).';');
  62. }
  63. $invoker = $this->twig->getExtension('assetic')->getFilterInvoker($name);
  64. $inputs = isset($arguments[0]) ? (array) $arguments[0] : array();
  65. $filters = $invoker->getFilters();
  66. $options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array());
  67. if (!isset($options['name'])) {
  68. $options['name'] = $invoker->getFactory()->generateAssetName($inputs, $filters, $options);
  69. }
  70. $formulae[$options['name']] = array($inputs, $filters, $options);
  71. }
  72. }
  73. foreach ($node as $child) {
  74. if ($child instanceof \Twig_Node) {
  75. $formulae += $this->loadNode($child);
  76. }
  77. }
  78. return $formulae;
  79. }
  80. }