1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the MilioooMessageBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Michiel boeckaert <[email protected]> |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Miliooo\Messaging\Helpers\FlashMessages; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
14
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A flash message provider. |
18
|
|
|
* |
19
|
|
|
* @author Michiel Boeckaert <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class FlashMessageProvider implements FlashMessageProviderInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* A flash bag instance. |
25
|
|
|
* |
26
|
|
|
* @var FlashBagInterface |
27
|
|
|
*/ |
28
|
|
|
protected $flashBag; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* A translator instance. |
32
|
|
|
* |
33
|
|
|
* @var TranslatorInterface |
34
|
|
|
*/ |
35
|
|
|
protected $translator; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The success key |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $successKey; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The failure key |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $errorKey; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param FlashBagInterface $flashBag A flash bag instance |
54
|
|
|
* @param TranslatorInterface $translator A translator instance |
55
|
|
|
* @param string $successKey They key for success messages for example success |
56
|
|
|
* @param string $errorKey The key for error messages for example error |
57
|
|
|
*/ |
58
|
|
|
public function __construct(FlashBagInterface $flashBag, TranslatorInterface $translator, $successKey, $errorKey) |
59
|
|
|
{ |
60
|
|
|
$this->flashBag = $flashBag; |
61
|
|
|
$this->translator = $translator; |
62
|
|
|
$this->successKey = $successKey; |
63
|
|
|
$this->errorKey = $errorKey; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function addFlash($type, $translationKey, $parameters = []) |
70
|
|
|
{ |
71
|
|
|
$key = $type.'Key'; |
72
|
|
|
$message = $this->translate($translationKey, $parameters); |
73
|
|
|
$this->flashBag->add($this->$key, $message); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $message The message to translate |
78
|
|
|
* @param array $parameters Optional parameters |
79
|
|
|
* |
80
|
|
|
* @return string The translated string. |
81
|
|
|
*/ |
82
|
|
|
protected function translate($message, $parameters = []) |
83
|
|
|
{ |
84
|
|
|
return $this->translator->trans($message, $parameters, 'MilioooMessagingBundle'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|