HttpMiddleware.php in File Link 2.0.x
File
tests/modules/file_link_test/src/HttpMiddleware.php
View source
<?php
namespace Drupal\file_link_test;
use Drupal\Core\Site\Settings;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
class HttpMiddleware {
public static $recorder = [];
public static function getRequestCount($key) {
if (!isset(static::$recorder[$key])) {
static::$recorder[$key] = 0;
}
return static::$recorder[$key];
}
public function __invoke() {
return function ($handler) {
return function (RequestInterface $request, array $options) use ($handler) {
$uri = $request
->getUri();
$settings = Settings::get('file_link_test_middleware', []);
$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]);
}
return $handler($request, $options);
};
};
}
protected function createPromise(RequestInterface $request, $fixture) {
$response = new Response($fixture['status'] ?? 200, $fixture['headers'] ?? [], $fixture['body'] ?? NULL);
return new FulfilledPromise($response);
}
}