AsseticNodeVisitor.php 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
  20. {
  21. return $node;
  22. }
  23. public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
  24. {
  25. if (!$formula = $this->checkNode($node, $env)) {
  26. return $node;
  27. }
  28. list($input, $filters, $options) = $formula;
  29. $line = $node->getLine();
  30. // check context and call either asset() or path()
  31. return new \Twig_Node_Expression_Conditional(
  32. new \Twig_Node_Expression_GetAttr(
  33. new \Twig_Node_Expression_Name('assetic', $line),
  34. new \Twig_Node_Expression_Constant('use_controller', $line),
  35. new \Twig_Node(),
  36. \Twig_TemplateInterface::ARRAY_CALL,
  37. $line
  38. ),
  39. new \Twig_Node_Expression_Function(
  40. new \Twig_Node_Expression_Name('path', $line),
  41. new \Twig_Node(array(
  42. new \Twig_Node_Expression_Constant('_assetic_'.$options['name'], $line),
  43. )),
  44. $line
  45. ),
  46. new \Twig_Node_Expression_Function(
  47. new \Twig_Node_Expression_Name('asset', $line),
  48. new \Twig_Node(array($node, new \Twig_Node_Expression_Constant(isset($options['package']) ? $options['package'] : null, $line))),
  49. $line
  50. ),
  51. $line
  52. );
  53. }
  54. /**
  55. * Extracts formulae from filter function nodes.
  56. *
  57. * @return array|null The formula
  58. */
  59. private function checkNode(\Twig_NodeInterface $node, \Twig_Environment $env)
  60. {
  61. if ($node instanceof \Twig_Node_Expression_Function) {
  62. $name = $node->getNode('name')->getAttribute('name');
  63. if ($env->getFunction($name) instanceof AsseticFilterFunction) {
  64. $arguments = array();
  65. foreach ($node->getNode('arguments') as $argument) {
  66. $arguments[] = eval('return '.$env->compile($argument).';');
  67. }
  68. $invoker = $env->getExtension('assetic')->getFilterInvoker($name);
  69. $factory = $invoker->getFactory();
  70. $inputs = isset($arguments[0]) ? (array) $arguments[0] : array();
  71. $filters = $invoker->getFilters();
  72. $options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array());
  73. if (!isset($options['name'])) {
  74. $options['name'] = $factory->generateAssetName($inputs, $filters);
  75. }
  76. return array($inputs, $filters, $options);
  77. }
  78. }
  79. }
  80. public function getPriority()
  81. {
  82. return 0;
  83. }
  84. }