TwigNodeStyle.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @package Grav.Common.Twig
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Twig\Node;
  9. class TwigNodeStyle extends \Twig_Node implements \Twig_NodeOutputInterface
  10. {
  11. protected $tagName = 'style';
  12. /**
  13. * TwigNodeAssets constructor.
  14. * @param \Twig_Node|null $body
  15. * @param \Twig_Node_Expression|null $attributes
  16. * @param int $lineno
  17. * @param null $tag
  18. */
  19. public function __construct(
  20. \Twig_Node $body = null,
  21. \Twig_Node_Expression $file = null,
  22. \Twig_Node_Expression $group = null,
  23. \Twig_Node_Expression $priority = null,
  24. \Twig_Node_Expression $attributes = null,
  25. $lineno = 0,
  26. $tag = null
  27. )
  28. {
  29. parent::__construct(['body' => $body, 'file' => $file, 'group' => $group, 'priority' => $priority, 'attributes' => $attributes], [], $lineno, $tag);
  30. }
  31. /**
  32. * Compiles the node to PHP.
  33. *
  34. * @param \Twig_Compiler $compiler A Twig_Compiler instance
  35. * @throws \LogicException
  36. */
  37. public function compile(\Twig_Compiler $compiler)
  38. {
  39. $compiler->addDebugInfo($this);
  40. if ($this->getNode('attributes') !== null) {
  41. $compiler
  42. ->write('$attributes = ')
  43. ->subcompile($this->getNode('attributes'))
  44. ->raw(";\n")
  45. ->write("if (\$attributes !== null && !is_array(\$attributes)) {\n")
  46. ->indent()
  47. ->write("throw new UnexpectedValueException('{% {$this->tagName} with x %}: x is not an array');\n")
  48. ->outdent()
  49. ->write("}\n");
  50. } else {
  51. $compiler->write('$attributes = [];' . "\n");
  52. }
  53. if ($this->getNode('group') !== null) {
  54. $compiler
  55. ->write('$group = ')
  56. ->subcompile($this->getNode('group'))
  57. ->raw(";\n")
  58. ->write("if (\$group !== null && !is_string(\$group)) {\n")
  59. ->indent()
  60. ->write("throw new UnexpectedValueException('{% {$this->tagName} in x %}: x is not a string');\n")
  61. ->outdent()
  62. ->write("}\n");
  63. } else {
  64. $compiler->write('$group = null;' . "\n");
  65. }
  66. if ($this->getNode('priority') !== null) {
  67. $compiler
  68. ->write('$priority = (int)(')
  69. ->subcompile($this->getNode('priority'))
  70. ->raw(");\n");
  71. } else {
  72. $compiler->write('$priority = null;' . "\n");
  73. }
  74. $compiler->write("\$assets = \\Grav\\Common\\Grav::instance()['assets'];\n");
  75. if ($this->getNode('file') !== null) {
  76. $compiler
  77. ->write('$file = ')
  78. ->subcompile($this->getNode('file'))
  79. ->write(";\n")
  80. ->write("\$pipeline = !empty(\$attributes['pipeline']);\n")
  81. ->write("\$assets->addCss(\$file, \$priority, \$pipeline, \$group);\n");
  82. } else {
  83. $compiler
  84. ->write("ob_start();\n")
  85. ->subcompile($this->getNode('body'))
  86. ->write("\$content = ob_get_clean();")
  87. ->write("\$assets->addInlineCss(\$content, \$priority, \$group);\n");
  88. }
  89. }
  90. }