cronard/src/CronardMiddleware.php

44 lines
1.1 KiB
PHP

<?php
namespace Cronard;
use Psr\Http\Message\ResponseFactoryInterface,
Psr\Container\ContainerInterface,
Psr\Http\Message\ResponseInterface,
Psr\Http\Message\ServerRequestInterface,
Psr\Http\Server\MiddlewareInterface,
Psr\Http\Server\RequestHandlerInterface;
class CronardMiddleware implements MiddlewareInterface
{
use CronardTrait;
public string $cronKey;
public array $variables = [];
public $response;
public function __construct(string $cronKey = "", $responseInterface, $variables = []) {
$this->cronKey = $cronKey;
$this->response = $responseInterface;
$this->variables = $variables;
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ( $this->cronKey === ( $request->getQueryParams()['cronkey'] ?? false ) ) {
$response = $this->response->call($this);
$this->launch([
$request,
$response,
]);
return $response;
}
return $handler->handle($request);
}
}