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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Shapecode\Bundle\CronBundle\EventListener;
  4. use Doctrine\Common\Annotations\Reader;
  5. use ReflectionClass;
  6. use Shapecode\Bundle\CronBundle\Annotation\CronJob;
  7. use Shapecode\Bundle\CronBundle\Event\LoadJobsEvent;
  8. use Shapecode\Bundle\CronBundle\Model\CronJobMetadata;
  9. use Symfony\Bundle\FrameworkBundle\Console\Application;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\KernelInterface;
  12. final class AnnotationJobLoaderListener implements EventSubscriberInterface
  13. {
  14.     /** @var Application */
  15.     private $application;
  16.     /** @var Reader */
  17.     private $reader;
  18.     public function __construct(KernelInterface $kernelReader $reader)
  19.     {
  20.         $this->application = new Application($kernel);
  21.         $this->reader      $reader;
  22.     }
  23.     /**
  24.      * @inheritDoc
  25.      */
  26.     public static function getSubscribedEvents() : array
  27.     {
  28.         return [
  29.             LoadJobsEvent::NAME => 'onLoadJobs',
  30.         ];
  31.     }
  32.     public function onLoadJobs(LoadJobsEvent $event) : void
  33.     {
  34.         foreach ($this->application->all() as $command) {
  35.             // Check for an @CronJob annotation
  36.             $reflClass = new ReflectionClass($command);
  37.             foreach ($this->reader->getClassAnnotations($reflClass) as $annotation) {
  38.                 if (! ($annotation instanceof CronJob)) {
  39.                     continue;
  40.                 }
  41.                 $schedule     $annotation->value;
  42.                 $arguments    $annotation->arguments;
  43.                 $maxInstances $annotation->maxInstances;
  44.                 $meta CronJobMetadata::createByCommand($schedule$command$arguments$maxInstances);
  45.                 $event->addJob($meta);
  46.             }
  47.         }
  48.     }
  49. }