vendor/shapecode/cron-bundle/src/EventListener/ServiceJobLoaderListener.php line 45

Open in your IDE?
  1. <?php
  2. namespace Shapecode\Bundle\CronBundle\EventListener;
  3. use Shapecode\Bundle\CronBundle\Event\LoadJobsEvent;
  4. use Shapecode\Bundle\CronBundle\Model\CronJobMetadata;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. /**
  8.  * Class ServiceJobLoaderListener
  9.  *
  10.  * @package Shapecode\Bundle\CronBundle\EventListener
  11.  * @author  Nikita Loges
  12.  */
  13. class ServiceJobLoaderListener implements EventSubscriberInterface
  14. {
  15.     /** @var CronJobMetadata[] */
  16.     protected $jobs = [];
  17.     /**
  18.      * @param string      $expression
  19.      * @param Command     $command
  20.      * @param null|string $arguments
  21.      */
  22.     public function addCommand(string $expressionCommand $command, ?string $arguments null): void
  23.     {
  24.         $this->jobs[] = CronJobMetadata::createByCommand($expression$command$arguments);
  25.     }
  26.     /**
  27.      * @inheritdoc
  28.      */
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return [
  32.             LoadJobsEvent::NAME => 'onLoadJobs'
  33.         ];
  34.     }
  35.     /**
  36.      * @param LoadJobsEvent $event
  37.      */
  38.     public function onLoadJobs(LoadJobsEvent $event): void
  39.     {
  40.         foreach ($this->jobs as $job) {
  41.             $event->addJob($job);
  42.         }
  43.     }
  44. }