Commit d7421153 authored by alexpott's avatar alexpott

Issue #2426489 by dawehner: Remove request_uri()

parent cdfc9024
......@@ -457,35 +457,6 @@ function drupal_validate_utf8($text) {
return Unicode::validateUtf8($text);
}
/**
* Returns the equivalent of Apache's $_SERVER['REQUEST_URI'] variable.
*
* Because $_SERVER['REQUEST_URI'] is only available on Apache, we generate an
* equivalent using other environment variables.
*
* @todo The above comment is incorrect: http://drupal.org/node/1547294.
*/
function request_uri($omit_query_string = FALSE) {
if (isset($_SERVER['REQUEST_URI'])) {
$uri = $_SERVER['REQUEST_URI'];
}
else {
if (isset($_SERVER['argv'][0])) {
$uri = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['argv'][0];
}
elseif (isset($_SERVER['QUERY_STRING'])) {
$uri = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];
}
else {
$uri = $_SERVER['SCRIPT_NAME'];
}
}
// Prevent multiple slashes to avoid cross site requests via the Form API.
$uri = '/' . ltrim($uri, '/');
return $omit_query_string ? strtok($uri, '?') : $uri;
}
/**
* Logs an exception.
*
......
......@@ -869,10 +869,10 @@ protected function initializeRequestGlobals(Request $request) {
// are in effect and the script path can be similarly dropped from URL
// generation. For servers that don't provide $_SERVER['REQUEST_URI'],
// we do not know the actual URI requested by the client, and
// request_uri() returns a URI with the script name, resulting in
// non-clean URLs unless
// $request->getPathInfo() returns a URI with the script name,
// resulting in non-clean URLs unless
// there's other code that intervenes.
if (strpos(request_uri(TRUE) . '/', $base_path . $script_path) !== 0) {
if (strpos($request->getPathInfo() . '/', $base_path . $script_path) !== 0) {
$script_path = '';
}
// @todo Temporary BC for install.php, authorize.php, and other scripts.
......
......@@ -301,9 +301,9 @@ public function rebuildForm($form_id, FormStateInterface &$form_state, $old_form
$form['#build_id'] = 'form-' . Crypt::randomBytesBase64();
}
// #action defaults to request_uri(), but in case of Ajax and other partial
// rebuilds, the form is submitted to an alternate URL, and the original
// #action needs to be retained.
// #action defaults to $request->getRequestUri(), but in case of Ajax and
// other partial rebuilds, the form is submitted to an alternate URL, and
// the original #action needs to be retained.
if (isset($old_form['#action']) && !empty($rebuild_info['copy']['#action'])) {
$form['#action'] = $old_form['#action'];
}
......@@ -534,7 +534,7 @@ public function prepareForm($form_id, &$form, FormStateInterface &$form_state) {
// Only update the action if it is not already set.
if (!isset($form['#action'])) {
$form['#action'] = $this->requestUri();
$form['#action'] = $this->requestStack->getMasterRequest()->getRequestUri();
}
// Fix the form method, if it is 'get' in $form_state, but not in $form.
......@@ -1099,13 +1099,4 @@ protected function currentUser() {
return $this->currentUser;
}
/**
* Gets the current request URI.
*
* @return string
*/
protected function requestUri() {
return request_uri();
}
}
......@@ -147,7 +147,7 @@ private function generateLogEntries($count, $type = 'custom', $severity = RfcLog
'link' => NULL,
'user' => $this->adminUser,
'uid' => $this->adminUser->id(),
'request_uri' => $base_root . request_uri(),
'request_uri' => $base_root . \Drupal::request()->getRequestUri(),
'referer' => \Drupal::request()->server->get('HTTP_REFERER'),
'ip' => '127.0.0.1',
'timestamp' => REQUEST_TIME,
......@@ -456,7 +456,7 @@ public function testDBLogAddAndClear() {
'link' => NULL,
'user' => $this->adminUser,
'uid' => $this->adminUser->id(),
'request_uri' => $base_root . request_uri(),
'request_uri' => $base_root . \Drupal::request()->getRequestUri(),
'referer' => \Drupal::request()->server->get('HTTP_REFERER'),
'ip' => '127.0.0.1',
'timestamp' => REQUEST_TIME,
......
......@@ -12,7 +12,7 @@ services:
arguments: ['@keyvalue']
string_translator.locale.lookup:
class: Drupal\locale\LocaleTranslation
arguments: ['@locale.storage', '@cache.default', '@lock', '@config.factory', '@language_manager']
arguments: ['@locale.storage', '@cache.default', '@lock', '@config.factory', '@language_manager', '@request_stack']
tags:
- { name: string_translator }
- { name: needs_destruction }
......
......@@ -12,6 +12,7 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* A cache collector to allow for dynamic building of the locale cache.
......@@ -67,6 +68,13 @@ class LocaleLookup extends CacheCollector {
*/
protected $languageManager;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a LocaleLookup object.
*
......@@ -84,8 +92,10 @@ class LocaleLookup extends CacheCollector {
* The config factory.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct($langcode, $context, StringStorageInterface $string_storage, CacheBackendInterface $cache, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager) {
public function __construct($langcode, $context, StringStorageInterface $string_storage, CacheBackendInterface $cache, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, RequestStack $request_stack) {
$this->langcode = $langcode;
$this->context = (string) $context;
$this->stringStorage = $string_storage;
......@@ -95,6 +105,7 @@ public function __construct($langcode, $context, StringStorageInterface $string_
$this->cache = $cache;
$this->lock = $lock;
$this->tags = array('locale');
$this->requestStack = $request_stack;
}
/**
......@@ -142,7 +153,7 @@ protected function resolveCacheMiss($offset) {
'source' => $offset,
'context' => $this->context,
'version' => \Drupal::VERSION,
))->addLocation('path', $this->requestUri())->save();
))->addLocation('path', $this->requestStack->getCurrentRequest()->getRequestUri())->save();
$value = TRUE;
}
......@@ -177,11 +188,4 @@ protected function resolveCacheMiss($offset) {
return $value;
}
/**
* Wraps request_uri().
*/
protected function requestUri($omit_query_string = FALSE) {
return request_uri($omit_query_string);
}
}
......@@ -14,6 +14,7 @@
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* String translator using the locale module.
......@@ -74,6 +75,13 @@ class LocaleTranslation implements TranslatorInterface, DestructableInterface {
*/
protected $languageManager;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a translator using a string storage.
*
......@@ -87,13 +95,16 @@ class LocaleTranslation implements TranslatorInterface, DestructableInterface {
* The config factory.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(StringStorageInterface $storage, CacheBackendInterface $cache, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager) {
public function __construct(StringStorageInterface $storage, CacheBackendInterface $cache, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, RequestStack $request_stack) {
$this->storage = $storage;
$this->cache = $cache;
$this->lock = $lock;
$this->configFactory = $config_factory;
$this->languageManager = $language_manager;
$this->requestStack = $request_stack;
}
/**
......@@ -107,7 +118,7 @@ public function getStringTranslation($langcode, $string, $context) {
// Strings are cached by langcode, context and roles, using instances of the
// LocaleLookup class to handle string lookup and caching.
if (!isset($this->translations[$langcode][$context])) {
$this->translations[$langcode][$context] = new LocaleLookup($langcode, $context, $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager);
$this->translations[$langcode][$context] = new LocaleLookup($langcode, $context, $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack);
}
$translation = $this->translations[$langcode][$context]->get($string);
return $translation === TRUE ? FALSE : $translation;
......
......@@ -10,6 +10,8 @@
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\locale\LocaleLookup;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* @coversDefaultClass \Drupal\locale\LocaleLookup
......@@ -59,6 +61,13 @@ class LocaleLookupTest extends UnitTestCase {
*/
protected $languageManager;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* {@inheritdoc}
*/
......@@ -77,6 +86,7 @@ protected function setUp() {
$this->configFactory = $this->getConfigFactoryStub(array('locale.settings' => array('cache_strings' => FALSE)));
$this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
$this->requestStack = new RequestStack();
$container = new ContainerBuilder();
$container->set('current_user', $this->user);
......@@ -109,8 +119,8 @@ public function testResolveCacheMissWithoutFallback() {
->will($this->returnValue($result));
$locale_lookup = $this->getMockBuilder('Drupal\locale\LocaleLookup')
->setConstructorArgs(array('en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager))
->setMethods(array('persist', 'requestUri'))
->setConstructorArgs(array('en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack))
->setMethods(array('persist'))
->getMock();
$locale_lookup->expects($this->never())
->method('persist');
......@@ -175,7 +185,7 @@ public function testResolveCacheMissWithFallback($langcode, $string, $context, $
->method('get')
->with('locale:' . $langcode . ':' . $context . ':0', FALSE);
$locale_lookup = new LocaleLookup($langcode, $context, $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager);
$locale_lookup = new LocaleLookup($langcode, $context, $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack);
$this->assertSame($expected, $locale_lookup->get($string));
}
......@@ -222,8 +232,8 @@ public function testResolveCacheMissWithPersist() {
$this->configFactory = $this->getConfigFactoryStub(array('locale.settings' => array('cache_strings' => TRUE)));
$locale_lookup = $this->getMockBuilder('Drupal\locale\LocaleLookup')
->setConstructorArgs(array('en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager))
->setMethods(array('persist', 'requestUri'))
->setConstructorArgs(array('en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack))
->setMethods(array('persist'))
->getMock();
$locale_lookup->expects($this->once())
->method('persist');
......@@ -248,9 +258,12 @@ public function testResolveCacheMissNoTranslation() {
->method('createString')
->will($this->returnValue($string));
$request = Request::create('/test');
$this->requestStack->push($request);
$locale_lookup = $this->getMockBuilder('Drupal\locale\LocaleLookup')
->setConstructorArgs(array('en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager))
->setMethods(array('persist', 'requestUri'))
->setConstructorArgs(array('en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack))
->setMethods(array('persist'))
->getMock();
$locale_lookup->expects($this->never())
->method('persist');
......
......@@ -9,6 +9,7 @@
use Drupal\locale\LocaleTranslation;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* @coversDefaultClass \Drupal\locale\LocaleTranslation
......@@ -30,6 +31,13 @@ class LocaleTranslationTest extends UnitTestCase {
*/
protected $languageManager;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* {@inheritdoc}
*/
......@@ -38,13 +46,14 @@ protected function setUp() {
$this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
$this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
$this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
$this->requestStack = new RequestStack();
}
/**
* Tests for \Drupal\locale\LocaleTranslation::destruct().
*/
public function testDestruct() {
$translation = new LocaleTranslation($this->storage, $this->cache, $this->lock, $this->getConfigFactoryStub(), $this->languageManager);
$translation = new LocaleTranslation($this->storage, $this->cache, $this->lock, $this->getConfigFactoryStub(), $this->languageManager, $this->requestStack);
// Prove that destruction works without errors when translations are empty.
$this->assertAttributeEmpty('translations', $translation);
$translation->destruct();
......
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