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

[InjectParams] Don't inject parameter into doctrine repository #107

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
4 changes: 4 additions & 0 deletions Generator/RepositoryInjectionGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public function generate(\ReflectionClass $original, PhpClass $proxy)
->indent()
->writeln('$processed[] = $this->container->getParameter((string) $arg);')
->outdent()
->writeln('} elseif(0 === strpos($arg, \'%\')) {')
->indent()
->writeln('$processed[] = $this->container->getParameter(substr((string)$arg, 1, -1));')
->outdent()
->writeln('} else {')
->indent()
->writeln('$processed[] = $arg;')
Expand Down
22 changes: 22 additions & 0 deletions Tests/Functional/Bundle/TestBundle/Entity/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass = "UserRepository")
*/
class User
{

/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;

/** @ORM\column(type="string") */
protected $login;
}
29 changes: 29 additions & 0 deletions Tests/Functional/Bundle/TestBundle/Entity/UserRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Entity;

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

/**
* Not declaring as Service
*
* @author Aleksandr Moroz <[email protected]>
*/
class UserRepository extends EntityRepository
{
protected $param;
protected $em;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this is useless as the repository already has the entity manager

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, but i known it, and added em only for test reason ;)
If it is important, I can change to another service.


/**
* @DI\InjectParams({
* "param" = @DI\Inject("%some.parameter%"),
* "em" = @DI\Inject("doctrine.orm.default_entity_manager")
* })
*/
public function setParam($param, $em)
{
$this->param = $param;
$this->em = $em;
}
}
20 changes: 20 additions & 0 deletions Tests/Functional/InjectParamsRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace JMS\DiExtraBundle\Tests\Functional;


class InjectParamsRepositoryTest extends BaseTestCase
{
public function testInjectParam()
{
$kernel = static::createKernel(array('debug' => true, 'config' => 'doctrine.yml'));
$kernel->boot();


$em = $kernel->getContainer()->get('doctrine.orm.default_entity_manager');
$repo = $em->getRepository('JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Entity\User');

$this->assertAttributeEquals($em, 'em', $repo);
$this->assertAttributeEquals($kernel->getContainer()->getParameter('some.parameter'), 'param', $repo);
}
}
3 changes: 3 additions & 0 deletions Tests/Functional/config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ services:
controller.hello:
class: JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Controller\ServiceController
arguments: [ "@router" ]

parameters:
some.parameter: [some_array]