diff --git a/core/core.api.php b/core/core.api.php index 63fd607..9f1d4f8 100644 --- a/core/core.api.php +++ b/core/core.api.php @@ -2190,6 +2190,9 @@ function hook_validation_constraint_alter(array &$definitions) { * - context_provider: Indicates a block context provider, used for example * by block conditions. It has to implement * \Drupal\Core\Plugin\Context\ContextProviderInterface. + * - http_client_middleware: Indicates that the service provides a guzzle + * middleware, see https://guzzle.readthedocs.org/en/latest/handlers-and-middleware.html + * for more information. * * Creating a tag for a service does not do anything on its own, but tags * can be discovered or queried in a compiler pass when the container is built, diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index 9ff3861..baebc75 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -403,6 +403,8 @@ public static function httpClient() { } /** + * Returns a factory for guzzle HTTP clients with Drupal drupal options. + * * @return \Drupal\Core\Http\ClientFactory */ public static function httpClientFactory() { diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/GuzzleMiddlewarePass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/GuzzleMiddlewarePass.php index d2523cf..42e403c 100644 --- a/core/lib/Drupal/Core/DependencyInjection/Compiler/GuzzleMiddlewarePass.php +++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/GuzzleMiddlewarePass.php @@ -9,7 +9,6 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Reference; class GuzzleMiddlewarePass implements CompilerPassInterface { diff --git a/core/lib/Drupal/Core/Http/ClientFactory.php b/core/lib/Drupal/Core/Http/ClientFactory.php index b9d597a..f6471e5 100644 --- a/core/lib/Drupal/Core/Http/ClientFactory.php +++ b/core/lib/Drupal/Core/Http/ClientFactory.php @@ -44,7 +44,7 @@ public function __construct(HandlerStack $stack) { * The HTTP client. */ public function fromOptions(array $config = []) { - $default_config = array( + $default_config = [ // Security consideration: we must not use the certificate authority // file shipped with Guzzle because it can easily get outdated if a // certificate authority is hacked. Instead, we rely on the certificate @@ -53,16 +53,16 @@ public function fromOptions(array $config = []) { // path to the pem file bundled with Guzzle. 'verify' => TRUE, 'timeout' => 30, - 'headers' => array( + 'headers' => [ 'User-Agent' => 'Drupal/' . \Drupal::VERSION . ' (+https://www.drupal.org/) ' . \GuzzleHttp\default_user_agent(), - ), + ], 'handler' => $this->stack, - ); + ]; // The entire config array is merged/configurable to allow Guzzle client // options outside of 'defaults' to be changed, such as 'adapter', or // 'message_factory'. - $config = NestedArray::mergeDeep($config, $default_config, Settings::get('http_client_config', array())); + $config = NestedArray::mergeDeep($config, $default_config, Settings::get('http_client_config', [])); return new Client($config); } diff --git a/core/lib/Drupal/Core/Http/HandlerStackConfigurator.php b/core/lib/Drupal/Core/Http/HandlerStackConfigurator.php index 3dfc6b6..8e9e36a 100644 --- a/core/lib/Drupal/Core/Http/HandlerStackConfigurator.php +++ b/core/lib/Drupal/Core/Http/HandlerStackConfigurator.php @@ -19,6 +19,8 @@ * as http_client_middleware. Each service must contain an __invoke method that * returns a closure which will serve as the middleware. * + * @see https://guzzle.readthedocs.org/en/latest/handlers-and-middleware.html + * * @see \Drupal\Core\Http\Client * @see \Drupal\Core\Test\EventSubscriber\HttpRequestSubscriber */ @@ -66,9 +68,12 @@ protected function initializeMiddlewares() { $this->middlewares = []; foreach ($this->middlewareIds as $middleware_id) { $middleware = $this->container->get($middleware_id); - if (method_exists($middleware, '__invoke')) { + if (is_callable($middleware)) { $this->middlewares[$middleware_id] = $middleware(); } + else { + throw new \InvalidArgumentException('Middlewares need to implement __invoke, see https://guzzle.readthedocs.org/en/latest/handlers-and-middleware.html for more information about middlewares.'); + } } } } diff --git a/core/lib/Drupal/Core/Test/EventSubscriber/HttpRequestSubscriber.php b/core/lib/Drupal/Core/Test/EventSubscriber/HttpRequestSubscriber.php index e3d2504..1f8f576 100644 --- a/core/lib/Drupal/Core/Test/EventSubscriber/HttpRequestSubscriber.php +++ b/core/lib/Drupal/Core/Test/EventSubscriber/HttpRequestSubscriber.php @@ -8,7 +8,6 @@ namespace Drupal\Core\Test\EventSubscriber; use Drupal\Core\Http\StackHandlerRegistrationInterface; -use GuzzleHttp\HandlerStack; use Psr\Http\Message\RequestInterface; /** diff --git a/core/modules/statistics/src/Tests/StatisticsAdminTest.php b/core/modules/statistics/src/Tests/StatisticsAdminTest.php index cdd8a48..9762d0a 100644 --- a/core/modules/statistics/src/Tests/StatisticsAdminTest.php +++ b/core/modules/statistics/src/Tests/StatisticsAdminTest.php @@ -40,7 +40,7 @@ class StatisticsAdminTest extends WebTestBase { /** * The Guzzle HTTP client. * - * @var \GuzzleHttp\ClientInterface; + * @var \GuzzleHttp\Client; */ protected $client; @@ -78,16 +78,16 @@ function testStatisticsSettings() { $post = array('nid' => $nid); global $base_url; $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php'; - $this->client->post($stats_path, array('body' => $post)); + $this->client->post($stats_path, array('form_params' => $post)); // Hit the node again (the counter is incremented after the hit, so // "1 view" will actually be shown when the node is hit the second time). $this->drupalGet('node/' . $this->testNode->id()); - $this->client->post($stats_path, array('body' => $post)); + $this->client->post($stats_path, array('form_params' => $post)); $this->assertText('1 view', 'Node is viewed once.'); $this->drupalGet('node/' . $this->testNode->id()); - $this->client->post($stats_path, array('body' => $post)); + $this->client->post($stats_path, array('form_params' => $post)); $this->assertText('2 views', 'Node is viewed 2 times.'); } @@ -103,7 +103,7 @@ function testDeleteNode() { $post = array('nid' => $nid); global $base_url; $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php'; - $this->client->post($stats_path, array('body' => $post)); + $this->client->post($stats_path, array('form_params' => $post)); $result = db_select('node_counter', 'n') ->fields('n', array('nid')) @@ -137,9 +137,9 @@ function testExpiredLogs() { $post = array('nid' => $nid); global $base_url; $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php'; - $this->client->post($stats_path, array('body' => $post)); + $this->client->post($stats_path, array('form_params' => $post)); $this->drupalGet('node/' . $this->testNode->id()); - $this->client->post($stats_path, array('body' => $post)); + $this->client->post($stats_path, array('form_params' => $post)); $this->assertText('1 view', 'Node is viewed once.'); // statistics_cron() will subtract diff --git a/core/modules/statistics/src/Tests/StatisticsLoggingTest.php b/core/modules/statistics/src/Tests/StatisticsLoggingTest.php index ce58e0e..c26ea04 100644 --- a/core/modules/statistics/src/Tests/StatisticsLoggingTest.php +++ b/core/modules/statistics/src/Tests/StatisticsLoggingTest.php @@ -36,7 +36,7 @@ class StatisticsLoggingTest extends WebTestBase { /** * The Guzzle HTTP client. * - * @var \GuzzleHttp\ClientInterface; + * @var \GuzzleHttp\Client; */ protected $client; @@ -94,7 +94,7 @@ function testLogging() { // Manually call statistics.php to simulate ajax data collection behavior. $nid = $this->node->id(); $post = array('nid' => $nid); - $this->client->post($stats_path, array('body' => $post)); + $this->client->post($stats_path, array('form_params' => $post)); $node_counter = statistics_get($this->node->id()); $this->assertIdentical($node_counter['totalcount'], '1'); } diff --git a/core/modules/statistics/src/Tests/Views/IntegrationTest.php b/core/modules/statistics/src/Tests/Views/IntegrationTest.php index d83a387..a186779 100644 --- a/core/modules/statistics/src/Tests/Views/IntegrationTest.php +++ b/core/modules/statistics/src/Tests/Views/IntegrationTest.php @@ -76,9 +76,8 @@ public function testNodeCounterIntegration() { // @see \Drupal\statistics\Tests\StatisticsLoggingTest::testLogging(). global $base_url; $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php'; - $client = \Drupal::httpClient(); - $client->setDefaultOption('config/curl', array(CURLOPT_TIMEOUT => 10)); - $client->post($stats_path, array('body' => array('nid' => $this->node->id()))); + $client = \Drupal::httpClientFactory()->fromOptions(['config/curl', array(CURLOPT_TIMEOUT => 10)]); + $client->post($stats_path, array('form_params' => array('nid' => $this->node->id()))); $this->drupalGet('test_statistics_integration'); $expected = statistics_get($this->node->id());