FlashManager.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\FlashMessage;
  11. use Sonata\CoreBundle\Component\Status\StatusClassRendererInterface;
  12. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  13. use Symfony\Component\Translation\TranslatorInterface;
  14. /**
  15. * @author Vincent Composieux <composieux@ekino.com>
  16. */
  17. class FlashManager implements StatusClassRendererInterface
  18. {
  19. /**
  20. * @var SessionInterface
  21. */
  22. protected $session;
  23. /**
  24. * @var TranslatorInterface
  25. */
  26. protected $translator;
  27. /**
  28. * @var array
  29. */
  30. protected $types;
  31. /**
  32. * @var array
  33. */
  34. protected $cssClasses;
  35. /**
  36. * @param SessionInterface $session Symfony session service
  37. * @param TranslatorInterface $translator Symfony translator service
  38. * @param array $types Sonata core types array (defined in configuration)
  39. * @param array $cssClasses Css classes associated with $types
  40. */
  41. public function __construct(SessionInterface $session, TranslatorInterface $translator, array $types, array $cssClasses)
  42. {
  43. $this->session = $session;
  44. $this->translator = $translator;
  45. $this->types = $types;
  46. $this->cssClasses = $cssClasses;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function handlesObject($object, $statusName = null)
  52. {
  53. return is_string($object) && array_key_exists($object, $this->cssClasses);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getStatusClass($object, $statusName = null, $default = '')
  59. {
  60. return array_key_exists($object, $this->cssClasses)
  61. ? $this->cssClasses[$object]
  62. : $default;
  63. }
  64. /**
  65. * Returns Sonata core flash message types.
  66. *
  67. * @return array
  68. */
  69. public function getTypes()
  70. {
  71. return $this->types;
  72. }
  73. /**
  74. * Returns Symfony session service.
  75. *
  76. * @return SessionInterface
  77. */
  78. public function getSession()
  79. {
  80. return $this->session;
  81. }
  82. /**
  83. * Returns Symfony translator service.
  84. *
  85. * @return TranslatorInterface
  86. */
  87. public function getTranslator()
  88. {
  89. return $this->translator;
  90. }
  91. /**
  92. * Returns flash bag messages for correct type after renaming with Sonata core type.
  93. *
  94. * @param string $type Type of flash message
  95. * @param string $domain Translation domain to use
  96. *
  97. * @return array
  98. */
  99. public function get($type, $domain = null)
  100. {
  101. $this->handle($domain);
  102. return $this->getSession()->getFlashBag()->get($type);
  103. }
  104. /**
  105. * Gets handled message types.
  106. *
  107. * @return array
  108. */
  109. public function getHandledTypes()
  110. {
  111. return array_keys($this->getTypes());
  112. }
  113. /**
  114. * Handles flash bag types renaming.
  115. *
  116. * @param string $domain
  117. */
  118. protected function handle($domain = null)
  119. {
  120. foreach ($this->getTypes() as $type => $values) {
  121. foreach ($values as $value => $options) {
  122. $domainType = $domain ?: $options['domain'];
  123. $this->rename($type, $value, $domainType);
  124. }
  125. }
  126. }
  127. /**
  128. * Process flash message type rename.
  129. *
  130. * @param string $type Sonata core flash message type
  131. * @param string $value Original flash message type
  132. * @param string $domain Translation domain to use
  133. */
  134. protected function rename($type, $value, $domain)
  135. {
  136. $flashBag = $this->getSession()->getFlashBag();
  137. foreach ($flashBag->get($value) as $message) {
  138. $message = $this->getTranslator()->trans($message, array(), $domain);
  139. $flashBag->add($type, $message);
  140. }
  141. }
  142. }