[Symfony] Make set symfony42 refactor get(...) in former container aware commands

This commit is contained in:
Tomas Votruba 2019-07-02 19:29:28 +02:00
parent 4cec86bbe0
commit da0b9658e6
3 changed files with 24 additions and 7 deletions

View File

@ -10,6 +10,9 @@ services:
Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand: 'Symfony\Component\Console\Command\Command'
Symfony\Component\Translation\TranslatorInterface: 'Symfony\Contracts\Translation\TranslatorInterface'
# related to "Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand" deprecation, see https://github.com/rectorphp/rector/issues/1629
Rector\Symfony\Rector\FrameworkBundle\ContainerGetToConstructorInjectionRector: ~
# https://symfony.com/blog/new-in-symfony-4-2-important-deprecations
Rector\Symfony\Rector\New_\StringToArrayArgumentProcessRector: ~
Rector\Symfony\Rector\New_\RootNodeTreeBuilderRector: ~

View File

@ -13,6 +13,13 @@ use Throwable;
final class DefaultAnalyzedSymfonyApplicationContainer implements AnalyzedApplicationContainerInterface
{
/**
* @var string[]
*/
private $commonNamesToTypes = [
'doctrine' => 'Doctrine\Bundle\DoctrineBundle\Registry',
];
/**
* @var ParameterProvider
*/
@ -40,7 +47,12 @@ final class DefaultAnalyzedSymfonyApplicationContainer implements AnalyzedApplic
public function getTypeForName(string $name): ?string
{
$container = $this->getContainer();
if (isset($this->commonNamesToTypes[$name])) {
return $this->commonNamesToTypes[$name];
}
// get known Symfony types
$container = $this->getContainer($name);
if (! $container->has($name)) {
return null;
@ -74,7 +86,7 @@ final class DefaultAnalyzedSymfonyApplicationContainer implements AnalyzedApplic
public function hasService(string $name): bool
{
$container = $this->getContainer();
$container = $this->getContainer($name);
return $container->has($name);
}
@ -82,14 +94,14 @@ final class DefaultAnalyzedSymfonyApplicationContainer implements AnalyzedApplic
/**
* @return ContainerBuilder
*/
private function getContainer(): Container
private function getContainer(string $requestServiceName): Container
{
$kernelClass = $this->parameterProvider->provideParameter(Option::KERNEL_CLASS_PARAMETER);
if ($kernelClass === null) {
$kernelClass = $this->getDefaultKernelClass();
}
$this->symfonyKernelParameterGuard->ensureKernelClassIsValid($kernelClass);
$this->symfonyKernelParameterGuard->ensureKernelClassIsValid($kernelClass, $requestServiceName);
/** @var string $kernelClass */
return $this->containerFactory->createFromKernelClass($kernelClass);

View File

@ -8,13 +8,15 @@ use Symfony\Component\HttpKernel\Kernel;
final class SymfonyKernelParameterGuard
{
public function ensureKernelClassIsValid(?string $kernelClass): void
public function ensureKernelClassIsValid(?string $kernelClass, string $requestServiceName): void
{
// ensure value is not null nor empty
if ($kernelClass === null || $kernelClass === '') {
throw new InvalidConfigurationException(sprintf(
'Make sure "%s" parameters is set in rector.yaml in "parameters:" section',
Option::KERNEL_CLASS_PARAMETER
'Make sure "%s" parameters is set in rector.yaml in "parameters:" section.%sIt is needed to resolve "%s" service name to type',
Option::KERNEL_CLASS_PARAMETER,
PHP_EOL,
$requestServiceName
));
}