Skip to content
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

Fix parameter injection in Doctrine Repository (#219) #229

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 1 addition & 5 deletions Generator/RepositoryInjectionGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,9 @@ public function generate(\ReflectionClass $original, PhpClass $proxy)
->indent()
->writeln('$processed[] = $this->container->get((string) $arg, $arg->getInvalidBehavior());')
->outdent()
->writeln('} else if ($arg instanceof \Symfony\Component\DependencyInjection\Parameter) {')
->indent()
->writeln('$processed[] = $this->container->getParameter((string) $arg);')
->outdent()
->writeln('} else {')
->indent()
->writeln('$processed[] = $arg;')
->writeln('$processed[] = $this->container->getParameterBag()->resolveValue((string) $arg);')
->outdent()
->writeln('}')
->outdent()
Expand Down
17 changes: 17 additions & 0 deletions Tests/Functional/Entities/TestEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace JMS\DiExtraBundle\Tests\Functional\Entities;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="\JMS\DiExtraBundle\Tests\Functional\Entities\TestEntityRepository")
*/
class TestEntity
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
*/
private $id;
}
43 changes: 43 additions & 0 deletions Tests/Functional/Entities/TestEntityRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace JMS\DiExtraBundle\Tests\Functional\Entities;

use Doctrine\ORM\EntityRepository;
use JMS\DiExtraBundle\Annotation as DI;

class TestEntityRepository extends EntityRepository
{
private $service;

private $parameter;

public function getService()
{
return $this->service;
}

public function getParameter()
{
return $this->parameter;
}

/**
* @DI\InjectParams({
* "service" = @DI\Inject("some_service")
* })
*/
public function setService($service)
{
$this->service = $service;
}

/**
* @DI\InjectParams({
* "parameter" = @DI\Inject("foo_%some_parameter%")
* })
*/
public function setParameter($parameter)
{
$this->parameter = $parameter;
}
}
18 changes: 18 additions & 0 deletions Tests/Functional/Issue219Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace JMS\DiExtraBundle\Tests\Functional;

class Issue219Test extends BaseTestCase
{
public function testDoctrineRepositoryInjection()
{
$kernel = static::createKernel(array('debug' => false, 'config' => 'issue-219.yml'));
$kernel->boot();

$manager = $kernel->getContainer()->get('doctrine.orm.default_entity_manager');
$repository = $manager->getRepository('\JMS\DiExtraBundle\Tests\Functional\Entities\TestEntity');

$this->assertSame($kernel->getContainer()->get('some_service'), $repository->getService());
$this->assertEquals("foo_42", $repository->getParameter());
}
}
17 changes: 17 additions & 0 deletions Tests/Functional/config/issue-219.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
imports:
- { resource: doctrine.yml }

doctrine:
orm:
mappings:
test_entities:
type: annotation
dir: %kernel.root_dir%/Entities
prefix: JMS\DiExtraBundle\Tests\Functional\Entities

services:
some_service:
class: stdClass

parameters:
some_parameter: 42