diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index b7e3e64..4d89586 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -308,7 +308,8 @@ public function build(ContainerBuilder $container) { ->register('transliteration', 'Drupal\Core\Transliteration\PHPTransliteration'); $container->register('flood', 'Drupal\Core\Flood\DatabaseBackend') - ->addArgument(new Reference('database')); + ->addArgument(new Reference('database')) + ->addArgument(new Reference('request')); $container->register('plugin.manager.condition', 'Drupal\Core\Condition\ConditionManager'); diff --git a/core/lib/Drupal/Core/EventSubscriber/ReverseProxySubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ReverseProxySubscriber.php index b7b45d6..926d36d 100644 --- a/core/lib/Drupal/Core/EventSubscriber/ReverseProxySubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/ReverseProxySubscriber.php @@ -24,6 +24,12 @@ class ReverseProxySubscriber implements EventSubscriberInterface { */ protected $settings; + /** + * Construct the ReverseProxySubscriber. + * + * @param \Drupal\Component\Utility\Settings $settings + * The read-only settings object of this request. + */ public function __construct(Settings $settings) { $this->settings = $settings; } diff --git a/core/lib/Drupal/Core/Flood/DatabaseBackend.php b/core/lib/Drupal/Core/Flood/DatabaseBackend.php index 54a8ffa..0bc30d1 100644 --- a/core/lib/Drupal/Core/Flood/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Flood/DatabaseBackend.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Flood; +use Symfony\Component\HttpFoundation\Request; use Drupal\Core\Database\Connection; /** @@ -22,14 +23,24 @@ class DatabaseBackend implements FloodInterface { protected $connection; /** + * A request object. + * + * @var \Symfony\Component\HttpFoundation\Request + */ + protected $request; + + /** * Construct the DatabaseBackend. * * @param \Drupal\Core\Database\Connection $connection * The database connection which will be used to store the flood event * information. + * @param \Symfony\Component\HttpFoundation\Request $request + * The HttpRequest object representing the current request. */ - public function __construct(Connection $connection) { + public function __construct(Connection $connection, Request $request) { $this->connection = $connection; + $this->request = $request; } /** @@ -37,7 +48,7 @@ public function __construct(Connection $connection) { */ public function register($name, $window = 3600, $identifier = NULL) { if (!isset($identifier)) { - $identifier = \Drupal::service('request')->getClientIP(); + $identifier = $this->request->getClientIp(); } $this->connection->insert('flood') ->fields(array( @@ -54,7 +65,7 @@ public function register($name, $window = 3600, $identifier = NULL) { */ public function clear($name, $identifier = NULL) { if (!isset($identifier)) { - $identifier = \Drupal::service('request')->getClientIP(); + $identifier = $this->request->getClientIp(); } $this->connection->delete('flood') ->condition('event', $name) @@ -67,7 +78,7 @@ public function clear($name, $identifier = NULL) { */ public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) { if (!isset($identifier)) { - $identifier = \Drupal::service('request')->getClientIP(); + $identifier = $this->request->getClientIp(); } $number = $this->connection->select('flood', 'f') ->condition('event', $name) diff --git a/core/lib/Drupal/Core/Flood/MemoryBackend.php b/core/lib/Drupal/Core/Flood/MemoryBackend.php index ff49eff..13ab55a 100644 --- a/core/lib/Drupal/Core/Flood/MemoryBackend.php +++ b/core/lib/Drupal/Core/Flood/MemoryBackend.php @@ -7,22 +7,41 @@ namespace Drupal\Core\Flood; +use Symfony\Component\HttpFoundation\Request; + /** * Defines the memory flood backend. This is used for testing. */ class MemoryBackend implements FloodInterface { /** + * A request object. + * + * @var \Symfony\Component\HttpFoundation\Request + */ + protected $request; + + /** * An array holding flood events, keyed by event name and identifier. */ protected $events = array(); /** + * Construct the MemoryBackend. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The HttpRequest object representing the current request. + */ + public function __construct(Request $request) { + $this->request = $request; + } + + /** * Implements Drupal\Core\Flood\FloodInterface::register(). */ public function register($name, $window = 3600, $identifier = NULL) { if (!isset($identifier)) { - $identifier = \Drupal::service('request')->getClientIP(); + $identifier = $this->request->getClientIP(); } // We can't use REQUEST_TIME here, because that would not guarantee // uniqueness. @@ -35,7 +54,7 @@ public function register($name, $window = 3600, $identifier = NULL) { */ public function clear($name, $identifier = NULL) { if (!isset($identifier)) { - $identifier = \Drupal::service('request')->getClientIP(); + $identifier = $this->request->getClientIP(); } unset($this->events[$name][$identifier]); } @@ -45,7 +64,7 @@ public function clear($name, $identifier = NULL) { */ public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) { if (!isset($identifier)) { - $identifier = \Drupal::service('request')->getClientIP(); + $identifier = $this->request->getClientIP(); } $limit = microtime(true) - $window; $number = count(array_filter($this->events[$name][$identifier], function ($timestamp) use ($limit) { diff --git a/core/modules/system/lib/Drupal/system/Tests/System/FloodTest.php b/core/modules/system/lib/Drupal/system/Tests/System/FloodTest.php index 6af396b..b3e8662 100644 --- a/core/modules/system/lib/Drupal/system/Tests/System/FloodTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/System/FloodTest.php @@ -55,7 +55,7 @@ function testMemoryBackend() { $window_expired = -1; $name = 'flood_test_cleanup'; - $flood = new \Drupal\Core\Flood\MemoryBackend; + $flood = new \Drupal\Core\Flood\MemoryBackend($this->kernel->getContainer()->get('request')); // Register expired event. $flood->register($name, $window_expired); // Verify event is not allowed.