AsseticNodeVisitor.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Bundle\AsseticBundle\Twig;
  11. use Assetic\Extension\Twig\AsseticFilterFunction;
  12. /**
  13. * Assetic node visitor.
  14. *
  15. * @author Kris Wallsmith <kris@symfony.com>
  16. */
  17. class AsseticNodeVisitor implements \Twig_NodeVisitorInterface
  18. {
  19. private $useNodeName;
  20. public function __construct()
  21. {
  22. $this->useNodeName = version_compare(\Twig_Environment::VERSION, '1.2.0-DEV', '<');
  23. }
  24. public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
  25. {
  26. return $node;
  27. }
  28. public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
  29. {
  30. if (!$formula = $this->checkNode($node, $env)) {
  31. return $node;
  32. }
  33. list($input, $filters, $options) = $formula;
  34. $line = $node->getLine();
  35. // check context and call either asset() or path()
  36. return new \Twig_Node_Expression_Conditional(
  37. new \Twig_Node_Expression_GetAttr(
  38. new \Twig_Node_Expression_Name('assetic', $line),
  39. new \Twig_Node_Expression_Constant('use_controller', $line),
  40. new \Twig_Node(),
  41. \Twig_TemplateInterface::ARRAY_CALL,
  42. $line
  43. ),
  44. new \Twig_Node_Expression_Function(
  45. $this->useNodeName ? new \Twig_Node_Expression_Name('path', $line) : 'path',
  46. new \Twig_Node(array(
  47. new \Twig_Node_Expression_Constant('_assetic_'.$options['name'], $line),
  48. )),
  49. $line
  50. ),
  51. new \Twig_Node_Expression_Function(
  52. $this->useNodeName ? new \Twig_Node_Expression_Name('asset', $line) : 'asset',
  53. new \Twig_Node(array($node, new \Twig_Node_Expression_Constant(isset($options['package']) ? $options['package'] : null, $line))),
  54. $line
  55. ),
  56. $line
  57. );
  58. }
  59. /**
  60. * Extracts formulae from filter function nodes.
  61. *
  62. * @return array|null The formula
  63. */
  64. private function checkNode(\Twig_NodeInterface $node, \Twig_Environment $env)
  65. {
  66. if ($node instanceof \Twig_Node_Expression_Function) {
  67. $name = $this->useNodeName
  68. ? $node->getNode('name')->getAttribute('name')
  69. : $node->getAttribute('name');
  70. if ($env->getFunction($name) instanceof AsseticFilterFunction) {
  71. $arguments = array();
  72. foreach ($node->getNode('arguments') as $argument) {
  73. $arguments[] = eval('return '.$env->compile($argument).';');
  74. }
  75. $invoker = $env->getExtension('assetic')->getFilterInvoker($name);
  76. $factory = $invoker->getFactory();
  77. $inputs = isset($arguments[0]) ? (array) $arguments[0] : array();
  78. $filters = $invoker->getFilters();
  79. $options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array());
  80. if (!isset($options['name'])) {
  81. $options['name'] = $factory->generateAssetName($inputs, $filters);
  82. }
  83. return array($inputs, $filters, $options);
  84. }
  85. }
  86. }
  87. public function getPriority()
  88. {
  89. return 0;
  90. }
  91. }