Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rector\Symfony\Bridge\DependencyInjection\SymfonyContainerFactory;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Throwable;

Expand All @@ -28,6 +29,11 @@ final class DefaultAnalyzedSymfonyApplicationContainer implements AnalyzedApplic
*/
private $parameterProvider;

/**
* @var ContainerInterface[]
*/
private $containerByKernelClass = [];

/**
* @var string[]
*/
Expand Down Expand Up @@ -110,10 +116,18 @@ private function getContainer(string $requestServiceName): Container
{
$kernelClass = $this->resolveKernelClass();

if (isset($this->containerByKernelClass[$kernelClass])) {
return $this->containerByKernelClass[$kernelClass];
}

$this->symfonyKernelParameterGuard->ensureKernelClassIsValid($kernelClass, $requestServiceName);

/** @var string $kernelClass */
return $this->symfonyContainerFactory->createFromKernelClass($kernelClass);
$container = $this->symfonyContainerFactory->createFromKernelClass($kernelClass);

$this->containerByKernelClass[$kernelClass] = $container;

return $container;
}

private function getDefaultKernelClass(): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Rector\Symfony\Bridge\DependencyInjection;

use Nette\Utils\Random;
use Rector\Configuration\Option;
use Rector\Exception\ShouldNotHappenException;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpKernel\Kernel;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\PackageBuilder\Reflection\PrivatesAccessor;
use Symplify\PackageBuilder\Reflection\PrivatesCaller;
use Throwable;

Expand Down Expand Up @@ -90,6 +94,21 @@ public function process(ContainerBuilder $containerBuilder): void

$containerBuilder->compile();

// solves "You have requested a synthetic service ("kernel"). The DIC does not know how to construct this service"
$containerBuilder->set('kernel', $kernel);

// solves "You have requested a non-existent parameter "container.build_id"
if ($containerBuilder->getParameterBag() instanceof FrozenParameterBag) {
$unfrozenParameterBag = new ParameterBag($containerBuilder->getParameterBag()->all());

$privatesAccessor = new PrivatesAccessor();
$privatesAccessor->setPrivateProperty($containerBuilder, 'parameterBag', $unfrozenParameterBag);
}

if (! $containerBuilder->hasParameter('container.build_id')) {
$containerBuilder->setParameter('container.build_id', Random::generate(10));
}

return $containerBuilder;
}

Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,4 @@ parameters:

# array is callable
- '#Parameter \#2 \$listener of method Symfony\\Component\\EventDispatcher\\Debug\\TraceableEventDispatcher\:\:getListenerPriority\(\) expects callable\(\)\: mixed, array given#'
- '#Parameter \#1 \$kernelClass of method Rector\\Symfony\\Bridge\\DependencyInjection\\SymfonyContainerFactory\:\:createFromKernelClass\(\) expects string, string\|null given#'