AsseticNode.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\Asset\AssetInterface;
  12. use Assetic\Extension\Twig\AsseticNode as BaseAsseticNode;
  13. /**
  14. * Assetic node.
  15. *
  16. * @author Kris Wallsmith <kris@symfony.com>
  17. */
  18. class AsseticNode extends BaseAsseticNode
  19. {
  20. protected function compileAssetUrl(\Twig_Compiler $compiler, AssetInterface $asset, $name)
  21. {
  22. $compiler
  23. ->raw('isset($context[\'assetic\'][\'use_controller\']) && $context[\'assetic\'][\'use_controller\'] ? ')
  24. ->subcompile($this->getPathFunction($name))
  25. ->raw(' : ')
  26. ->subcompile($this->getAssetFunction($asset->getTargetPath()))
  27. ;
  28. }
  29. private function getPathFunction($name)
  30. {
  31. return new \Twig_Node_Expression_Function(
  32. new \Twig_Node_Expression_Name('path', $this->getLine()),
  33. new \Twig_Node(array(new \Twig_Node_Expression_Constant('_assetic_'.$name, $this->getLine()))),
  34. $this->getLine()
  35. );
  36. }
  37. private function getAssetFunction($path)
  38. {
  39. $arguments = array(new \Twig_Node_Expression_Constant($path, $this->getLine()));
  40. if ($this->hasAttribute('package')) {
  41. $arguments[] = new \Twig_Node_Expression_Constant($this->getAttribute('package'), $this->getLine());
  42. }
  43. return new \Twig_Node_Expression_Function(
  44. new \Twig_Node_Expression_Name('asset', $this->getLine()),
  45. new \Twig_Node($arguments),
  46. $this->getLine()
  47. );
  48. }
  49. }