Skip to content

Commit 7629136

Browse files
committed
Bootstrap listener request events only on master request.
1 parent e8a0514 commit 7629136

File tree

1 file changed

+71
-59
lines changed

1 file changed

+71
-59
lines changed

EventListener/BootstrapListener.php

+71-59
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,24 @@ public function onPreConfiguration(Event $event)
5353
*/
5454
public function onKernelRequestEarly(GetResponseEvent $event)
5555
{
56+
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
5657

57-
$request = $event->getRequest();
58+
$request = $event->getRequest();
5859

59-
// When clean URLs are enabled, emulate ?q=foo/bar using REQUEST_URI. It is
60-
// not possible to append the query string using mod_rewrite without the B
61-
// flag (this was added in Apache 2.2.8), because mod_rewrite unescapes the
62-
// path before passing it on to PHP. This is a problem when the path contains
63-
// e.g. "&" or "%" that have special meanings in URLs and must be encoded.
64-
//
65-
// @see drupal_environment_initialize();
66-
$path = $_GET['q'] = urldecode(substr($request->getPathInfo(), 1));
67-
$request->query->set('q', $path);
60+
// When clean URLs are enabled, emulate ?q=foo/bar using REQUEST_URI. It is
61+
// not possible to append the query string using mod_rewrite without the B
62+
// flag (this was added in Apache 2.2.8), because mod_rewrite unescapes the
63+
// path before passing it on to PHP. This is a problem when the path contains
64+
// e.g. "&" or "%" that have special meanings in URLs and must be encoded.
65+
//
66+
// @see drupal_environment_initialize();
67+
$path = $_GET['q'] = urldecode(substr($request->getPathInfo(), 1));
68+
$request->query->set('q', $path);
6869

69-
$GLOBALS['base_url'] = $request->getSchemeAndHttpHost();
70+
$GLOBALS['base_url'] = $request->getSchemeAndHttpHost();
7071

71-
drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES);
72+
drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES);
73+
}
7274
}
7375

7476
/**
@@ -78,7 +80,9 @@ public function onKernelRequestEarly(GetResponseEvent $event)
7880
*/
7981
public function onKernelRequestBeforeSession(GetResponseEvent $event)
8082
{
81-
drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
83+
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
84+
drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
85+
}
8286
}
8387

8488
/**
@@ -88,14 +92,16 @@ public function onKernelRequestBeforeSession(GetResponseEvent $event)
8892
*/
8993
public function onKernelRequestAfterSession(GetResponseEvent $event)
9094
{
91-
if (empty($GLOBALS['user'])) {
92-
$GLOBALS['user'] = drupal_anonymous_user();
93-
date_default_timezone_set(drupal_get_user_timezone());
94-
}
95+
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
96+
if (empty($GLOBALS['user'])) {
97+
$GLOBALS['user'] = drupal_anonymous_user();
98+
date_default_timezone_set(drupal_get_user_timezone());
99+
}
95100

96-
// This is basically noop.
97-
drupal_bootstrap(DRUPAL_BOOTSTRAP_PAGE_HEADER);
101+
// This is basically noop.
102+
drupal_bootstrap(DRUPAL_BOOTSTRAP_PAGE_HEADER);
98103

104+
}
99105
}
100106

