- Splitted some code to add a new Trait instead, which allows reusability
This commit is contained in:
parent
d3bd727517
commit
93ada0e6de
@ -23,7 +23,7 @@ class Cronard {
|
|||||||
protected $output = true;
|
protected $output = true;
|
||||||
|
|
||||||
public function __construct(? string $tab = null) {
|
public function __construct(? string $tab = null) {
|
||||||
if ($tab) {
|
if ( $tab ) {
|
||||||
$this->setTab($tab);
|
$this->setTab($tab);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ use Psr\Http\Message\ResponseFactoryInterface,
|
|||||||
|
|
||||||
class CronardMiddleware implements MiddlewareInterface
|
class CronardMiddleware implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
public string $cronKey;
|
use CronardTrait;
|
||||||
|
|
||||||
public array $cronards = [];
|
public string $cronKey;
|
||||||
|
|
||||||
public array $variables = [];
|
public array $variables = [];
|
||||||
|
|
||||||
@ -29,28 +29,15 @@ class CronardMiddleware implements MiddlewareInterface
|
|||||||
{
|
{
|
||||||
if ( $this->cronKey === ( $request->getQueryParams()['cronkey'] ?? false ) ) {
|
if ( $this->cronKey === ( $request->getQueryParams()['cronkey'] ?? false ) ) {
|
||||||
$response = $this->response->call($this);
|
$response = $this->response->call($this);
|
||||||
$cronard = new Cronard();
|
|
||||||
|
|
||||||
foreach($this->cronards as $tab => $callback) {
|
$this->launch([
|
||||||
$cronard->setTab($tab);
|
|
||||||
$cronard->run(array_merge([
|
|
||||||
$request,
|
$request,
|
||||||
$response,
|
$response,
|
||||||
], $this->variables), $callback);
|
]);
|
||||||
}
|
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $handler->handle($request);
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
28
src/CronardTrait.php
Normal file
28
src/CronardTrait.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Cronard;
|
||||||
|
|
||||||
|
trait CronardTrait {
|
||||||
|
|
||||||
|
public array $crontabs = [];
|
||||||
|
|
||||||
|
public function launch(array $variables) : void
|
||||||
|
{
|
||||||
|
$cronard = new Cronard();
|
||||||
|
|
||||||
|
foreach($this->crontabs as $tab => $callback) {
|
||||||
|
$cronard->setTab($tab);
|
||||||
|
$cronard->run(array_merge($this->variables, $variables), $callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fromFile(string $filepath) : self
|
||||||
|
{
|
||||||
|
if ( ! file_exists($filepath) ) {
|
||||||
|
throw new \RuntimeException("Given crontab file cannot be found. There could also be a problem at permission level.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->crontabs = include($filepath);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
@ -1,18 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "mcnd/cronard",
|
|
||||||
"description": "Cron made simple. Call this every minute and attach a task to your function",
|
|
||||||
"type": "library",
|
|
||||||
"license": "MIT",
|
|
||||||
"authors": [
|
|
||||||
{
|
|
||||||
"name": "Dave Mc Nicoll",
|
|
||||||
"email": "mcndave@gmail.com"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"require": {},
|
|
||||||
"autoload": {
|
|
||||||
"psr-4": {
|
|
||||||
"Cronard\\": "src/"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user