vendor/contao/core-bundle/src/EventListener/CommandSchedulerListener.php line 61

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\EventListener;
  11. use Contao\Config;
  12. use Contao\CoreBundle\Cron\Cron;
  13. use Contao\CoreBundle\Framework\ContaoFramework;
  14. use Doctrine\DBAL\Connection;
  15. use Doctrine\DBAL\Exception\DriverException;
  16. use Psr\Container\ContainerInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  19. use Symfony\Contracts\Service\ServiceSubscriberInterface;
  20. /**
  21.  * @internal
  22.  */
  23. class CommandSchedulerListener implements ServiceSubscriberInterface
  24. {
  25.     /**
  26.      * @var ContainerInterface
  27.      */
  28.     private $locator;
  29.     /**
  30.      * @var ContaoFramework
  31.      */
  32.     private $framework;
  33.     /**
  34.      * @var Connection
  35.      */
  36.     private $connection;
  37.     /**
  38.      * @var string
  39.      */
  40.     private $fragmentPath;
  41.     public function __construct(ContainerInterface $locatorContaoFramework $frameworkConnection $connectionstring $fragmentPath '_fragment')
  42.     {
  43.         $this->locator $locator;
  44.         $this->framework $framework;
  45.         $this->connection $connection;
  46.         $this->fragmentPath $fragmentPath;
  47.     }
  48.     /**
  49.      * Runs the command scheduler.
  50.      */
  51.     public function __invoke(TerminateEvent $event): void
  52.     {
  53.         if ($this->framework->isInitialized() && $this->canRunCron($event->getRequest())) {
  54.             $this->locator->get(Cron::class)->run(Cron::SCOPE_WEB);
  55.         }
  56.     }
  57.     /**
  58.      * Lazy-load services to prevent issues with MySQL server_version.
  59.      *
  60.      * @see https://github.com/contao/contao/pull/3623
  61.      *
  62.      * @return array<string>
  63.      */
  64.     public static function getSubscribedServices(): array
  65.     {
  66.         return [Cron::class];
  67.     }
  68.     private function canRunCron(Request $request): bool
  69.     {
  70.         $pathInfo $request->getPathInfo();
  71.         // Skip the listener in the install tool and upon fragment URLs
  72.         if (preg_match('~(?:^|/)(?:contao/install$|'.preg_quote($this->fragmentPath'~').'/)~'$pathInfo)) {
  73.             return false;
  74.         }
  75.         /** @var Config $config */
  76.         $config $this->framework->getAdapter(Config::class);
  77.         return $config->isComplete() && !$config->get('disableCron') && $this->canRunDbQuery();
  78.     }
  79.     /**
  80.      * Checks if a database connection can be established and the table exist.
  81.      */
  82.     private function canRunDbQuery(): bool
  83.     {
  84.         try {
  85.             return $this->connection->isConnected()
  86.                 && $this->connection->getSchemaManager()->tablesExist(['tl_cron_job']);
  87.         } catch (DriverException $e) {
  88.             return false;
  89.         }
  90.     }
  91. }