vendor/shapecode/cron-bundle/src/EventListener/AnnotationJobLoaderListener.php line 57

Open in your IDE?
  1. <?php
  2. namespace Shapecode\Bundle\CronBundle\EventListener;
  3. use Doctrine\Common\Annotations\Reader;
  4. use Shapecode\Bundle\CronBundle\Annotation\CronJob;
  5. use Shapecode\Bundle\CronBundle\Event\LoadJobsEvent;
  6. use Shapecode\Bundle\CronBundle\Model\CronJobMetadata;
  7. use Symfony\Bundle\FrameworkBundle\Console\Application;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\KernelInterface;
  10. /**
  11.  * Class AnnotationJobLoaderListener
  12.  *
  13.  * @package Shapecode\Bundle\CronBundle\EventListener
  14.  * @author  Nikita Loges
  15.  */
  16. class AnnotationJobLoaderListener implements EventSubscriberInterface
  17. {
  18.     /** @var KernelInterface */
  19.     protected $kernel;
  20.     /** @var Application */
  21.     protected $application;
  22.     /** @var Reader */
  23.     protected $reader;
  24.     /**
  25.      * @param KernelInterface $kernel
  26.      * @param Reader          $reader
  27.      */
  28.     public function __construct(KernelInterface $kernelReader $reader)
  29.     {
  30.         $this->kernel $kernel;
  31.         $this->application = new Application($kernel);
  32.         $this->reader $reader;
  33.     }
  34.     /**
  35.      * @inheritdoc
  36.      */
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             LoadJobsEvent::NAME => 'onLoadJobs'
  41.         ];
  42.     }
  43.     /**
  44.      * @param LoadJobsEvent $event
  45.      *
  46.      * @throws \ReflectionException
  47.      */
  48.     public function onLoadJobs(LoadJobsEvent $event): void
  49.     {
  50.         foreach ($this->application->all() as $command) {
  51.             // Check for an @CronJob annotation
  52.             $reflClass = new \ReflectionClass($command);
  53.             foreach ($this->reader->getClassAnnotations($reflClass) as $annotation) {
  54.                 if ($annotation instanceof CronJob) {
  55.                     $schedule $annotation->value;
  56.                     $arguments $annotation->getArguments();
  57.                     $meta CronJobMetadata::createByCommand($schedule$command$arguments);
  58.                     $event->addJob($meta);
  59.                 }
  60.             }
  61.         }
  62.     }
  63. }