Same filename and directory in other branches
- 8.9.x core/modules/automated_cron/src/EventSubscriber/AutomatedCron.php
- 9 core/modules/automated_cron/src/EventSubscriber/AutomatedCron.php
Namespace
Drupal\automated_cron\EventSubscriber
File
core/modules/automated_cron/src/EventSubscriber/AutomatedCron.php
View source
<?php
namespace Drupal\automated_cron\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\CronInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class AutomatedCron implements EventSubscriberInterface {
protected $cron;
protected $config;
protected $state;
public function __construct(CronInterface $cron, ConfigFactoryInterface $config_factory, StateInterface $state) {
$this->cron = $cron;
$this->config = $config_factory
->get('automated_cron.settings');
$this->state = $state;
}
public function onTerminate(TerminateEvent $event) {
$interval = $this->config
->get('interval');
if ($interval > 0) {
$cron_next = $this->state
->get('system.cron_last', 0) + $interval;
if ((int) $event
->getRequest()->server
->get('REQUEST_TIME') > $cron_next) {
$this->cron
->run();
}
}
}
public static function getSubscribedEvents() : array {
return [
KernelEvents::TERMINATE => [
[
'onTerminate',
100,
],
],
];
}
}
Classes