TemplateBoxNode.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\CoreBundle\Twig\Node;
  11. use Symfony\Component\Translation\TranslatorInterface;
  12. class TemplateBoxNode extends \Twig_Node
  13. {
  14. /**
  15. * @var int
  16. */
  17. protected $enabled;
  18. /**
  19. * @var TranslatorInterface
  20. */
  21. protected $translator;
  22. /**
  23. * @param \Twig_Node_Expression $message Node message to display
  24. * @param \Twig_Node_Expression $translationBundle Node translation bundle to use for display
  25. * @param int $enabled Is Symfony debug enabled?
  26. * @param TranslatorInterface $translator Symfony Translator service
  27. * @param null|string $lineno Symfony template line number
  28. * @param null $tag Symfony tag name
  29. */
  30. public function __construct(\Twig_Node_Expression $message, \Twig_Node_Expression $translationBundle = null, $enabled, TranslatorInterface $translator, $lineno, $tag = null)
  31. {
  32. $this->enabled = $enabled;
  33. $this->translator = $translator;
  34. $nodes = array('message' => $message);
  35. if ($translationBundle) {
  36. $nodes['translationBundle'] = $translationBundle;
  37. }
  38. parent::__construct($nodes, array(), $lineno, $tag);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function compile(\Twig_Compiler $compiler)
  44. {
  45. $compiler
  46. ->addDebugInfo($this);
  47. if (!$this->enabled) {
  48. $compiler->write("// token for sonata_template_box, however the box is disabled\n");
  49. return;
  50. }
  51. $value = $this->getNode('message')->getAttribute('value');
  52. $translationBundle = null;
  53. if ($this->hasNode('translationBundle')) {
  54. $translationBundle = $this->getNode('translationBundle');
  55. }
  56. if ($translationBundle) {
  57. $translationBundle = $translationBundle->getAttribute('value');
  58. }
  59. $message = <<<CODE
  60. "<div class='alert alert-default alert-info'>
  61. <strong>{$this->translator->trans($value, array(), $translationBundle)}</strong>
  62. <div>{$this->translator->trans('sonata_core_template_box_file_found_in', array(), 'SonataCoreBundle')} <code>{\$this->getTemplateName()}</code>.</div>
  63. </div>"
  64. CODE;
  65. $compiler
  66. ->write("echo $message;");
  67. }
  68. }