/
RequestListener.php
110 lines (93 loc) · 3.47 KB
/
RequestListener.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace Symfony\Bundle\FrameworkBundle;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* RequestListener.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class RequestListener
{
protected $router;
protected $logger;
protected $container;
public function __construct(ContainerInterface $container, RouterInterface $router, LoggerInterface $logger = null)
{
$this->container = $container;
$this->router = $router;
$this->logger = $logger;
}
/**
* Registers a core.request listener.
*
* @param EventDispatcher $dispatcher An EventDispatcher instance
* @param integer $priority The priority
*/
public function register(EventDispatcher $dispatcher, $priority = 0)
{
$dispatcher->connect('core.request', array($this, 'handle'), $priority);
}
public function handle(Event $event)
{
$request = $event->getParameter('request');
$master = HttpKernelInterface::MASTER_REQUEST === $event->getParameter('request_type');
$this->initializeSession($request, $master);
$this->initializeRequestAttributes($request, $master);
}
protected function initializeSession(Request $request, $master)
{
if (!$master) {
return;
}
// inject the session object if none is present
if (null === $request->getSession()) {
$request->setSession($this->container->get('session'));
}
// starts the session if a session cookie already exists in the request...
if ($request->hasSession()) {
$request->getSession()->start();
}
}
protected function initializeRequestAttributes(Request $request, $master)
{
if ($master) {
// set the context even if the parsing does not need to be done
// to have correct link generation
$this->router->setContext(array(
'base_url' => $request->getBaseUrl(),
'method' => $request->getMethod(),
'host' => $request->getHost(),
'is_secure' => $request->isSecure(),
));
}
if ($request->attributes->has('_controller')) {
// routing is already done
return;
}
// add attributes based on the path info (routing)
if (false !== $parameters = $this->router->match($request->getPathInfo())) {
if (null !== $this->logger) {
$this->logger->info(sprintf('Matched route "%s" (parameters: %s)', $parameters['_route'], str_replace("\n", '', var_export($parameters, true))));
}
$request->attributes->add($parameters);
if ($locale = $request->attributes->get('_locale')) {
$request->getSession()->setLocale($locale);
}
} elseif (null !== $this->logger) {
$this->logger->err(sprintf('No route found for %s', $request->getPathInfo()));
}
}
}