Skip to content

Commit

Permalink
feature #17782 Support autowiring for Doctrine\Common\Annotations\Rea…
Browse files Browse the repository at this point in the history
…der (maryo)

This PR was merged into the 3.1-dev branch.

Discussion
----------

Support autowiring for Doctrine\Common\Annotations\Reader

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

I've added support for autowiring based on `Doctrine\Common\Annotations\Reader` interface simmilar to https://github.com/symfony/symfony/pull/17703/files

The `annotations.cached_reader` service is injected when cache is enabled.

Commits
-------

b325f9c Support autowiring for Doctrine\Common\Annotations\Reader
  • Loading branch information
fabpot committed Feb 26, 2016
2 parents 0eaf329 + b325f9c commit aa770e1
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 1 deletion.
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;

use Doctrine\Common\Annotations\Reader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -851,6 +852,7 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
->getDefinition('annotations.cached_reader')
->replaceArgument(1, new Reference('file' !== $config['cache'] ? $config['cache'] : 'annotations.filesystem_cache'))
->replaceArgument(2, $config['debug'])
->addAutowiringType(Reader::class)
;
$container->setAlias('annotation_reader', 'annotations.cached_reader');
}
Expand Down
Expand Up @@ -5,7 +5,9 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="annotations.reader" class="Doctrine\Common\Annotations\AnnotationReader" public="false" />
<service id="annotations.reader" class="Doctrine\Common\Annotations\AnnotationReader" public="false">
<autowiring-type>Doctrine\Common\Annotations\Reader</autowiring-type>
</service>

<service id="annotations.cached_reader" class="Doctrine\Common\Annotations\CachedReader" public="false">
<argument type="service" id="annotations.reader" />
Expand Down
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;

class AutowiringTypesTest extends WebTestCase
{
public function testAnnotationReaderAutowiring()
{
static::bootKernel(array('root_config' => 'no_annotations_cache.yml', 'environment' => 'no_annotations_cache'));
$container = static::$kernel->getContainer();

$annotationReader = $container->get('test.autowiring_types.autowired_services')->getAnnotationReader();
$this->assertInstanceOf(AnnotationReader::class, $annotationReader);
}

public function testCachedAnnotationReaderAutowiring()
{
static::bootKernel();
$container = static::$kernel->getContainer();

$annotationReader = $container->get('test.autowiring_types.autowired_services')->getAnnotationReader();
$this->assertInstanceOf(CachedReader::class, $annotationReader);
}

protected static function createKernel(array $options = array())
{
return parent::createKernel(array('test_case' => 'AutowiringTypes') + $options);
}
}
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\AutowiringTypes;

use Doctrine\Common\Annotations\Reader;

class AutowiredServices
{
private $annotationReader;

public function __construct(Reader $annotationReader = null)
{
$this->annotationReader = $annotationReader;
}

public function getAnnotationReader()
{
return $this->annotationReader;
}
}
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;

return array(
new FrameworkBundle(),
new TestBundle(),
);
@@ -0,0 +1,7 @@
imports:
- { resource: ../config/default.yml }

services:
test.autowiring_types.autowired_services:
class: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\AutowiringTypes\AutowiredServices
autowire: true
@@ -0,0 +1,6 @@
imports:
- { resource: config.yml }

framework:
annotations:
cache: none

0 comments on commit aa770e1

Please sign in to comment.