Skip to content

Commit

Permalink
Merge branch '5.1' into 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Nov 5, 2020
2 parents 6f284b0 + 50da552 commit deb6aa0
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 4 deletions.
6 changes: 3 additions & 3 deletions ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1167,20 +1167,20 @@ private function doResolveServices($value, array &$inlineServices = [], bool $is
return $this->resolveServices($reference);
};
} elseif ($value instanceof IteratorArgument) {
$value = new RewindableGenerator(function () use ($value) {
$value = new RewindableGenerator(function () use ($value, &$inlineServices) {
foreach ($value->getValues() as $k => $v) {
foreach (self::getServiceConditionals($v) as $s) {
if (!$this->has($s)) {
continue 2;
}
}
foreach (self::getInitializedConditionals($v) as $s) {
if (!$this->doGet($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
if (!$this->doGet($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE, $inlineServices)) {
continue 2;
}
}

yield $k => $this->resolveServices($v);
yield $k => $this->doResolveServices($v, $inlineServices);
}
}, function () use ($value): int {
$count = 0;
Expand Down
2 changes: 1 addition & 1 deletion Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ private function collectCircularReferences(string $sourceId, array $edges, array
foreach ($edges as $edge) {
$node = $edge->getDestNode();
$id = $node->getId();
if (!$node->getValue() instanceof Definition || $sourceId === $id || $edge->isLazy() || $edge->isWeak()) {
if (!$node->getValue() instanceof Definition || $sourceId === $id || $edge->isWeak()) {
continue;
}

Expand Down
4 changes: 4 additions & 0 deletions Tests/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,10 @@ public function testUninitializedReference()
public function testAlmostCircular($visibility)
{
$container = include __DIR__.'/Fixtures/containers/container_almost_circular.php';
$container->compile();

$logger = $container->get('monolog.logger');
$this->assertEquals(new \stdClass(), $logger->handler);

$foo = $container->get('foo');
$this->assertSame($foo, $foo->bar->foobar->foo);
Expand Down
3 changes: 3 additions & 0 deletions Tests/Dumper/PhpDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,9 @@ public function testAlmostCircular($visibility)

$container = new $container();

$logger = $container->get('monolog.logger');
$this->assertEquals(new \stdClass(), $logger->handler);

$foo = $container->get('foo');
$this->assertSame($foo, $foo->bar->foobar->foo);

Expand Down
19 changes: 19 additions & 0 deletions Tests/Fixtures/containers/container_almost_circular.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
Expand All @@ -8,6 +9,24 @@
$public = 'public' === $visibility;
$container = new ContainerBuilder();

// monolog-like + handler that require monolog

$container->register('monolog.logger', 'stdClass')->setPublic(true)
->setProperty('handler', new Reference('mailer.transport'));

$container->register('mailer.transport', 'stdClass')->setPublic($public)
->setFactory([new Reference('mailer.transport_factory'), 'create']);

$container->register('mailer.transport_factory', FactoryCircular::class)->setPublic($public)
->addArgument(new TaggedIteratorArgument('mailer.transport'));

$container->register('mailer.transport_factory.amazon', 'stdClass')->setPublic($public)
->addArgument(new Reference('monolog.logger_2'))
->addTag('mailer.transport');

$container->register('monolog.logger_2', 'stdClass')->setPublic($public)
->setProperty('handler', new Reference('mailer.transport'));

// same visibility for deps

$container->register('foo', FooCircular::class)->setPublic(true)
Expand Down
17 changes: 17 additions & 0 deletions Tests/Fixtures/includes/classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ public function __construct($lazyValues, $lazyEmptyValues)
}
}

class FactoryCircular
{
public $services;

public function __construct($services)
{
$this->services = $services;
}

public function create()
{
foreach ($this->services as $service) {
return $service;
}
}
}

class FoobarCircular
{
public function __construct(FooCircular $foo)
Expand Down
47 changes: 47 additions & 0 deletions Tests/Fixtures/php/services_almost_circular_private.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function __construct()
'manager' => 'getManagerService',
'manager2' => 'getManager2Service',
'manager3' => 'getManager3Service',
'monolog.logger' => 'getMonolog_LoggerService',
'root' => 'getRootService',
'subscriber' => 'getSubscriberService',
];
Expand Down Expand Up @@ -77,7 +78,11 @@ public function getRemovedIds(): array
'level5' => true,
'level6' => true,
'logger2' => true,
'mailer.transport' => true,
'mailer.transport_factory' => true,
'mailer.transport_factory.amazon' => true,
'manager4' => true,
'monolog.logger_2' => true,
'multiuse1' => true,
'subscriber2' => true,
];
Expand Down Expand Up @@ -352,6 +357,20 @@ protected function getManager3Service($lazyLoad = true)
return $this->services['manager3'] = new \stdClass($b);
}

/**
* Gets the public 'monolog.logger' shared service.
*
* @return \stdClass
*/
protected function getMonolog_LoggerService()
{
$this->services['monolog.logger'] = $instance = new \stdClass();

$instance->handler = ($this->privates['mailer.transport'] ?? $this->getMailer_TransportService());

return $instance;
}

/**
* Gets the public 'root' shared service.
*
Expand Down Expand Up @@ -416,6 +435,34 @@ protected function getLevel5Service()
return $instance;
}

/**
* Gets the private 'mailer.transport' shared service.
*
* @return \stdClass
*/
protected function getMailer_TransportService()
{
return $this->privates['mailer.transport'] = (new \FactoryCircular(new RewindableGenerator(function () {
yield 0 => ($this->privates['mailer.transport_factory.amazon'] ?? $this->getMailer_TransportFactory_AmazonService());
}, 1)))->create();
}

/**
* Gets the private 'mailer.transport_factory.amazon' shared service.
*
* @return \stdClass
*/
protected function getMailer_TransportFactory_AmazonService()
{
$a = new \stdClass();

$this->privates['mailer.transport_factory.amazon'] = $instance = new \stdClass($a);

$a->handler = ($this->privates['mailer.transport'] ?? $this->getMailer_TransportService());

return $instance;
}

/**
* Gets the private 'manager4' shared service.
*
Expand Down
77 changes: 77 additions & 0 deletions Tests/Fixtures/php/services_almost_circular_public.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ public function __construct()
'listener3' => 'getListener3Service',
'listener4' => 'getListener4Service',
'logger' => 'getLoggerService',
'mailer.transport' => 'getMailer_TransportService',
'mailer.transport_factory' => 'getMailer_TransportFactoryService',
'mailer.transport_factory.amazon' => 'getMailer_TransportFactory_AmazonService',
'manager' => 'getManagerService',
'manager2' => 'getManager2Service',
'manager3' => 'getManager3Service',
'monolog.logger' => 'getMonolog_LoggerService',
'monolog.logger_2' => 'getMonolog_Logger2Service',
'root' => 'getRootService',
'subscriber' => 'getSubscriberService',
];
Expand Down Expand Up @@ -434,6 +439,50 @@ protected function getLoggerService()
return $instance;
}

/**
* Gets the public 'mailer.transport' shared service.
*
* @return \stdClass
*/
protected function getMailer_TransportService()
{
$a = ($this->services['mailer.transport_factory'] ?? $this->getMailer_TransportFactoryService());

if (isset($this->services['mailer.transport'])) {
return $this->services['mailer.transport'];
}

return $this->services['mailer.transport'] = $a->create();
}

/**
* Gets the public 'mailer.transport_factory' shared service.
*
* @return \FactoryCircular
*/
protected function getMailer_TransportFactoryService()
{
return $this->services['mailer.transport_factory'] = new \FactoryCircular(new RewindableGenerator(function () {
yield 0 => ($this->services['mailer.transport_factory.amazon'] ?? $this->getMailer_TransportFactory_AmazonService());
}, 1));
}

/**
* Gets the public 'mailer.transport_factory.amazon' shared service.
*
* @return \stdClass
*/
protected function getMailer_TransportFactory_AmazonService()
{
$a = ($this->services['monolog.logger_2'] ?? $this->getMonolog_Logger2Service());

if (isset($this->services['mailer.transport_factory.amazon'])) {
return $this->services['mailer.transport_factory.amazon'];
}

return $this->services['mailer.transport_factory.amazon'] = new \stdClass($a);
}

/**
* Gets the public 'manager' shared service.
*
Expand Down Expand Up @@ -482,6 +531,34 @@ protected function getManager3Service($lazyLoad = true)
return $this->services['manager3'] = new \stdClass($a);
}

/**
* Gets the public 'monolog.logger' shared service.
*
* @return \stdClass
*/
protected function getMonolog_LoggerService()
{
$this->services['monolog.logger'] = $instance = new \stdClass();

$instance->handler = ($this->services['mailer.transport'] ?? $this->getMailer_TransportService());

return $instance;
}

/**
* Gets the public 'monolog.logger_2' shared service.
*
* @return \stdClass
*/
protected function getMonolog_Logger2Service()
{
$this->services['monolog.logger_2'] = $instance = new \stdClass();

$instance->handler = ($this->services['mailer.transport'] ?? $this->getMailer_TransportService());

return $instance;
}

/**
* Gets the public 'root' shared service.
*
Expand Down

0 comments on commit deb6aa0

Please sign in to comment.