Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DependencyInjection] Add "instanceof" section for local interface-defined configs #21530

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

Merged
merged 2 commits into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
[DependencyInjection] Tests + refacto for "instanceof" definitions
  • Loading branch information
dunglas authored and nicolas-grekas committed Feb 17, 2017
commit 773eca7794facb9bbd3055db8da1055e37c2b103
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ private function parseDefaults(array &$content, $file)
return $defaults;
}

private function isUnderscoredParamValid($content, $name, $file)
{
if (!isset($content['services'][$name])) {
return false;
}

if (!is_array($underscoreParam = $content['services'][$name])) {
throw new InvalidArgumentException(sprintf('Service "%s" key must be an array, "%s" given in "%s".', $name, gettype($underscoreParam), $file));
}

// @deprecated condition, to be removed in 4.0

return !isset($underscoreParam['alias']) && !isset($underscoreParam['class']) && !isset($underscoreParam['factory']);
}

/**
* @param array $service
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class PassConfigTest extends \PHPUnit_Framework_TestCase
public function testPassOrdering()
{
$config = new PassConfig();
$config->setBeforeOptimizationPasses(array());

$pass1 = $this->getMockBuilder(CompilerPassInterface::class)->getMock();
$config->addPass($pass1, PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
Expand All @@ -30,7 +31,7 @@ public function testPassOrdering()
$config->addPass($pass2, PassConfig::TYPE_BEFORE_OPTIMIZATION, 30);

$passes = $config->getBeforeOptimizationPasses();
$this->assertSame($pass2, $passes[1]);
$this->assertSame($pass1, $passes[2]);
$this->assertSame($pass2, $passes[0]);
$this->assertSame($pass1, $passes[1]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<instanceof id="Symfony\Component\DependencyInjection\Tests\Loader\BarInterface" lazy="true">
<autowire>set*</autowire>
<tag name="foo" />
<tag name="bar" />
</instanceof>

<service id="Symfony\Component\DependencyInjection\Tests\Loader\Bar" class="Symfony\Component\DependencyInjection\Tests\Loader\Bar" autowire="true" />
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
_instanceof:
Symfony\Component\DependencyInjection\Tests\Loader\FooInterface:
autowire: true
lazy: true
tags:
- { name: foo }
- { name: bar }

Symfony\Component\DependencyInjection\Tests\Loader\Foo: ~
Original file line number Diff line number Diff line change
Expand Up @@ -719,4 +719,25 @@ public function testNamedArguments()
$this->assertEquals(array(null, 'ABCD'), $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
$this->assertEquals(array(array('setApiKey', array('123'))), $container->getDefinition(NamedArgumentsDummy::class)->getMethodCalls());
}

public function testInstanceof()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services_instanceof.xml');
$container->compile();

$definition = $container->getDefinition(Bar::class);
$this->assertSame(array('__construct', 'set*'), $definition->getAutowiredCalls());
$this->assertTrue($definition->isLazy());
$this->assertSame(array('foo' => array(array()), 'bar' => array(array())), $definition->getTags());
}
}

interface BarInterface
{
}

class Bar implements BarInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,19 @@ public function testNamedArguments()
$this->assertEquals(array(array('setApiKey', array('123'))), $container->getDefinition('another_one')->getMethodCalls());
}

public function testInstanceof()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('services_instanceof.yml');
$container->compile();

$definition = $container->getDefinition(Foo::class);
$this->assertTrue($definition->isAutowired());
$this->assertTrue($definition->isLazy());
$this->assertSame(array('foo' => array(array()), 'bar' => array(array())), $definition->getTags());
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage The value of the "decorates" option for the "bar" service must be the id of the service without the "@" prefix (replace "@foo" with "foo").
Expand Down Expand Up @@ -500,3 +513,11 @@ public function testUnderscoreServiceId()
$loader->load('services_underscore.yml');
}
}

interface FooInterface
{
}

class Foo implements FooInterface
{
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.