AsseticNode.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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(new TargetPathNode($this, $asset, $name)))
  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($path);
  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. }
  52. class TargetPathNode extends AsseticNode
  53. {
  54. private $node;
  55. private $asset;
  56. private $name;
  57. public function __construct(AsseticNode $node, AssetInterface $asset, $name)
  58. {
  59. $this->node = $node;
  60. $this->asset = $asset;
  61. $this->name = $name;
  62. }
  63. public function compile(\Twig_Compiler $compiler)
  64. {
  65. BaseAsseticNode::compileAssetUrl($compiler, $this->asset, $this->name);
  66. }
  67. public function getLine()
  68. {
  69. return $this->node->getLine();
  70. }
  71. }