-
Notifications
You must be signed in to change notification settings - Fork 19
/
AlertifySessionHandler.php
89 lines (77 loc) · 2.53 KB
/
AlertifySessionHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Troopers\AlertifyBundle\Handler;
use Symfony\Component\HttpFoundation\Session\Session;
/**
* AlertifySessionHandler.
*/
class AlertifySessionHandler
{
/**
* @var \Twig_Environment
*/
protected $twig;
/**
* @var array
*/
private $defaultParameters;
/**
* AlertifySessionHandler constructor.
*
* @param \Twig_Environment $twig
* @param array $defaultParameters
*/
public function __construct(array $defaultParameters)
{
$this->defaultParameters = $defaultParameters;
}
/**
* Alertify .
*
* @param \Twig_Environment $this-twig
* @param Session $session
*
* @return string
*/
public function handle($session, \Twig_Environment $twig)
{
$flashes = $session->getFlashBag()->all();
$renders = [];
foreach ($flashes as $type => $flash) {
foreach ($flash as $key => $content) {
if (is_array($content)) {
$context = isset($content['context']) ? $content['context'] : null;
$defaultParameters = self::getDefaultParametersFromContext($context);
$parameters = array_replace_recursive($defaultParameters, $content);
} else {
$defaultParameters = self::getDefaultParametersFromContext(null);
$parameters = array_replace($defaultParameters, ['body' => $content]);
}
$parameters['type'] = $type;
$renders[$type.$key] = $twig->render('@TroopersAlertify/'.$parameters['engine'].'.html.twig', $parameters);
}
}
return implode(' ', $renders);
}
/**
* Get the configuration for the given context.
*
* @param string $context The actual context
*
* @return array
**/
protected function getDefaultParametersFromContext($context = null)
{
if (count($this->defaultParameters['contexts'])) {
//If context is not given, just take the default one
if ($context === null) {
$context = $this->defaultParameters['default']['context'];
}
//If context is in declared contexts, we use it
if (array_key_exists($context, $this->defaultParameters['contexts'])) {
return $this->defaultParameters['contexts'][$context];
}
}
//else we return the default configuration
return $this->defaultParameters['default'];
}
}