Unverified Commit 9244acff authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3502882 by catch, alexpott, godotislate: Add a classloader that can handle class moves

parent 31a1eb28
Loading
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -9,6 +9,9 @@ parameters:
  cache_default_bin_backends: []
  memory_cache_default_bin_backends: []
  security.enable_super_user: true
  core.moved_classes:
    'Drupal\Core\StringTranslation\TranslationWrapper':
      class: 'Drupal\Core\StringTranslation\TranslatableMarkup'
  session.storage.options:
    gc_probability: 1
    gc_divisor: 100
+0 −5
Original line number Diff line number Diff line
@@ -44,11 +44,6 @@
 */
define('DRUPAL_ROOT', dirname(__DIR__, 2));

/**
 * Keep backward compatibility for sites with references to TranslationWrapper.
 */
class_alias(TranslatableMarkup::class, '\Drupal\Core\StringTranslation\TranslationWrapper', TRUE);

/**
 * Translates a string to the current language or to a given language.
 *
+28 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Core\ClassLoader;

final class BackwardsCompatibilityClassLoader {

  public function __construct(protected array $movedClasses) {}

  /**
   * Aliases a moved class to another class, instead of actually autoloading it.
   *
   * @param string $class
   *   The classname to load.
   */
  public function loadClass(string $class): void {
    if (isset($this->movedClasses[$class])) {
      $moved = $this->movedClasses[$class];
      if (isset($moved['deprecation_version']) && isset($moved['removed_version']) && isset($moved['change_record'])) {
        // @phpcs:ignore
        @trigger_error(sprintf('Class %s is deprecated in %s and is removed from %s, use %s instead. See %s', $class, $moved['deprecation_version'], $moved['removed_version'], $moved['class'], $moved['change_record']), E_USER_DEPRECATED);
      }
      class_alias($moved['class'], $class, TRUE);
    }
  }

}
+4 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
use Drupal\Core\Cache\ListCacheBinsPass;
use Drupal\Core\DependencyInjection\Compiler\AuthenticationProviderPass;
use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass;
use Drupal\Core\DependencyInjection\Compiler\BackwardsCompatibilityClassLoaderPass;
use Drupal\Core\DependencyInjection\Compiler\CorsCompilerPass;
use Drupal\Core\DependencyInjection\Compiler\DeprecatedServicePass;
use Drupal\Core\DependencyInjection\Compiler\DevelopmentSettingsPass;
@@ -110,6 +111,9 @@ public function register(ContainerBuilder $container) {

    $container->addCompilerPass(new DeprecatedServicePass());

    // Collect moved classes for the backwards compatibility class loader.
    $container->addCompilerPass(new BackwardsCompatibilityClassLoaderPass());

    $container->registerForAutoconfiguration(EventSubscriberInterface::class)
      ->addTag('event_subscriber');

+35 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Core\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
 * Defines a compiler pass to merge moved classes into a single container parameter.
 */
class BackwardsCompatibilityClassLoaderPass implements CompilerPassInterface {

  /**
   * {@inheritdoc}
   */
  public function process(ContainerBuilder $container): void {
    $moved_classes = $container->hasParameter('core.moved_classes') ? $container->getParameter('core.moved_classes') : [];
    $modules = array_keys($container->getParameter('container.modules'));
    foreach ($modules as $module) {
      $parameter_name = $module . '.moved_classes';
      if ($container->hasParameter($parameter_name)) {
        $module_moved = $container->getParameter($parameter_name);
        \assert(is_array($module_moved));
        \assert(count($module_moved) === count(array_column($module_moved, 'class')), 'Missing class key for moved classes in ' . $module);
        $moved_classes = $moved_classes + $module_moved;
      }
    }
    if (!empty($moved_classes)) {
      $container->setParameter('moved_classes', $moved_classes);
    }
  }

}
Loading