PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/kriswallsmith/assetic/src/Assetic/Extension/Twig/AsseticNode.php

https://github.com/nattaphat/hgis
PHP | 166 lines | 112 code | 28 blank | 26 comment | 10 complexity | f8dd27a8cfac8a392b8afd8d4a261f97 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2013 OpenSky Project Inc
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Assetic\Extension\Twig;
  11. use Assetic\Asset\AssetInterface;
  12. class AsseticNode extends \Twig_Node
  13. {
  14. /**
  15. * Constructor.
  16. *
  17. * Available attributes:
  18. *
  19. * * debug: The debug mode
  20. * * combine: Whether to combine assets
  21. * * var_name: The name of the variable to expose to the body node
  22. *
  23. * @param AssetInterface $asset The asset
  24. * @param \Twig_NodeInterface $body The body node
  25. * @param array $inputs An array of input strings
  26. * @param array $filters An array of filter strings
  27. * @param string $name The name of the asset
  28. * @param array $attributes An array of attributes
  29. * @param integer $lineno The line number
  30. * @param string $tag The tag name
  31. */
  32. public function __construct(AssetInterface $asset, \Twig_NodeInterface $body, array $inputs, array $filters, $name, array $attributes = array(), $lineno = 0, $tag = null)
  33. {
  34. $nodes = array('body' => $body);
  35. $attributes = array_replace(
  36. array('debug' => null, 'combine' => null, 'var_name' => 'asset_url'),
  37. $attributes,
  38. array('asset' => $asset, 'inputs' => $inputs, 'filters' => $filters, 'name' => $name)
  39. );
  40. parent::__construct($nodes, $attributes, $lineno, $tag);
  41. }
  42. public function compile(\Twig_Compiler $compiler)
  43. {
  44. $compiler->addDebugInfo($this);
  45. $combine = $this->getAttribute('combine');
  46. $debug = $this->getAttribute('debug');
  47. if (null === $combine && null !== $debug) {
  48. $combine = !$debug;
  49. }
  50. if (null === $combine) {
  51. $compiler
  52. ->write("if (isset(\$context['assetic']['debug']) && \$context['assetic']['debug']) {\n")
  53. ->indent()
  54. ;
  55. $this->compileDebug($compiler);
  56. $compiler
  57. ->outdent()
  58. ->write("} else {\n")
  59. ->indent()
  60. ;
  61. $this->compileAsset($compiler, $this->getAttribute('asset'), $this->getAttribute('name'));
  62. $compiler
  63. ->outdent()
  64. ->write("}\n")
  65. ;
  66. } elseif ($combine) {
  67. $this->compileAsset($compiler, $this->getAttribute('asset'), $this->getAttribute('name'));
  68. } else {
  69. $this->compileDebug($compiler);
  70. }
  71. $compiler
  72. ->write('unset($context[')
  73. ->repr($this->getAttribute('var_name'))
  74. ->raw("]);\n")
  75. ;
  76. }
  77. protected function compileDebug(\Twig_Compiler $compiler)
  78. {
  79. $i = 0;
  80. foreach ($this->getAttribute('asset') as $leaf) {
  81. $leafName = $this->getAttribute('name').'_'.$i++;
  82. $this->compileAsset($compiler, $leaf, $leafName);
  83. }
  84. }
  85. protected function compileAsset(\Twig_Compiler $compiler, AssetInterface $asset, $name)
  86. {
  87. if ($vars = $asset->getVars()) {
  88. $compiler->write("// check variable conditions\n");
  89. foreach ($vars as $var) {
  90. $compiler
  91. ->write("if (!isset(\$context['assetic']['vars']['$var'])) {\n")
  92. ->indent()
  93. ->write("throw new \RuntimeException(sprintf('The asset \"".$name."\" expected variable \"".$var."\" to be set, but got only these vars: %s. Did you set-up a value supplier?', isset(\$context['assetic']['vars']) && \$context['assetic']['vars'] ? implode(', ', \$context['assetic']['vars']) : '# none #'));\n")
  94. ->outdent()
  95. ->write("}\n")
  96. ;
  97. }
  98. $compiler->raw("\n");
  99. }
  100. $compiler
  101. ->write("// asset \"$name\"\n")
  102. ->write('$context[')
  103. ->repr($this->getAttribute('var_name'))
  104. ->raw('] = ')
  105. ;
  106. $this->compileAssetUrl($compiler, $asset, $name);
  107. $compiler
  108. ->raw(";\n")
  109. ->subcompile($this->getNode('body'))
  110. ;
  111. }
  112. protected function compileAssetUrl(\Twig_Compiler $compiler, AssetInterface $asset, $name)
  113. {
  114. if (!$vars = $asset->getVars()) {
  115. $compiler->repr($asset->getTargetPath());
  116. return;
  117. }
  118. $compiler
  119. ->raw("strtr(")
  120. ->string($asset->getTargetPath())
  121. ->raw(", array(");
  122. ;
  123. $first = true;
  124. foreach ($vars as $var) {
  125. if (!$first) {
  126. $compiler->raw(", ");
  127. }
  128. $first = false;
  129. $compiler
  130. ->string("{".$var."}")
  131. ->raw(" => \$context['assetic']['vars']['$var']")
  132. ;
  133. }
  134. $compiler
  135. ->raw("))")
  136. ;
  137. }
  138. }