Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\automated_cron\EventSubscriber;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Config\ConfigFactoryInterface;
|
Chris@0
|
6 use Drupal\Core\CronInterface;
|
Chris@0
|
7 use Drupal\Core\State\StateInterface;
|
Chris@0
|
8 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
Chris@0
|
9 use Symfony\Component\HttpKernel\Event\PostResponseEvent;
|
Chris@0
|
10 use Symfony\Component\HttpKernel\KernelEvents;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * A subscriber running cron after a response is sent.
|
Chris@0
|
14 */
|
Chris@0
|
15 class AutomatedCron implements EventSubscriberInterface {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * The cron service.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var \Drupal\Core\CronInterface
|
Chris@0
|
21 */
|
Chris@0
|
22 protected $cron;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * The cron configuration.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var \Drupal\Core\Config\Config
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $config;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * The state key value store.
|
Chris@0
|
33 *
|
Chris@17
|
34 * @var \Drupal\Core\State\StateInterface
|
Chris@0
|
35 */
|
Chris@0
|
36 protected $state;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Constructs a new automated cron runner.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @param \Drupal\Core\CronInterface $cron
|
Chris@0
|
42 * The cron service.
|
Chris@0
|
43 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
Chris@0
|
44 * The config factory.
|
Chris@0
|
45 * @param \Drupal\Core\State\StateInterface $state
|
Chris@0
|
46 * The state key-value store service.
|
Chris@0
|
47 */
|
Chris@0
|
48 public function __construct(CronInterface $cron, ConfigFactoryInterface $config_factory, StateInterface $state) {
|
Chris@0
|
49 $this->cron = $cron;
|
Chris@0
|
50 $this->config = $config_factory->get('automated_cron.settings');
|
Chris@0
|
51 $this->state = $state;
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * Run the automated cron if enabled.
|
Chris@0
|
56 *
|
Chris@0
|
57 * @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event
|
Chris@0
|
58 * The Event to process.
|
Chris@0
|
59 */
|
Chris@0
|
60 public function onTerminate(PostResponseEvent $event) {
|
Chris@0
|
61 $interval = $this->config->get('interval');
|
Chris@0
|
62 if ($interval > 0) {
|
Chris@0
|
63 $cron_next = $this->state->get('system.cron_last', 0) + $interval;
|
Chris@0
|
64 if ((int) $event->getRequest()->server->get('REQUEST_TIME') > $cron_next) {
|
Chris@0
|
65 $this->cron->run();
|
Chris@0
|
66 }
|
Chris@0
|
67 }
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * Registers the methods in this class that should be listeners.
|
Chris@0
|
72 *
|
Chris@0
|
73 * @return array
|
Chris@0
|
74 * An array of event listener definitions.
|
Chris@0
|
75 */
|
Chris@0
|
76 public static function getSubscribedEvents() {
|
Chris@0
|
77 return [KernelEvents::TERMINATE => [['onTerminate', 100]]];
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 }
|