- First commit of this WIP

This commit is contained in:
mcndave committed 2019-08-27 09:29:53 -04:00
commit d3bd727517
4 files changed
+376

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
<?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
{
public string $cronKey;
public array $cronards = [];
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);
$cronard = new Cronard();
foreach($this->cronards as $tab => $callback) {
$cronard->setTab($tab);
$cronard->run(array_merge([
$request,
$response,
], $this->variables), $callback);
}
return $response;
}
return $handler->handle($request);
}
public function fromFile(string $filepath) {
if ( ! file_exists($filepath) ) {
throw new \RuntimeException("Given crontab file cannot be found. There could also be a problem at permission level.");
}
$this->cronards = include($filepath);
return $this;
}
}