Commit dbe9186c authored by catch's avatar catch
Browse files

Issue #3483996 by kentr, kim.pepper, godotislate, acbramley: Remove lazy...

Issue #3483996 by kentr, kim.pepper, godotislate, acbramley: Remove lazy declaration and proxy class for cron and use service closure instead

(cherry picked from commit e62e8422)
parent 51b54bc2
Loading
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -11113,12 +11113,6 @@
	'count' => 1,
	'path' => __DIR__ . '/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/UniqueFieldValueValidator.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\automated_cron\\\\EventSubscriber\\\\AutomatedCron\\:\\:onTerminate\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/modules/automated_cron/src/EventSubscriber/AutomatedCron.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\ban\\\\BanIpManager\\:\\:banIp\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
+0 −1
Original line number Diff line number Diff line
@@ -461,7 +461,6 @@ services:
  cron:
    class: Drupal\Core\Cron
    arguments: ['@module_handler', '@lock', '@queue', '@state', '@account_switcher', '@logger.channel.cron', '@plugin.manager.queue_worker', '@datetime.time', '%queue.config%']
    lazy: true
  Drupal\Core\CronInterface: '@cron'
  diff.formatter:
    class: Drupal\Core\Diff\DiffFormatter
+0 −80
Original line number Diff line number Diff line
<?php
// phpcs:ignoreFile

/**
 * This file was generated via php core/scripts/generate-proxy-class.php 'Drupal\Core\Cron' "core/lib/Drupal/Core".
 */

namespace Drupal\Core\ProxyClass {

    /**
     * Provides a proxy class for \Drupal\Core\Cron.
     *
     * @see \Drupal\Component\ProxyBuilder
     */
    class Cron implements \Drupal\Core\CronInterface
    {

        use \Drupal\Core\DependencyInjection\DependencySerializationTrait;

        /**
         * The id of the original proxied service.
         *
         * @var string
         */
        protected $drupalProxyOriginalServiceId;

        /**
         * The real proxied service, after it was lazy loaded.
         *
         * @var \Drupal\Core\Cron
         */
        protected $service;

        /**
         * The service container.
         *
         * @var \Symfony\Component\DependencyInjection\ContainerInterface
         */
        protected $container;

        /**
         * Constructs a ProxyClass Drupal proxy object.
         *
         * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
         *   The container.
         * @param string $drupal_proxy_original_service_id
         *   The service ID of the original service.
         */
        public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, $drupal_proxy_original_service_id)
        {
            $this->container = $container;
            $this->drupalProxyOriginalServiceId = $drupal_proxy_original_service_id;
        }

        /**
         * Lazy loads the real service from the container.
         *
         * @return object
         *   Returns the constructed real service.
         */
        protected function lazyLoadItself()
        {
            if (!isset($this->service)) {
                $this->service = $this->container->get($this->drupalProxyOriginalServiceId);
            }

            return $this->service;
        }

        /**
         * {@inheritdoc}
         */
        public function run()
        {
            return $this->lazyLoadItself()->run();
        }

    }

}
+1 −1
Original line number Diff line number Diff line
@@ -4,6 +4,6 @@ parameters:
services:
  _defaults:
    autoconfigure: true
    autowire: true
  automated_cron.subscriber:
    class: Drupal\automated_cron\EventSubscriber\AutomatedCron
    arguments: ['@cron', '@config.factory', '@state']
+12 −40
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\automated_cron\EventSubscriber;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\CronInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\Attribute\AutowireServiceClosure;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;
@@ -14,42 +16,12 @@
 */
class AutomatedCron implements EventSubscriberInterface {

  /**
   * The cron service.
   *
   * @var \Drupal\Core\CronInterface
   */
  protected $cron;

  /**
   * The cron configuration.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * The state key value store.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Constructs a new automated cron runner.
   *
   * @param \Drupal\Core\CronInterface $cron
   *   The cron service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state key-value store service.
   */
  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 __construct(
    #[AutowireServiceClosure('cron')]
    protected readonly \Closure $cron,
    protected readonly ConfigFactoryInterface $configFactory,
    protected StateInterface $state,
  ) {}

  /**
   * Run the automated cron if enabled.
@@ -57,12 +29,12 @@ public function __construct(CronInterface $cron, ConfigFactoryInterface $config_
   * @param \Symfony\Component\HttpKernel\Event\TerminateEvent $event
   *   The Event to process.
   */
  public function onTerminate(TerminateEvent $event) {
    $interval = $this->config->get('interval');
  public function onTerminate(TerminateEvent $event): void {
    $interval = $this->configFactory->get('automated_cron.settings')->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();
        ($this->cron)()->run();
      }
    }
  }
Loading