101107
/**
@@ -105,35 +111,37 @@ public function onKernelRequestAfterSession(GetResponseEvent $event)
105111
*/
106112
public function onKernelRequestBeforeRouter(GetResponseEvent $event)
107113
{
108-
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
109-
110-
$request = $event->getRequest();
111-
if (!$request->attributes->get('_drupal', false) && !empty($request->query->get('q', ''))) {
112-
113-
// The 'q' variable is pervasive in Drupal, so it's best to just keep
114-
// it even though it's very un-Symfony.
115-
$path = drupal_get_normal_path($_GET['q']);
116-
117-
if (variable_get('menu_rebuild_needed', FALSE) || !variable_get('menu_masks', array())) {
118-
menu_rebuild();
119-
}
120-
$original_map = arg(NULL, $path);
121-
122-
$parts = array_slice($original_map, 0, MENU_MAX_PARTS);
123-
$ancestors = menu_get_ancestors($parts);
124-
$router_item = db_query_range('SELECT * FROM {menu_router} WHERE path IN (:ancestors) ORDER BY fit DESC', 0, 1, array(':ancestors' => $ancestors))->fetchAssoc();
125-
126-
if ($router_item) {
127-
// Allow modules to alter the router item before it is translated and
128-
// checked for access.
129-
drupal_alter('menu_get_item', $router_item, $path, $original_map);
130-
131-
// The requested path is an unalaised Drupal route.
132-
$request->attributes->add(array(
133-
'_drupal' => true,
134-
'_controller' => $router_item['page_callback'],
135-
'_route' => $router_item['path'],
136-
));
114+
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
115+
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
116+
117+
$request = $event->getRequest();
118+
if (!$request->attributes->get('_drupal', false) && !empty($request->query->get('q', ''))) {
119+
120+
// The 'q' variable is pervasive in Drupal, so it's best to just keep
121+
// it even though it's very un-Symfony.
122+
$path = drupal_get_normal_path($_GET['q']);
123+
124+
if (variable_get('menu_rebuild_needed', FALSE) || !variable_get('menu_masks', array())) {
125+
menu_rebuild();
126+
}
127+
$original_map = arg(NULL, $path);
128+
129+
$parts = array_slice($original_map, 0, MENU_MAX_PARTS);
130+
$ancestors = menu_get_ancestors($parts);
131+
$router_item = db_query_range('SELECT * FROM {menu_router} WHERE path IN (:ancestors) ORDER BY fit DESC', 0, 1, array(':ancestors' => $ancestors))->fetchAssoc();
132+
133+
if ($router_item) {
134+
// Allow modules to alter the router item before it is translated and
135+
// checked for access.
136+
drupal_alter('menu_get_item', $router_item, $path, $original_map);
137+
138+
// The requested path is an unalaised Drupal route.
139+
$request->attributes->add(array(
140+
'_drupal' => true,
141+
'_controller' => $router_item['page_callback'],
142+
'_route' => $router_item['path'],
143+
));
144+
}
137145
}
138146
}
139147
}
@@ -145,7 +153,9 @@ public function onKernelRequestBeforeRouter(GetResponseEvent $event)
145153
*/
146154
public function onKernelRequestAfterLocale(GetResponseEvent $event)
147155
{
148-
drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
156+
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
157+
drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
158+
}
149159
}
150160

151161
/**
@@ -156,15 +166,17 @@ public function onKernelRequestAfterLocale(GetResponseEvent $event)
156166
*/
157167
public function onKernelRequestAfterFirewall(GetResponseEvent $event)
158168
{
159-
// Prior to invoking hook_init(), initialize the theme (potentially a custom
160-
// one for this page), so that:
161-
// - Modules with hook_init() implementations that call theme() or
162-
// theme_get_registry() don't initialize the incorrect theme.
163-
// - The theme can have hook_*_alter() implementations affect page building
164-
// (e.g., hook_form_alter(), hook_node_view_alter(), hook_page_alter()),
165-
// ahead of when rendering starts.
166-
menu_set_custom_theme();
167-
drupal_theme_initialize();
168-
module_invoke_all('init');
169+
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
170+
// Prior to invoking hook_init(), initialize the theme (potentially a custom
171+
// one for this page), so that:
172+
// - Modules with hook_init() implementations that call theme() or
173+
// theme_get_registry() don't initialize the incorrect theme.
174+
// - The theme can have hook_*_alter() implementations affect page building
175+
// (e.g., hook_form_alter(), hook_node_view_alter(), hook_page_alter()),
176+
// ahead of when rendering starts.
177+
menu_set_custom_theme();
178+
drupal_theme_initialize();
179+
module_invoke_all('init');
180+
}
169181
}
170182
}

0 commit comments

Comments
 (0)