Commit e1b4fe97 authored by katbailey's avatar katbailey

Get rid of the request() wrapper function, plus various other cleanups

parent d1e52e00
......@@ -1611,32 +1611,6 @@ function request_uri($omit_query_string = FALSE) {
return $omit_query_string ? strtok($uri, '?') : $uri;
}
/**
* Returns the current global request object.
*
* @todo Replace this function with a proper dependency injection container.
*
* @staticvar Symfony\Component\HttpFoundation\Request $request
*
* @param Symfony\Component\HttpFoundation\Request $new_request
* Optional. The new request object to store. This parameter should only be
* used by index.php.
*
* @return Symfony\Component\HttpFoundation\Request
* The current request object.
*/
function request(Request $new_request = NULL) {
static $request;
if ($new_request) {
$request = $new_request;
}
elseif (drupal_container()->has('request')) {
$request = drupal_container()->get('request');
}
return $request;
}
/**
* Logs an exception.
*
......@@ -2659,37 +2633,46 @@ function drupal_language_initialize() {
* request. When it's not available, we return a default language object,
* regardless of the type passed in.
*
* Note that this function is provided for legacy code that needs to get e.g.
* the interface language outside the scope of a request. Code that runs within
* the scope of a request should call
* drupal_container()->get('language_manager')->getLanguage($type)
* directly.
*
* @see Drupal\Core\Language\LanguageManager
*/
function language_manager($type) {
static $default_language;
// Keep track of whether the language_manager has been initiated.
static $initiated = FALSE;
function language_manager($type = NULL) {
// Keep track of whether we are in a multilingual environment.
static $multilingual = FALSE;
$languages = &drupal_static(__FUNCTION__, array());
if ($multilingual && $initiated) {
if (!isset($languages[$type])) {
$languages[$type] = drupal_container()->get('language_manager')->getLanguage($type);
// Keep track of whether the language_manager service is available.
static $language_manager_service = FALSE;
if ($multilingual && $language_manager_service) {
if (!$type) {
drupal_container()->get('language_manager')->reset();
return;
}
return $languages[$type];
return drupal_container()->get('language_manager')->getLanguage($type);
}
if (language_multilingual()) {
$multilingual = TRUE;
if (drupal_container()->has('language_manager')) {
include_once DRUPAL_ROOT . '/core/includes/language.inc';
$initiated = TRUE;
$languages[$type] = drupal_container()->get('language_manager')->getLanguage($type);
return $languages[$type];
$language_manager_service = TRUE;
return drupal_container()->get('language_manager')->getLanguage($type);
}
}
// We can't use drupal_static() here because resetting is not a simple case of
// drupal_static_reset().
static $languages;
// If no type was passed in, reset the languages array.
if (!$type) {
$languages = array();
}
// If this is not a multilingual environment or we have not yet entered the
// request scope, just use the default language regardless of $type.
if (isset($default_language)) {
return $default_language;
if (!isset($languages[$type])) {
$languages[$type] = language_default();
}
$default_language = language_default();
return $default_language;
return $languages[$type];
}
/**
......
......@@ -1267,7 +1267,7 @@ function drupal_redirect_form($form_state) {
$function($form_state['redirect']);
}
}
drupal_goto(current_path(), array('query' => request()->query->all()));
drupal_goto(current_path(), array('query' => drupal_container()->get('request')->query->all()));
}
}
......
......@@ -255,11 +255,6 @@ function install_begin_request(&$install_state) {
// A request object from the HTTPFoundation to tell us about the request.
$request = Request::createFromGlobals();
// Set the global $request object. This is a temporary measure to
// keep legacy utility functions working. It should be moved to a dependency
// injection container at some point.
request($request);
// This must go after drupal_bootstrap(), which unsets globals!
global $conf;
......
......@@ -10,19 +10,30 @@
use Symfony\Component\HttpFoundation\Request;
/**
* Ajl
* The LanguageManager service intializes the language types passing in the
* Request object, which can then be used for e.g. url-based language negotiation.
*/
class LanguageManager {
private $request;
private $languages;
public function __construct(Request $request = NULL) {
$this->request = $request;
}
public function getLanguage($type) {
$language = language_types_initialize($type, array('request' => $this->request));
return $language;
if (isset($this->languages[$type])) {
return $this->languages[$type];
}
// @todo Objectify the language system so that we don't have to do this.
include_once DRUPAL_ROOT . '/core/includes/language.inc';
$this->languages[$type] = language_types_initialize($type, array('request' => $this->request));
return $this->languages[$type];
}
function reset() {
$this->languages = array();
}
}
\ No newline at end of file
......@@ -412,7 +412,7 @@ function aggregator_admin_refresh_feed($feed) {
// @todo CSRF tokens are validated in page callbacks rather than access
// callbacks, because access callbacks are also invoked during menu link
// generation. Add token support to routing: http://drupal.org/node/755584.
$token = request()->query->get('token');
$token = drupal_container()->get('request')->query->get('token');
if (!isset($token) || !drupal_valid_token($token, 'aggregator/update/' . $feed->fid)) {
throw new AccessDeniedHttpException();
}
......
......@@ -504,11 +504,11 @@ function comment_permalink($cid) {
$page = comment_get_display_page($comment->cid, $node->type);
// @todo: Cleaner sub request handling.
$subrequest = Request::create('/node/' . $node->nid, 'GET', request()->query->all(), request()->cookies->all(), array(), request()->server->all());
$request = drupal_container()->get('request');
$subrequest = Request::create('/node/' . $node->nid, 'GET', $request->query->all(), $request->cookies->all(), array(), $request->server->all());
$subrequest->query->set('page', $page);
// @todo: Convert the pager to use the request object.
$_GET['page'] = $page;
request($subrequest);
return drupal_container()->get('http_kernel')->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
}
throw new NotFoundHttpException();
......
......@@ -116,7 +116,7 @@ function comment_approve($cid) {
// @todo CSRF tokens are validated in page callbacks rather than access
// callbacks, because access callbacks are also invoked during menu link
// generation. Add token support to routing: http://drupal.org/node/755584.
$token = request()->query->get('token');
$token = drupal_container()->get('request')->query->get('token');
if (!isset($token) || !drupal_valid_token($token, "comment/$cid/approve")) {
throw new AccessDeniedHttpException();
}
......
......@@ -27,7 +27,7 @@ function setUp() {
parent::setUp('language');
// Ensure we are building a new Language object for each test.
drupal_static_reset('language_manager');
language_manager();
}
......@@ -60,6 +60,7 @@ function testDependencyInjectedNewDefaultLanguage() {
'name' => 'French',
'direction' => 0,
'weight' => 0,
'method_id' => 'language-default',
'default' => TRUE,
);
variable_set('language_default', $new_language_default);
......
......@@ -46,10 +46,9 @@ function testUninstallProcess() {
'default' => $this->langcode == 'fr',
);
language_save($language);
// Make sure the language interface object gets freshly initialized.
drupal_static_reset('language_manager');
// Reset the language_manager so that the language interface object gets
// freshly initialized.
language_manager();
// Check the UI language.
drupal_language_initialize();
$this->assertEqual(language_manager(LANGUAGE_TYPE_INTERFACE)->langcode, $this->langcode, t('Current language: %lang', array('%lang' => language_manager(LANGUAGE_TYPE_INTERFACE)->langcode)));
......@@ -94,7 +93,9 @@ function testUninstallProcess() {
// Visit the front page.
$this->drupalGet('');
// Reset the language_manager so that the language interface object gets
// freshly initialized.
language_manager();
// Check the init language logic.
drupal_language_initialize();
$this->assertEqual(language_manager(LANGUAGE_TYPE_INTERFACE)->langcode, 'en', t('Language after uninstall: %lang', array('%lang' => language_manager(LANGUAGE_TYPE_INTERFACE)->langcode)));
......
......@@ -333,7 +333,7 @@ function overlay_user_dismiss_message() {
// @todo CSRF tokens are validated in page callbacks rather than access
// callbacks, because access callbacks are also invoked during menu link
// generation. Add token support to routing: http://drupal.org/node/755584.
$token = request()->query->get('token');
$token = drupal_container()->get('request')->query->get('token');
if (!isset($token) || !drupal_valid_token($token, 'overlay')) {
throw new AccessDeniedHttpException();
}
......@@ -693,7 +693,7 @@ function overlay_overlay_child_initialize() {
// it to the same content rendered in overlay_exit(), at the end of the page
// request. This allows us to check if anything actually did change, and, if
// so, trigger an immediate Ajax refresh of the parent window.
$token = request()->query->get('token');
$token = drupal_container()->get('request')->query->get('token');
if (!empty($_POST) || isset($token)) {
foreach (overlay_supplemental_regions() as $region) {
overlay_store_rendered_content($region, overlay_render_region($region));
......
......@@ -2199,7 +2199,8 @@ function system_add_module_assets() {
* Implements hook_custom_theme().
*/
function system_custom_theme() {
if ($request = request()) {
if (drupal_container()->has('request')) {
$request = drupal_container()->get('request');
$path = $request->attributes->get('system_path');
if (user_access('view the administration theme') && path_is_admin($path)) {
return variable_get('admin_theme');
......
......@@ -33,7 +33,6 @@
// Continue with normal request handling.
$request = Request::createFromGlobals();
request($request);
drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
......
......@@ -32,7 +32,6 @@
// Continue with normal request handling.
$request = Request::createFromGlobals();
request($request);
drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
......
......@@ -498,7 +498,8 @@ function user_page() {
global $user;
if ($user->uid) {
// @todo: Cleaner sub request handling.
$subrequest = Request::create('/user/' . $user->uid, 'GET', request()->query->all(), request()->cookies->all(), array(), request()->server->all());
$request = drupal_container()->get('request');
$subrequest = Request::create('/user/' . $user->uid, 'GET', $request->query->all(), $request->cookies->all(), array(), $request->server->all());
return drupal_container()->get('http_kernel')->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
}
else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment