Skip to content

Commit

Permalink
Merge pull request #167 from mihai-stancu/enhanced_service_id_generator
Browse files Browse the repository at this point in the history
Enhanced the logic of the Service ID generator
  • Loading branch information
schmittjoh committed May 21, 2015
2 parents cd1c5a4 + 4caf73b commit 36f8b06
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 14 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/JMSDiExtraExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('jms_di_extra.cache_dir', $config['cache_dir']);
$container->setParameter('jms_di_extra.disable_grep', $config['disable_grep']);
$container->setParameter('jms_di_extra.doctrine_integration', $config['doctrine_integration']);

if ($config['cache_warmer']['enabled']) {
foreach ($config['cache_warmer']['controller_file_blacklist'] as $filename) {
$this->blackListControllerFile($filename);
Expand Down
20 changes: 20 additions & 0 deletions Metadata/DefaultNamingStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace JMS\DiExtraBundle\Metadata;

class DefaultNamingStrategy implements NamingStrategy
{
/**
* Returns a service name for an annotated class.
*
* @param string $name The fully-qualified class name.
*
* @return string A service name.
*/
public function classToServiceName($name)
{
$name = preg_replace('/(?<=[a-zA-Z0-9])[A-Z]/', '_\\0', $name);

return strtolower(strtr($name, '\\', '.'));
}
}
22 changes: 9 additions & 13 deletions Metadata/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@
use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\Tag;
use JMS\DiExtraBundle\Metadata\ClassMetadata;
use JMS\DiExtraBundle\Metadata\NamingStrategy;
use Metadata\Driver\DriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;

class AnnotationDriver implements DriverInterface
{
private $reader;
private $namingStrategy;

public function __construct(Reader $reader)
public function __construct(Reader $reader, NamingStrategy $namingStrategy)
{
$this->reader = $reader;
$this->namingStrategy = $namingStrategy;
}

public function loadMetadataForClass(\ReflectionClass $class)
Expand All @@ -69,7 +72,7 @@ public function loadMetadataForClass(\ReflectionClass $class)
foreach ($this->reader->getClassAnnotations($class) as $annot) {
if ($annot instanceof Service) {
if (null === $annot->id) {
$metadata->id = $this->generateId($className);
$metadata->id = $this->namingStrategy->classToServiceName($className);
} else {
$metadata->id = $annot->id;
}
Expand All @@ -83,15 +86,15 @@ public function loadMetadataForClass(\ReflectionClass $class)
} else if ($annot instanceof Validator) {
// automatically register as service if not done explicitly
if (null === $metadata->id) {
$metadata->id = $this->generateId($className);
$metadata->id = $this->namingStrategy->classToServiceName($className);
}

$metadata->tags['validator.constraint_validator'][] = array(
'alias' => $annot->alias,
);
} else if ($annot instanceof AbstractDoctrineListener) {
if (null === $metadata->id) {
$metadata->id = $this->generateId($className);
$metadata->id = $this->namingStrategy->classToServiceName($className);
}

foreach ($annot->events as $event) {
Expand All @@ -104,7 +107,7 @@ public function loadMetadataForClass(\ReflectionClass $class)
}
} else if ($annot instanceof FormType) {
if (null === $metadata->id) {
$metadata->id = $this->generateId($className);
$metadata->id = $this->namingStrategy->classToServiceName($className);
}

$alias = $annot->alias;
Expand Down Expand Up @@ -210,7 +213,7 @@ public function loadMetadataForClass(\ReflectionClass $class)
private function convertReferenceValue($name, AnnotReference $annot)
{
if (null === $annot->value) {
return new Reference($this->generateId($name), false !== $annot->required ? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE : ContainerInterface::NULL_ON_INVALID_REFERENCE, $annot->strict);
return new Reference($this->namingStrategy->classToServiceName($name), false !== $annot->required ? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE : ContainerInterface::NULL_ON_INVALID_REFERENCE, $annot->strict);
}

if (false === strpos($annot->value, '%')) {
Expand All @@ -219,11 +222,4 @@ private function convertReferenceValue($name, AnnotReference $annot)

return $annot->value;
}

private function generateId($name)
{
$name = preg_replace('/(?<=[a-zA-Z0-9])[A-Z]/', '_\\0', $name);

return strtolower(strtr($name, '\\', '.'));
}
}
15 changes: 15 additions & 0 deletions Metadata/NamingStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace JMS\DiExtraBundle\Metadata;

interface NamingStrategy
{
/**
* Returns a service name for an annotated class.
*
* @param string $className The fully-qualified class name.
*
* @return string A service name.
*/
public function classToServiceName($className);
}
5 changes: 5 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<parameter key="jms_di_extra.metadata.driver.configured_controller_injections.class">JMS\DiExtraBundle\Metadata\Driver\ConfiguredControllerInjectionsDriver</parameter>
<parameter key="jms_di_extra.metadata.driver.lazy_loading_driver.class">Metadata\Driver\LazyLoadingDriver</parameter>

<parameter key="jms_di_extra.service_naming_strategy.default.class">JMS\DiExtraBundle\Metadata\DefaultNamingStrategy</parameter>

<parameter key="jms_di_extra.metadata.metadata_factory.class">Metadata\MetadataFactory</parameter>
<parameter key="jms_di_extra.metadata.cache.file_cache.class">Metadata\Cache\FileCache</parameter>

Expand All @@ -22,7 +24,10 @@
<!-- Metadata Drivers -->
<service id="jms_di_extra.metadata.driver.annotation_driver" class="%jms_di_extra.metadata.driver.annotation_driver.class%" public="false">
<argument type="service" id="annotation_reader" />
<argument type="service" id="jms_di_extra.service_naming_strategy" />
</service>
<service id="jms_di_extra.service_naming_strategy.default" class="%jms_di_extra.service_naming_strategy.default.class%" public="false"/>
<service id="jms_di_extra.service_naming_strategy" alias="jms_di_extra.service_naming_strategy.default"/>
<service id="jms_di_extra.metadata.driver.configured_controller_injections" class="%jms_di_extra.metadata.driver.configured_controller_injections.class%" public="false">
<argument type="service" id="jms_di_extra.metadata.driver.annotation_driver" />
</service>
Expand Down
3 changes: 2 additions & 1 deletion Tests/Metadata/Driver/AnnotationDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace JMS\DiExtraBundle\Tests\Metadata\Driver;

use Doctrine\Common\Annotations\AnnotationReader;
use JMS\DiExtraBundle\Metadata\DefaultNamingStrategy;
use JMS\DiExtraBundle\Metadata\Driver\AnnotationDriver;

class AnnotationDriverTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -32,6 +33,6 @@ public function testFormTypeWithExplicitAlias()

private function getDriver()
{
return new AnnotationDriver(new AnnotationReader());
return new AnnotationDriver(new AnnotationReader(), new DefaultNamingStrategy());
}
}

0 comments on commit 36f8b06

Please sign in to comment.