8000 [DI] Fix autowire error for inlined services by weaverryan · Pull Request #22857 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] Fix autowire error for inlined services #22857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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 @@ -21,16 +21,30 @@
class AutowireExceptionPass implements CompilerPassInterface
{
private $autowirePass;
private $inlineServicePass;

public function __construct(AutowirePass $autowirePass)
public function __construct(AutowirePass $autowirePass, InlineServiceDefinitionsPass $inlineServicePass)
{
$this->autowirePass = $autowirePass;
$this->inlineServicePass = $inlineServicePass;
}

public function process(ContainerBuilder $container)
{
foreach ($this->autowirePass->getAutowiringExceptions() as $exception) {
if ($container->hasDefinition($exception->getServiceId())) {
// the pass should only be run once
if (null === $this->autowirePass || null === $this->inlineServicePass) {
return;
}

$inlinedIds = $this->inlineServicePass->getInlinedServiceIds();
$exceptions = $this->autowirePass->getAutowiringExceptions();

// free up references
$this->autowirePass = null;
$this->inlineServicePass = null;

foreach ($exceptions as $exception) {
if ($container->hasDefinition($exception->getServiceId()) || in_array($exception->getServiceId(), $inlinedIds)) {
throw $exception;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
class InlineServiceDefinitionsPass extends AbstractRecursivePass implements RepeatablePassInterface
{
private $repeatedPass;
private $inlinedServiceIds = array();

/**
* {@inheritdoc}
Expand All @@ -32,6 +33,16 @@ public function setRepeatedPass(RepeatedPass $repeatedPass)
$this->repeatedPass = $repeatedPass;
}

/**
* Returns an array of all services inlined by this pass.
*
* @return array Service id strings
*/
public function getInlinedServiceIds()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@internal?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so

{
return $this->inlinedServiceIds;
}

/**
* {@inheritdoc}
*/
Expand All @@ -46,6 +57,7 @@ protected function processValue($value, $isRoot = false)

if ($this->isInlineableDefinition($id, $definition, $this->container->getCompiler()->getServiceReferenceGraph())) {
$this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId));
$this->inlinedServiceIds[] = $id;

if ($definition->isShared()) {
return $definition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public function __construct()
new RemoveAbstractDefinitionsPass(),
new RepeatedPass(array(
new AnalyzeServiceReferencesPass(),
new InlineServiceDefinitionsPass(),
$inlinedServicePass = new InlineServiceDefinitionsPass(),
new AnalyzeServiceReferencesPass(),
new RemoveUnusedDefinitionsPass(),
)),
new AutowireExceptionPass($autowirePass),
new AutowireExceptionPass($autowirePass, $inlinedServicePass),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps pass $inlinedServicePass->getInlinedServiceIds() directly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to, but the passes haven't run yet at this point

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the pass did not run yet so this would just return an empty array

Copy lin 8000 k
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aargh.. right :)

new CheckExceptionOnInvalidReferenceBehaviorPass(),
));
}
Expand Down
A3E2
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Compiler\AutowireExceptionPass;
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
use Symfony\Component\DependencyInjection\Compiler\InlineServiceDefinitionsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;

Expand All @@ -29,10 +30,45 @@ public function testThrowsException()
->method('getAutowiringExceptions')
->will($this->returnValue(array($autowireException)));

$inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
->getMock();
$inlinePass->expects($this->any())
->method('getInlinedServiceIds')
->will($this->returnValue(array()));

$container = new ContainerBuilder();
$container->register('foo_service_id');

$pass = new AutowireExceptionPass($autowirePass);
$pass = new AutowireExceptionPass($autowirePass, $inlinePass);

try {
$pass->process($container);
$this->fail('->process() should throw the exception if the service id exists');
} catch (\Exception $e) {
$this->assertSame($autowireException, $e);
}
}

public function testThrowExceptionIfServiceInlined()
{
$autowirePass = $this->getMockBuilder(AutowirePass::class)
->getMock();

$autowireException = new AutowiringFailedException('foo_service_id', 'An autowiring exception message');
$autowirePass->expects($this->any())
->method('getAutowiringExceptions')
->will($this->returnValue(array($autowireException)));

$inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
->getMock();
$inlinePass->expects($this->any())
->method('getInlinedServiceIds')
->will($this->returnValue(array('foo_service_id')));

// don't register the foo_service_id service
$container = new ContainerBuilder();

$pass = new AutowireExceptionPass($autowirePass, $inlinePass);

try {
$pass->process($container);
Expand All @@ -52,9 +88,15 @@ public function testNoExceptionIfServiceRemoved()
->method('getAutowiringExceptions')
->will($this->returnValue(array($autowireException)));

$inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
->getMock();
$inlinePass->expects($this->any())
->method('getInlinedServiceIds')
->will($this->returnValue(array()));

$container = new ContainerBuilder();

$pass = new AutowireExceptionPass($autowirePass);
$pass = new AutowireExceptionPass($autowirePass, $inlinePass);

$pass->process($container);
// mark the test as passed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,30 @@ public function testProcessDoesNotSetLazyArgumentValuesAfterInlining()
$this->assertSame('inline', (string) $values[0]);
}

public function testGetInlinedServiceIds()
{
$container = new ContainerBuilder();
$container
->register('inlinable.service')
->setPublic(false)
;
$container
->register('non_inlinable.service')
->setPublic(true)
;

$container
->register('service')
->setArguments(array(new Reference('inlinable.service')))
;

$inlinePass = new InlineServiceDefinitionsPass();
$repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), $inlinePass));
$repeatedPass->process($container);

$this->assertEquals(array('inlinable.service'), $inlinePass->getInlinedServiceIds());
}

protected function process(ContainerBuilder $container)
{
$repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), new InlineServiceDefinitionsPass()));
Expand Down
0