1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OrbitaleCmsBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Rock Ancelet <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Orbitale\Bundle\CmsBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use Symfony\Bridge\Twig\TwigEngine; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
16
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
17
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
18
|
|
|
|
19
|
|
|
class LayoutsListener implements EventSubscriberInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $layouts; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var TwigEngine |
28
|
|
|
*/ |
29
|
|
|
private $templating; |
30
|
|
|
|
31
|
|
|
public function __construct(array $layouts, TwigEngine $templating) |
32
|
|
|
{ |
33
|
|
|
$this->layouts = $layouts; |
34
|
|
|
$this->templating = $templating; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public static function getSubscribedEvents() |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
KernelEvents::REQUEST => ['setRequestLayout', 1], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setRequestLayout(GetResponseEvent $event) |
48
|
|
|
{ |
49
|
|
|
$request = $event->getRequest(); |
50
|
|
|
|
51
|
|
|
// Get the necessary informations to check them in layout configurations |
52
|
|
|
$path = $request->getPathInfo(); |
53
|
|
|
$host = $request->getHost(); |
54
|
|
|
|
55
|
|
|
// As a layout must be set, we force it to be empty if no layout is properly configured. |
56
|
|
|
// Then this will throw an exception, and the user will be warned of the "no-layout" config problem. |
57
|
|
|
$finalLayout = null; |
58
|
|
|
|
59
|
|
|
foreach ($this->layouts as $layoutConfig) { |
60
|
|
|
$match = false; |
61
|
|
|
|
62
|
|
|
// First check host |
63
|
|
|
if ($layoutConfig['host'] && $host === $layoutConfig['host']) { |
64
|
|
|
$match = true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Check pattern |
68
|
|
|
if ($layoutConfig['pattern'] && preg_match('~' . $layoutConfig['pattern'] . '~', $path)) { |
69
|
|
|
$match = true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($match) { |
73
|
|
|
$finalLayout = $layoutConfig; |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// If nothing matches, we take the first layout that has no "host" or "pattern" configuration. |
79
|
|
|
if (null === $finalLayout) { |
80
|
|
|
$layouts = $this->layouts; |
81
|
|
|
do { |
82
|
|
|
$finalLayout = array_shift($layouts); |
83
|
|
|
if ($finalLayout['host'] || $finalLayout['pattern']) { |
84
|
|
|
$finalLayout = null; |
85
|
|
|
} |
86
|
|
|
} while (null === $finalLayout && count($layouts)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (null === $finalLayout || !$this->templating->exists($finalLayout['resource'])) { |
90
|
|
|
throw new \Twig_Error_Loader(sprintf( |
91
|
|
|
'Unable to find template %s for layout %s. The "layout" parameter must be a valid twig view to be used as a layout.', |
92
|
|
|
$finalLayout['resource'], $finalLayout['name'] |
93
|
|
|
), 0, $finalLayout['resource']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$event->getRequest()->attributes->set('_orbitale_cms_layout', $finalLayout); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|