Skip to content

[2.1] Method Injection #881

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 9 commits into from
Prev Previous commit
Next Next commit
[DependencyInjection] updated PassConfig
  • Loading branch information
schmittjoh committed May 10, 2011
commit 648c778f54350c66c0d6dd7485862fdb2b51e627
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function process(ContainerBuilder $container)
if (!$this->onlyConstructorArguments) {
$this->processArguments($definition->getMethodCalls());
$this->processArguments($definition->getProperties());
$this->processArguments($definition->getLookupMethods());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private function generateClass(Definition $definition, $cacheDir)
* You can modify this class by changing your "lookup_method" configuration
* for service "%s".
*/
class %s extends %s
class %s extends \%s
{
private $__symfonyDependencyInjectionContainer;
%s
Expand Down Expand Up @@ -157,7 +157,7 @@ private function dumpValue($value)
$this->container->getDefinition($id)->setPublic(true);
}

return '$this->__symfonyDependencyInjectionContainer->get('.var_export($id, true).', '.var_export($value->getInvalidBehavior(), true).');';
return '$this->__symfonyDependencyInjectionContainer->get('.var_export($id, true).', '.var_export($value->getInvalidBehavior(), true).')';
} else if (is_array($value) || is_scalar($value) || null === $value) {
return var_export($value, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function __construct()
);

$this->removingPasses = array(
new GenerateLookupMethodClassesPass(),
new RemovePrivateAliasesPass(),
new RemoveAbstractDefinitionsPass(),
new ReplaceAliasByActualDefinitionPass(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public function process(ContainerBuilder $container)
$definition->setArguments(
$this->processArguments($definition->getArguments())
);
$definition->setLookupMethods(
$this->processArguments($definition->getLookupMethods())
);

$calls = array();
foreach ($definition->getMethodCalls() as $call) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function process(ContainerBuilder $container)
$definition->setArguments($this->processArguments($definition->getArguments()));
$definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
$definition->setProperties($this->processArguments($definition->getProperties()));
$definition->setLookupMethods($this->processArguments($definition->getLookupMethods()));
}

foreach ($container->getAliases() as $id => $alias) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Symfony\Tests\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Compiler\GenerateLookupMethodClassesPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class GenerateLookupMethodClassesPassTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->dir = sys_get_temp_dir().'/lookup_method_classes';

if (!is_dir($this->dir) && false === @mkdir($this->dir, 0777, true)) {
$this->markTestIncomplete('Cache dir could not be created.');
}

$this->container = new ContainerBuilder();
$this->container->setParameter('kernel.cache_dir', sys_get_temp_dir());
}

protected function tearDown()
{
foreach (new \DirectoryIterator($this->dir) as $file) {
if ('.' === $file->getFileName()) {
continue;
}

@unlink($file->getPathName());
}

@rmdir($this->dir);
}

public function testProcess()
{
$defFoobar = $this->container
->register('foobar', 'Symfony\Tests\Component\DependencyInjection\Compiler\FooBarService')
->setPublic(false)
;
$def = $this->container
->register('test', 'Symfony\Tests\Component\DependencyInjection\Compiler\LookupMethodTestClass')
->setLookupMethod('getFooBar', new Reference('foobar'))
;

$this->process();

$service = $this->container->get('test');
$this->assertInstanceOf('Symfony\Component\DependencyInjection\LookupMethodClasses\LookupMethodTestClass', $service);
$this->assertInstanceOf('Symfony\Tests\Component\DependencyInjection\Compiler\FooBarService', $service->getFooBar());
$this->assertEquals($this->dir.'/LookupMethodTestClass.php', $def->getFile());
$this->assertEquals('Symfony\Component\DependencyInjection\LookupMethodClasses\LookupMethodTestClass', $def->getClass());
$this->assertTrue($defFoobar->isPublic());
}

private function process()
{
$pass = new GenerateLookupMethodClassesPass();
$pass->process($this->container);
}
}

class FooBarService {}

abstract class LookupMethodTestClass
{
abstract public function getFooBar();
}