diff --git a/src/Cronard.php b/src/Cronard.php index 3e46c08..32a66f4 100644 --- a/src/Cronard.php +++ b/src/Cronard.php @@ -55,7 +55,7 @@ class Cronard { unset($this->callback); } - public function run(array $arguments = [], Closure $callback = null) + public function run(array $arguments = [], ? Closure $callback = null) { if ( $this->validateCron($this->tab) ) { if ( null !== ( $callback = $callback ?? $this->callback ?? null ) ) { diff --git a/src/CronardMiddleware.php b/src/CronardMiddleware.php index 4643a85..54e078b 100644 --- a/src/CronardMiddleware.php +++ b/src/CronardMiddleware.php @@ -24,12 +24,11 @@ class CronardMiddleware implements MiddlewareInterface public ? TaskFetcher $taskFetcher; - public function __construct(ContainerInterface $container, string $cronKey = "", $responseInterface, $variables = [], ? TaskFetcher $fetcher = null) { + public function __construct(ContainerInterface $container, string $cronKey = "", $responseInterface, $variables = []) { $this->cronKey = $cronKey; $this->response = $responseInterface; $this->variables = $variables; $this->container = $container; - $this->taskFetcher = $fetcher; } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface diff --git a/src/CronardTrait.php b/src/CronardTrait.php index a9b4f16..efbaf1b 100644 --- a/src/CronardTrait.php +++ b/src/CronardTrait.php @@ -4,6 +4,11 @@ namespace Cronard; use Notes\Cronard\TaskFetcher; +use Psr\Container\ContainerInterface; + +use Psr\Http\Message\ResponseInterface, + Psr\Http\Message\ServerRequestInterface; + trait CronardTrait { public array $crontabs = []; @@ -12,9 +17,9 @@ trait CronardTrait { { $cronard = new Cronard(); - foreach($this->crontabs as $tab => $callback) { - $cronard->setTab($tab); - $cronard->run(array_merge($this->variables, $variables), $callback); + foreach($this->crontabs as $tab) { + $cronard->setTab(stripslashes($tab['cron'])); + $cronard->run(array_merge($this->variables, $variables), $tab['callback']); } } @@ -24,8 +29,13 @@ trait CronardTrait { throw new \RuntimeException("Given crontab file cannot be found. There could also be a problem at permission level."); } - $this->crontabs = array_merge($this->crontabs, include($filepath)); - + foreach(include($filepath) as $tab => $callback) { + $this->crontabs[] = [ + 'cron' => $tab, + 'callback' => $callback, + ]; + } + return $this; } @@ -33,12 +43,19 @@ trait CronardTrait { { $tasks = []; - /* foreach($fetcher->compile() as $task) { - dump($task); + foreach($fetcher->compile() as $task) { + $this->crontabs[] = [ + 'cron' => $task['annotation']->cron, + 'callback' => function(ServerRequestInterface $request, ResponseInterface $response, ContainerInterface $container) use ($task) { + if ( $task['annotation']->method ?? false ) { + $request = $request->withMethod($task['annotation']->method); + } + + return $container->make($task['class'])->{$task['method']}($request->withAttribute('lean.cronard', $task['annotation']), []); + } + ]; } - $this->crontabs = array_merge($this->crontabs, $tasks); -*/ return $this; } }