Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e5fbce

Browse files
committed
[DI] Change priority of AddConsoleCommandPass to TYPE_BEFORE_REMOVING
1 parent 10a2d39 commit 7e5fbce

File tree

2 files changed

+80
-5
lines changed

2 files changed

+80
-5
lines changed

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function build(ContainerBuilder $container)
107107
$this->addCompilerPassIfExists($container, AddConstraintValidatorsPass::class, PassConfig::TYPE_BEFORE_REMOVING);
108108
$container->addCompilerPass(new AddAnnotationsCachedReaderPass(), PassConfig::TYPE_AFTER_REMOVING, -255);
109109
$this->addCompilerPassIfExists($container, AddValidatorInitializersPass::class);
110-
$this->addCompilerPassIfExists($container, AddConsoleCommandPass::class);
110+
$this->addCompilerPassIfExists($container, AddConsoleCommandPass::class, PassConfig::TYPE_BEFORE_REMOVING);
111111
if (class_exists(TranslatorPass::class)) {
112112
// Arguments to be removed in 4.0, relying on the default values
113113
$container->addCompilerPass(new TranslatorPass('translator.default', 'translation.loader'));

src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
namespace Symfony\Component\Console\Tests\DependencyInjection;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
1617
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
17-
use Symfony\Component\Console\Command\Command;
1818
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
19+
use Symfony\Component\DependencyInjection\ChildDefinition;
20+
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
1921
use Symfony\Component\DependencyInjection\ContainerBuilder;
2022
use Symfony\Component\DependencyInjection\Definition;
2123
use Symfony\Component\DependencyInjection\TypedReference;
@@ -28,7 +30,7 @@ class AddConsoleCommandPassTest extends TestCase
2830
public function testProcess($public)
2931
{
3032
$container = new ContainerBuilder();
31-
$container->addCompilerPass(new AddConsoleCommandPass());
33+
$container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
3234
$container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
3335

3436
$definition = new Definition('%my-command.class%');
@@ -127,7 +129,7 @@ public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
127129
{
128130
$container = new ContainerBuilder();
129131
$container->setResourceTracking(false);
130-
$container->addCompilerPass(new AddConsoleCommandPass());
132+
$container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
131133

132134
$definition = new Definition('Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
133135
$definition->addTag('console.command');
@@ -145,7 +147,7 @@ public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
145147
{
146148
$container = new ContainerBuilder();
147149
$container->setResourceTracking(false);
148-
$container->addCompilerPass(new AddConsoleCommandPass());
150+
$container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
149151

150152
$definition = new Definition('SplObjectStorage');
151153
$definition->addTag('console.command');
@@ -175,6 +177,79 @@ public function testProcessPrivateServicesWithSameCommand()
175177
$this->assertTrue($container->hasAlias($alias1));
176178
$this->assertTrue($container->hasAlias($alias2));
177179
}
180+
181+
public function testProcessOnChildDefinitionWithClass()
182+
{
183+
$container = new ContainerBuilder();
184+
$container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
185+
$className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';
186+
187+
$parentId = 'my-parent-command';
188+
$childId = 'my-child-command';
189+
190+
$parentDefinition = new Definition(/* no class */);
191+
$parentDefinition->setAbstract(true)->setPublic(false);
192+
193+
$childDefinition = new ChildDefinition($parentId);
194+
$childDefinition->addTag('console.command')->setPublic(true);
195+
$childDefinition->setClass($className);
196+
197+
$container->setDefinition($parentId, $parentDefinition);
198+
$container->setDefinition($childId, $childDefinition);
199+
200+
$container->compile();
201+
$command = $container->get($childId);
202+
203+
$this->assertInstanceOf($className, $command);
204+
}
205+
206+
public function testProcessOnChildDefinitionWithParentClass()
207+
{
208+
$container = new ContainerBuilder();
209+
$container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
210+
$className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';
211+
212+
$parentId = 'my-parent-command';
213+
$childId = 'my-child-command';
214+
215+
$parentDefinition = new Definition($className);
216+
$parentDefinition->setAbstract(true)->setPublic(false);
217+
218+
$childDefinition = new ChildDefinition($parentId);
219+
$childDefinition->addTag('console.command')->setPublic(true);
220+
221+
$container->setDefinition($parentId, $parentDefinition);
222+
$container->setDefinition($childId, $childDefinition);
223+
224+
$container->compile();
225+
$command = $container->get($childId);
226+
227+
$this->assertInstanceOf($className, $command);
228+
}
229+
230+
/**
231+
* @expectedException \RuntimeException
232+
* @expectedExceptionMessage The definition for "my-child-command" has no class.
233+
*/
234+
public function testProcessOnChildDefinitionWithoutClass()
235+
{
236+
$container = new ContainerBuilder();
237+
$container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
238+
239+
$parentId = 'my-parent-command';
240+
$childId = 'my-child-command';
241+
242+
$parentDefinition = new Definition();
243+
$parentDefinition->setAbstract(true)->setPublic(false);
244+
245+
$childDefinition = new ChildDefinition($parentId);
246+
$childDefinition->addTag('console.command')->setPublic(true);
247+
248+
$container->setDefinition($parentId, $parentDefinition);
249+
$container->setDefinition($childId, $childDefinition);
250+
251+
$container->compile();
252+
}
178253
}
179254

180255
class MyCommand extends Command

0 commit comments

Comments
 (0)