AsseticNode.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. version_compare(\Twig_Environment::VERSION, '1.2.0-DEV', '<')
  33. ? new \Twig_Node_Expression_Name('path', $this->getLine()) : 'path',
  34. new \Twig_Node(array(new \Twig_Node_Expression_Constant('_assetic_'.$name, $this->getLine()))),
  35. $this->getLine()
  36. );
  37. }
  38. private function getAssetFunction($path)
  39. {
  40. $arguments = array(new \Twig_Node_Expression_Constant($path, $this->getLine()));
  41. if ($this->hasAttribute('package')) {
  42. $arguments[] = new \Twig_Node_Expression_Constant($this->getAttribute('package'), $this->getLine());
  43. }
  44. return new \Twig_Node_Expression_Function(
  45. version_compare(\Twig_Environment::VERSION, '1.2.0-DEV', '<')
  46. ? new \Twig_Node_Expression_Name('asset', $this->getLine()) : 'asset',
  47. new \Twig_Node($arguments),
  48. $this->getLine()
  49. );
  50. }
  51. }