FlashMessageExtension.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Extension;
  11. use Sonata\CoreBundle\FlashMessage\FlashManager;
  12. /**
  13. * This is the Sonata core flash message Twig extension.
  14. *
  15. * @author Vincent Composieux <composieux@ekino.com>
  16. */
  17. class FlashMessageExtension extends \Twig_Extension
  18. {
  19. /**
  20. * @var FlashManager
  21. */
  22. protected $flashManager;
  23. /**
  24. * @param FlashManager $flashManager
  25. */
  26. public function __construct(FlashManager $flashManager)
  27. {
  28. $this->flashManager = $flashManager;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getFunctions()
  34. {
  35. return array(
  36. new \Twig_SimpleFunction('sonata_flashmessages_get', array($this, 'getFlashMessages')),
  37. new \Twig_SimpleFunction('sonata_flashmessages_types', array($this, 'getFlashMessagesTypes')),
  38. );
  39. }
  40. /**
  41. * Returns flash messages handled by Sonata core flash manager.
  42. *
  43. * @param string $type Type of flash message
  44. * @param string $domain Translation domain to use
  45. *
  46. * @return string
  47. */
  48. public function getFlashMessages($type, $domain = null)
  49. {
  50. return $this->flashManager->get($type, $domain);
  51. }
  52. /**
  53. * Returns flash messages types handled by Sonata core flash manager.
  54. *
  55. * @return string
  56. */
  57. public function getFlashMessagesTypes()
  58. {
  59. return $this->flashManager->getHandledTypes();
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getName()
  65. {
  66. return 'sonata_core_flashmessage';
  67. }
  68. }