bug #24346 [DI] Fix missing use + minor tweaks (ogizanagi)

This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Fix missing use + minor tweaks

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes (see added test)
| New feature?  | no <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget updating UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | N/A <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A

Commits
-------

0a7e5b0 [DI] Fix missing use + minor tweaks
This commit is contained in:
Nicolas Grekas 2017-09-28 10:14:02 +02:00
commit 890f25a6c9
9 changed files with 39 additions and 33 deletions

View File

@ -22,6 +22,9 @@ abstract class AbstractConfigurator
{
const FACTORY = 'unknown';
/** @internal */
protected $definition;
public function __call($method, $args)
{
if (method_exists($this, 'set'.$method)) {
@ -37,7 +40,7 @@ abstract class AbstractConfigurator
* @param mixed $value
* @param bool $allowServices whether Definition and Reference are allowed; by default, only scalars and arrays are
*
* @return mixed the value, optionaly cast to a Definition/Reference
* @return mixed the value, optionally cast to a Definition/Reference
*/
public static function processValue($value, $allowServices = false)
{
@ -50,32 +53,14 @@ abstract class AbstractConfigurator
}
if ($value instanceof ReferenceConfigurator) {
static $refCast;
if (!$refCast) {
$refCast = \Closure::bind(function ($value) {
return new Reference($value->id, $value->invalidBehavior);
}, null, $value);
}
// cast ReferenceConfigurator to Reference
return $refCast($value);
return new Reference($value->id, $value->invalidBehavior);
}
if ($value instanceof InlineServiceConfigurator) {
static $defCast;
$def = $value->definition;
$value->definition = null;
if (!$defCast) {
$defCast = \Closure::bind(function ($value) {
$def = $value->definition;
$value->definition = null;
return $def;
}, null, $value);
}
// cast InlineServiceConfigurator to Definition
return $defCast($value);
return $def;
}
if ($value instanceof self) {

View File

@ -17,9 +17,8 @@ use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
abstract class AbstractServiceConfigurator extends AbstractConfigurator
{
protected $parent;
protected $definition;
protected $id;
protected $defaultTags = array();
private $defaultTags = array();
public function __construct(ServicesConfigurator $parent, Definition $definition, $id = null, array $defaultTags = array())
{
@ -59,7 +58,7 @@ abstract class AbstractServiceConfigurator extends AbstractConfigurator
* Creates an alias.
*
* @param string $id
* @param string $ref
* @param string $referencedId
*
* @return AliasConfigurator
*/

View File

@ -16,10 +16,13 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ReferenceConfigurator
class ReferenceConfigurator extends AbstractConfigurator
{
private $id;
private $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
/** @internal */
protected $id;
/** @internal */
protected $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
public function __construct($id)
{

View File

@ -11,8 +11,6 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
use Symfony\Component\DependencyInjection\Definition;
/**
* @method $this abstract(bool $abstract = true)
*/

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
trait AutoconfigureTrait

View File

@ -11,7 +11,6 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
trait DeprecateTrait

View File

@ -11,7 +11,6 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
trait TagTrait

View File

@ -0,0 +1,9 @@
<?php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
return function (ContainerConfigurator $c) {
$c->services()
->set('parent_service', \stdClass::class)
->set('child_service')->parent('parent_service')->autoconfigure(true);
};

View File

@ -76,4 +76,17 @@ class PhpFileLoaderTest extends TestCase
yield array('php7');
}
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage The service "child_service" cannot have a "parent" and also have "autoconfigure". Try disabling autoconfiguration for the service.
*/
public function testAutoConfigureAndChildDefinitionNotAllowed()
{
$fixtures = realpath(__DIR__.'/../Fixtures');
$container = new ContainerBuilder();
$loader = new PhpFileLoader($container, new FileLocator());
$loader->load($fixtures.'/config/services_autoconfigure_with_parent.php');
$container->compile();
}
}