Verified Commit 7ff3e039 authored by Dave Long's avatar Dave Long
Browse files

perf: #3583125 Strip dot-prefixed build parameters from cached container

By: dries
By: longwave
By: berdir
By: nicxvan
By: godotislate
(cherry picked from commit 007adf94)
parent c91a1b78
Loading
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@
use Drupal\Core\Site\Settings;
use Psr\Log\LoggerAwareInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Compiler\RemoveBuildParametersPass;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

@@ -65,6 +66,10 @@ public function register(ContainerBuilder $container) {
    $container->addCompilerPass(new HookCollectorPass());
    $container->addCompilerPass(new ThemeHookCollectorPass());
    $container->addCompilerPass(new HookCollectorKeyValueWritePass(), PassConfig::TYPE_OPTIMIZE);
    // Remove dot-prefixed build parameters (e.g. '.hook_data',
    // '.theme_hook_data') that are used to pass data between compiler
    // passes but should not leak into the cached container.
    $container->addCompilerPass(new RemoveBuildParametersPass(), PassConfig::TYPE_REMOVE);
    // Add the compiler pass that lets service providers modify existing
    // service definitions. This pass must come before all passes operating on
    // services so that later list-building passes are operating on the
+23 −0
Original line number Diff line number Diff line
@@ -162,6 +162,29 @@ public function testCompileDIC(): void {
    $this->assertNotContains('service_container', $persist_ids);
  }

  /**
   * Tests that dot-prefixed build parameters are removed from the container.
   *
   * Build parameters (e.g. '.hook_data') are used to pass data between
   * compiler passes. RemoveBuildParametersPass strips them so they don't
   * bloat the cached container definition with data that is only needed at
   * build time.
   */
  public function testBuildParametersRemoved(): void {
    $request = Request::createFromGlobals();
    $kernel = $this->getTestKernel($request);
    $container = $kernel->getContainer();

    $build_only = array_filter(
      array_keys($container->getParameterBag()->all()),
      fn(string $name) => str_starts_with($name, '.'),
    );
    $this->assertEmpty($build_only, sprintf(
      'Dot-prefixed build parameters should not be in the compiled container, but found: %s',
      implode(', ', $build_only),
    ));
  }

  /**
   * Tests repeated loading of compiled DIC with different environment.
   */