class HttpMiddleware in File Link 2.0.x
A middleware for guzzle to test requests.
Hierarchy
- class \Drupal\file_link_test\HttpMiddleware
Expanded class hierarchy of HttpMiddleware
2 files declare their use of HttpMiddleware
- FileLinkDeferredTest.php in tests/
src/ Kernel/ FileLinkDeferredTest.php - FileLinkMultiValueTest.php in tests/
src/ Kernel/ FileLinkMultiValueTest.php
1 string reference to 'HttpMiddleware'
- file_link_test.services.yml in tests/
modules/ file_link_test/ file_link_test.services.yml - tests/modules/file_link_test/file_link_test.services.yml
1 service uses HttpMiddleware
- file_link_test.client_middleware in tests/
modules/ file_link_test/ file_link_test.services.yml - Drupal\file_link_test\HttpMiddleware
File
- tests/
modules/ file_link_test/ src/ HttpMiddleware.php, line 13
Namespace
Drupal\file_link_testView source
class HttpMiddleware {
public static $recorder = [];
public static function getRequestCount($key) {
if (!isset(static::$recorder[$key])) {
static::$recorder[$key] = 0;
}
return static::$recorder[$key];
}
/**
* Invoked method that returns a promise.
*/
public function __invoke() {
return function ($handler) {
return function (RequestInterface $request, array $options) use ($handler) {
$uri = $request
->getUri();
$settings = Settings::get('file_link_test_middleware', []);
// Check if the request is made to one of our fixtures.
$key = $uri
->getScheme() . '://' . $uri
->getHost() . $uri
->getPath();
if (array_key_exists($key, $settings)) {
if (!isset(static::$recorder[$key])) {
static::$recorder[$key] = 0;
}
static::$recorder[$key]++;
return $this
->createPromise($request, $settings[$key]);
}
// Otherwise, no intervention. We defer to the handler stack.
return $handler($request, $options);
};
};
}
/**
* Creates a promise for the file_link fixture request.
*
* @param RequestInterface $request
*
* @return \GuzzleHttp\Promise\PromiseInterface
*/
protected function createPromise(RequestInterface $request, $fixture) {
// Create a response from the fixture.
$response = new Response($fixture['status'] ?? 200, $fixture['headers'] ?? [], $fixture['body'] ?? NULL);
return new FulfilledPromise($response);
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
HttpMiddleware:: |
public static | property | ||
HttpMiddleware:: |
protected | function | Creates a promise for the file_link fixture request. | |
HttpMiddleware:: |
public static | function | ||
HttpMiddleware:: |
public | function | Invoked method that returns a promise. |