From 48b2f15b05dd56aa413f5c397e53026282277e2f Mon Sep 17 00:00:00 2001 From: Mihai Stancu Date: Thu, 29 May 2014 23:25:57 +0300 Subject: [PATCH 01/11] Metadata > AnnotationDriver::generateId > Enhanced the logic of the Service ID generator (the changes are configurable). --- Metadata/Driver/AnnotationDriver.php | 30 +++++++++++++++++++++++++--- Resources/config/services.xml | 13 ++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Metadata/Driver/AnnotationDriver.php b/Metadata/Driver/AnnotationDriver.php index 989a809..8708d7d 100644 --- a/Metadata/Driver/AnnotationDriver.php +++ b/Metadata/Driver/AnnotationDriver.php @@ -46,9 +46,12 @@ class AnnotationDriver implements DriverInterface { private $reader; - public function __construct(Reader $reader) + protected $skipParts = array(); + + public function __construct(Reader $reader, $skipParts = array()) { $this->reader = $reader; + $this->skipParts = $skipParts; } public function loadMetadataForClass(\ReflectionClass $class) @@ -220,10 +223,31 @@ private function convertReferenceValue($name, AnnotReference $annot) return $annot->value; } - private function generateId($name) + protected function generateId($name) { $name = preg_replace('/(?<=[a-zA-Z0-9])[A-Z]/', '_\\0', $name); + $name = strtolower(strtr($name, '\\', '.')); + + if (!empty($this->skipParts)) { + $search = array(); + $replace = array(); + foreach ($this->skipParts as $skipPart => $context) { + if (in_array('prefix', $context)) { + $search[] = '/(.?\b'.$skipPart.'_)/'; + $replace[] = '.'; + } + if (in_array('suffix', $context)) { + $search[] = '/(_'.$skipPart.'\b.?)/'; + $replace[] = '.'; + } + if (in_array('namespace', $context)) { + $search[] = '/(.?\b'.$skipPart.'\b.?)/'; + $replace[] = '.'; + } + } + $name = preg_replace($search, $replace, $name); + } - return strtolower(strtr($name, '\\', '.')); + return $name; } } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 73bb37a..3363cd8 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -6,6 +6,18 @@ JMS\DiExtraBundle\Metadata\Driver\AnnotationDriver + + + prefix + suffix + namespace + + + prefix + suffix + namespace + + JMS\DiExtraBundle\Metadata\Driver\ConfiguredControllerInjectionsDriver Metadata\Driver\LazyLoadingDriver @@ -22,6 +34,7 @@ + %jms_di_extra.metadata.driver.annotation_driver.strip_from_service_id% From c614371b0ce5dc03724cede488b2079e4d0dd601 Mon Sep 17 00:00:00 2001 From: Mihai Stancu Date: Sun, 1 Jun 2014 22:30:01 +0300 Subject: [PATCH 02/11] Adding another option to skip camelcase to underscore conversion. --- Metadata/Driver/AnnotationDriver.php | 36 +++++++++++++++++++--------- Resources/config/services.xml | 6 +++-- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Metadata/Driver/AnnotationDriver.php b/Metadata/Driver/AnnotationDriver.php index 8708d7d..4a7dbb2 100644 --- a/Metadata/Driver/AnnotationDriver.php +++ b/Metadata/Driver/AnnotationDriver.php @@ -47,11 +47,13 @@ class AnnotationDriver implements DriverInterface private $reader; protected $skipParts = array(); + protected $underscore = false; - public function __construct(Reader $reader, $skipParts = array()) + public function __construct(Reader $reader, $skipParts = array(), $underscore) { $this->reader = $reader; $this->skipParts = $skipParts; + $this->underscore = $underscore; } public function loadMetadataForClass(\ReflectionClass $class) @@ -225,29 +227,41 @@ private function convertReferenceValue($name, AnnotReference $annot) protected function generateId($name) { - $name = preg_replace('/(?<=[a-zA-Z0-9])[A-Z]/', '_\\0', $name); - $name = strtolower(strtr($name, '\\', '.')); - if (!empty($this->skipParts)) { $search = array(); $replace = array(); + + /* remove prefix/suffix/namespace items */ foreach ($this->skipParts as $skipPart => $context) { if (in_array('prefix', $context)) { - $search[] = '/(.?\b'.$skipPart.'_)/'; - $replace[] = '.'; + $search[] = '/((^|\\\)\b'.$skipPart.')/'; + $replace[] = '\\'; } if (in_array('suffix', $context)) { - $search[] = '/(_'.$skipPart.'\b.?)/'; - $replace[] = '.'; + $search[] = '/('.ucfirst($skipPart).'\b(\\\|$))/'; + $replace[] = '\\'; } if (in_array('namespace', $context)) { - $search[] = '/(.?\b'.$skipPart.'\b.?)/'; - $replace[] = '.'; + $search[] = '/((^|\\\)\b'.ucfirst($skipPart).'\b(\\\|$))/'; + $replace[] = '\\'; } } + + /* remove duplicate dots */ + $search[] = '|\\\+|'; + $replace[] = '\\'; + + /* remove starting/trailing dots */ + $search[] = '/(^\\\|\\\$)/'; + $replace[] = ''; + $name = preg_replace($search, $replace, $name); } - return $name; + if ($this->underscore) { + $name = preg_replace('/(?<=[a-zA-Z0-9])[A-Z]/', '_\\0', $name); + } + + return strtolower(strtr($name, '\\', '.')); } } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 3363cd8..90cba3c 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -6,7 +6,7 @@ JMS\DiExtraBundle\Metadata\Driver\AnnotationDriver - + prefix suffix @@ -18,6 +18,7 @@ namespace + false JMS\DiExtraBundle\Metadata\Driver\ConfiguredControllerInjectionsDriver Metadata\Driver\LazyLoadingDriver @@ -34,7 +35,8 @@ - %jms_di_extra.metadata.driver.annotation_driver.strip_from_service_id% + %jms_di_extra.metadata.driver.annotation_driver.service_id.strip% + %jms_di_extra.metadata.driver.annotation_driver.service_id.underscore% From 846812a5178ac7ce292532f9ac63d3e64d726111 Mon Sep 17 00:00:00 2001 From: Mihai Stancu Date: Wed, 4 Jun 2014 12:13:51 +0300 Subject: [PATCH 03/11] Metadata > AnnotationDriver > Reverting visibility changes and retaining defaults for BC behavior. --- Metadata/Driver/AnnotationDriver.php | 8 ++++---- Resources/config/services.xml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Metadata/Driver/AnnotationDriver.php b/Metadata/Driver/AnnotationDriver.php index 4a7dbb2..73bb990 100644 --- a/Metadata/Driver/AnnotationDriver.php +++ b/Metadata/Driver/AnnotationDriver.php @@ -46,10 +46,10 @@ class AnnotationDriver implements DriverInterface { private $reader; - protected $skipParts = array(); - protected $underscore = false; + private $skipParts = array(); + private $underscore = false; - public function __construct(Reader $reader, $skipParts = array(), $underscore) + public function __construct(Reader $reader, array $skipParts = array(), $underscore = true) { $this->reader = $reader; $this->skipParts = $skipParts; @@ -225,7 +225,7 @@ private function convertReferenceValue($name, AnnotReference $annot) return $annot->value; } - protected function generateId($name) + private function generateId($name) { if (!empty($this->skipParts)) { $search = array(); diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 90cba3c..bc30570 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -18,7 +18,7 @@ namespace - false + true JMS\DiExtraBundle\Metadata\Driver\ConfiguredControllerInjectionsDriver Metadata\Driver\LazyLoadingDriver From 164c3ae29495d990b1b9a2e3028f1a404359fd0f Mon Sep 17 00:00:00 2001 From: Mihai Stancu Date: Wed, 4 Jun 2014 14:03:29 +0300 Subject: [PATCH 04/11] Metadata > AnnotationDriver > Exposed semantic configuration for new behavior. - defaults for semantic configuration are now backwards compatible; - renamed parameters strip => namespaceStrip, underscore => underscoreify; - fixed a bug in the 'suffix' RegEx which matched 'suffix' as well as 'namespace'; --- DependencyInjection/Configuration.php | 6 +++++ DependencyInjection/JMSDiExtraExtension.php | 2 ++ Metadata/Driver/AnnotationDriver.php | 26 ++++++++++----------- Resources/config/services.xml | 17 ++------------ 4 files changed, 23 insertions(+), 28 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 5d0ac5e..f8ae55c 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -108,6 +108,12 @@ public function getConfigTreeBuilder() }) ->end() ->defaultValue(class_exists('Doctrine\ORM\EntityManager'))->end() + ->arrayNode('namespace_strip') + ->prototype('array') + ->prototype('scalar')->end() + ->end() + ->end() + ->booleanNode('underscoreify')->defaultTrue()->end() ->end() ->end(); diff --git a/DependencyInjection/JMSDiExtraExtension.php b/DependencyInjection/JMSDiExtraExtension.php index fab9623..8d5809e 100644 --- a/DependencyInjection/JMSDiExtraExtension.php +++ b/DependencyInjection/JMSDiExtraExtension.php @@ -53,6 +53,8 @@ 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']); + $container->setParameter('jms_di_extra.metadata.driver.annotation_driver.service_id.namespace_strip', $config['namespace_strip']); + $container->setParameter('jms_di_extra.metadata.driver.annotation_driver.service_id.underscoreify', $config['underscoreify']); if ($config['cache_warmer']['enabled']) { foreach ($config['cache_warmer']['controller_file_blacklist'] as $filename) { $this->blackListControllerFile($filename); diff --git a/Metadata/Driver/AnnotationDriver.php b/Metadata/Driver/AnnotationDriver.php index 73bb990..12bb633 100644 --- a/Metadata/Driver/AnnotationDriver.php +++ b/Metadata/Driver/AnnotationDriver.php @@ -46,14 +46,14 @@ class AnnotationDriver implements DriverInterface { private $reader; - private $skipParts = array(); - private $underscore = false; + private $namespaceStrip = array(); + private $underscoreify = false; - public function __construct(Reader $reader, array $skipParts = array(), $underscore = true) + public function __construct(Reader $reader, array $namespaceStrip = array(), $underscoreify = true) { $this->reader = $reader; - $this->skipParts = $skipParts; - $this->underscore = $underscore; + $this->namespaceStrip = $namespaceStrip; + $this->underscoreify = $underscoreify; } public function loadMetadataForClass(\ReflectionClass $class) @@ -227,38 +227,38 @@ private function convertReferenceValue($name, AnnotReference $annot) private function generateId($name) { - if (!empty($this->skipParts)) { + if (!empty($this->namespaceStrip)) { $search = array(); $replace = array(); /* remove prefix/suffix/namespace items */ - foreach ($this->skipParts as $skipPart => $context) { + foreach ($this->namespaceStrip as $skipPart => $context) { if (in_array('prefix', $context)) { - $search[] = '/((^|\\\)\b'.$skipPart.')/'; + $search[] = '/(\b'.$skipPart.'(?!\b))/'; $replace[] = '\\'; } if (in_array('suffix', $context)) { - $search[] = '/('.ucfirst($skipPart).'\b(\\\|$))/'; + $search[] = '/((?underscore) { + if ($this->underscoreify) { $name = preg_replace('/(?<=[a-zA-Z0-9])[A-Z]/', '_\\0', $name); } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index bc30570..50ab9f4 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -6,19 +6,6 @@ JMS\DiExtraBundle\Metadata\Driver\AnnotationDriver - - - prefix - suffix - namespace - - - prefix - suffix - namespace - - - true JMS\DiExtraBundle\Metadata\Driver\ConfiguredControllerInjectionsDriver Metadata\Driver\LazyLoadingDriver @@ -35,8 +22,8 @@ - %jms_di_extra.metadata.driver.annotation_driver.service_id.strip% - %jms_di_extra.metadata.driver.annotation_driver.service_id.underscore% + %jms_di_extra.metadata.driver.annotation_driver.service_id.namespace_strip% + %jms_di_extra.metadata.driver.annotation_driver.service_id.underscoreify% From a741a6da92ed4d6c68974deb42a541b3a58413b8 Mon Sep 17 00:00:00 2001 From: Mihai Stancu Date: Tue, 8 Jul 2014 23:47:40 +0300 Subject: [PATCH 05/11] Implementing NamingStrategy for service names. --- DependencyInjection/Configuration.php | 19 ++++- DependencyInjection/JMSDiExtraExtension.php | 34 ++++++++ Metadata/DefaultNamingStrategy.php | 82 +++++++++++++++++++ Metadata/Driver/AnnotationDriver.php | 22 ++--- Metadata/NamingStrategy.php | 15 ++++ Resources/config/services.xml | 7 ++ .../Metadata/Driver/AnnotationDriverTest.php | 3 +- 7 files changed, 167 insertions(+), 15 deletions(-) create mode 100644 Metadata/DefaultNamingStrategy.php create mode 100644 Metadata/NamingStrategy.php diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 5d0ac5e..c95b214 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -107,7 +107,24 @@ public function getConfigTreeBuilder() return $v; }) ->end() - ->defaultValue(class_exists('Doctrine\ORM\EntityManager'))->end() + ->defaultValue(class_exists('Doctrine\ORM\EntityManager')) + ->end() + ->arrayNode('service_naming_strategy') + ->children() + ->scalarNode('id') + ->defaultValue('jms_di_extra.service_naming_strategy.default') + ->end() + ->arrayNode('namespace_strip') + ->defaultValue(array()) + ->prototype('array') + ->prototype('scalar')->end() + ->end() + ->end() + ->booleanNode('underscoreify') + ->defaultTrue() + ->end() + ->end() + ->end() ->end() ->end(); diff --git a/DependencyInjection/JMSDiExtraExtension.php b/DependencyInjection/JMSDiExtraExtension.php index fab9623..af5106f 100644 --- a/DependencyInjection/JMSDiExtraExtension.php +++ b/DependencyInjection/JMSDiExtraExtension.php @@ -28,6 +28,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; +use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpKernel\DependencyInjection\Extension; @@ -53,6 +54,39 @@ 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']); + + + $config['service_naming_strategy'] = array_merge( + array( + 'id' => 'jms_di_extra.service_naming_strategy.default', + 'class' => null, + 'namespace_strip' => array(), + 'underscoreify' => true, + ), + isset($config['service_naming_strategy']) ? $config['service_naming_strategy'] : array() + ); + + if ($config['service_naming_strategy']['class'] !== null) { + $container->register( + 'jms_di_extra.service_naming_strategy', + $config['service_naming_strategy']['class'] + ); + } else { + $container->setAlias( + 'jms_di_extra.service_naming_strategy', + new Alias($config['service_naming_strategy']['id']) + ); + } + $container->setParameter( + 'jms_di_extra.service_naming_strategy.namespace_strip', + $config['service_naming_strategy']['namespace_strip'] + ); + $container->setParameter( + 'jms_di_extra.service_naming_strategy.underscoreify', + $config['service_naming_strategy']['underscoreify'] + ); + + if ($config['cache_warmer']['enabled']) { foreach ($config['cache_warmer']['controller_file_blacklist'] as $filename) { $this->blackListControllerFile($filename); diff --git a/Metadata/DefaultNamingStrategy.php b/Metadata/DefaultNamingStrategy.php new file mode 100644 index 0000000..3ee4ed9 --- /dev/null +++ b/Metadata/DefaultNamingStrategy.php @@ -0,0 +1,82 @@ + ['prefix', 'namespace', 'suffix']). + * + * @var array + */ + private $namespaceStrip; + + /** + * Should we add underscores when de-camelcasing? + * + * @var bool + */ + private $underscoreify; + + + /** + * @param array $namespaceStrip + * @param bool $underscoreify + */ + public function __construct(array $namespaceStrip = array(), $underscoreify = true) + { + $this->namespaceStrip = $namespaceStrip; + $this->underscoreify = $underscoreify; + } + + + /** + * 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) + { + $name = $className; + + if (!empty($this->namespaceStrip)) { + $search = array(); + $replace = array(); + + /* remove prefix/suffix/namespace items */ + foreach ($this->namespaceStrip as $skipPart => $context) { + if (in_array('prefix', $context)) { + $search[] = '/(\b'.$skipPart.'(?!\b))/'; + $replace[] = '\\'; + } + if (in_array('suffix', $context)) { + $search[] = '/((?underscoreify) { + $name = preg_replace('/(?<=[a-zA-Z0-9])[A-Z]/', '_\\0', $name); + } + + return strtolower(strtr($name, '\\', '.')); + } +} diff --git a/Metadata/Driver/AnnotationDriver.php b/Metadata/Driver/AnnotationDriver.php index 989a809..1cbd27b 100644 --- a/Metadata/Driver/AnnotationDriver.php +++ b/Metadata/Driver/AnnotationDriver.php @@ -38,6 +38,7 @@ 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; @@ -45,10 +46,12 @@ 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) @@ -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; } @@ -83,7 +86,7 @@ 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( @@ -91,7 +94,7 @@ public function loadMetadataForClass(\ReflectionClass $class) ); } 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) { @@ -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; @@ -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, '%')) { @@ -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, '\\', '.')); - } } diff --git a/Metadata/NamingStrategy.php b/Metadata/NamingStrategy.php new file mode 100644 index 0000000..8084293 --- /dev/null +++ b/Metadata/NamingStrategy.php @@ -0,0 +1,15 @@ +JMS\DiExtraBundle\Metadata\Driver\ConfiguredControllerInjectionsDriver Metadata\Driver\LazyLoadingDriver + JMS\DiExtraBundle\Metadata\DefaultNamingStrategy + Metadata\MetadataFactory Metadata\Cache\FileCache @@ -22,6 +24,11 @@ + + + + %jms_di_extra.service_naming_strategy.namespace_strip% + %jms_di_extra.service_naming_strategy.underscoreify% diff --git a/Tests/Metadata/Driver/AnnotationDriverTest.php b/Tests/Metadata/Driver/AnnotationDriverTest.php index 200404f..459657f 100644 --- a/Tests/Metadata/Driver/AnnotationDriverTest.php +++ b/Tests/Metadata/Driver/AnnotationDriverTest.php @@ -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 @@ -32,6 +33,6 @@ public function testFormTypeWithExplicitAlias() private function getDriver() { - return new AnnotationDriver(new AnnotationReader()); + return new AnnotationDriver(new AnnotationReader(), new DefaultNamingStrategy()); } } From ae795e3285393d5f5a8aec57530f5c1bea951c42 Mon Sep 17 00:00:00 2001 From: Mihai Stancu Date: Wed, 6 May 2015 01:58:49 +0300 Subject: [PATCH 06/11] Moved strip rules to constructor. --- Metadata/DefaultNamingStrategy.php | 43 ++++++++++++++++++------------ 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/Metadata/DefaultNamingStrategy.php b/Metadata/DefaultNamingStrategy.php index 3ee4ed9..e9d839c 100644 --- a/Metadata/DefaultNamingStrategy.php +++ b/Metadata/DefaultNamingStrategy.php @@ -5,13 +5,20 @@ class DefaultNamingStrategy implements NamingStrategy { /** - * List of namespace / class name components to be stripped from the final service name. - * The structure should be: array('component' => ['prefix', 'namespace', 'suffix']). + * Configuration list of namespace or class name components to be stripped from the final service name. + * The structure should be: ['component' => ['prefix', 'namespace', 'suffix']]. * * @var array */ private $namespaceStrip; + /** + * An associative array of preg_replace key/value pairs. + * + * @var array|string[] + */ + private $stripRules; + /** * Should we add underscores when de-camelcasing? * @@ -28,21 +35,8 @@ public function __construct(array $namespaceStrip = array(), $underscoreify = tr { $this->namespaceStrip = $namespaceStrip; $this->underscoreify = $underscoreify; - } - - /** - * 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) - { - $name = $className; - - if (!empty($this->namespaceStrip)) { + if (!empty($this->namespaceStrip) and empty($this->stripRules)) { $search = array(); $replace = array(); @@ -70,7 +64,22 @@ public function classToServiceName($className) $search[] = '/(^\\\|\\\$)/'; $replace[] = ''; - $name = preg_replace($search, $replace, $name); + $this->stripRules = array_combine($search, $replace); + } + } + + + /** + * Returns a service name for an annotated class. + * + * @param string $class The fully-qualified class name. + * + * @return string A service name. + */ + public function classToServiceName($name) + { + if (!empty($this->stripRules)) { + $name = preg_replace(array_keys($this->stripRules), array_values($this->stripRules), $name); } if ($this->underscoreify) { From c4c74209ddb3a926a928e399a3c28e0ba24596df Mon Sep 17 00:00:00 2001 From: Mihai Stancu Date: Tue, 19 May 2015 19:47:08 +0300 Subject: [PATCH 07/11] DefaultNamingStrategy now does exactly what used to do. --- DependencyInjection/Configuration.php | 13 +--- DependencyInjection/JMSDiExtraExtension.php | 11 --- Metadata/DefaultNamingStrategy.php | 75 +-------------------- Resources/config/services.xml | 3 +- 4 files changed, 4 insertions(+), 98 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c95b214..7f403a9 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -111,18 +111,7 @@ public function getConfigTreeBuilder() ->end() ->arrayNode('service_naming_strategy') ->children() - ->scalarNode('id') - ->defaultValue('jms_di_extra.service_naming_strategy.default') - ->end() - ->arrayNode('namespace_strip') - ->defaultValue(array()) - ->prototype('array') - ->prototype('scalar')->end() - ->end() - ->end() - ->booleanNode('underscoreify') - ->defaultTrue() - ->end() + ->scalarNode('id')->end() ->end() ->end() ->end() diff --git a/DependencyInjection/JMSDiExtraExtension.php b/DependencyInjection/JMSDiExtraExtension.php index beac73f..aba414b 100644 --- a/DependencyInjection/JMSDiExtraExtension.php +++ b/DependencyInjection/JMSDiExtraExtension.php @@ -28,7 +28,6 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; -use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpKernel\DependencyInjection\Extension; @@ -59,8 +58,6 @@ public function load(array $configs, ContainerBuilder $container) array( 'id' => 'jms_di_extra.service_naming_strategy.default', 'class' => null, - 'namespace_strip' => array(), - 'underscoreify' => true, ), isset($config['service_naming_strategy']) ? $config['service_naming_strategy'] : array() ); @@ -76,14 +73,6 @@ public function load(array $configs, ContainerBuilder $container) new Alias($config['service_naming_strategy']['id']) ); } - $container->setParameter( - 'jms_di_extra.service_naming_strategy.namespace_strip', - $config['service_naming_strategy']['namespace_strip'] - ); - $container->setParameter( - 'jms_di_extra.service_naming_strategy.underscoreify', - $config['service_naming_strategy']['underscoreify'] - ); if ($config['cache_warmer']['enabled']) { foreach ($config['cache_warmer']['controller_file_blacklist'] as $filename) { diff --git a/Metadata/DefaultNamingStrategy.php b/Metadata/DefaultNamingStrategy.php index e9d839c..3903efd 100644 --- a/Metadata/DefaultNamingStrategy.php +++ b/Metadata/DefaultNamingStrategy.php @@ -4,87 +4,16 @@ class DefaultNamingStrategy implements NamingStrategy { - /** - * Configuration list of namespace or class name components to be stripped from the final service name. - * The structure should be: ['component' => ['prefix', 'namespace', 'suffix']]. - * - * @var array - */ - private $namespaceStrip; - - /** - * An associative array of preg_replace key/value pairs. - * - * @var array|string[] - */ - private $stripRules; - - /** - * Should we add underscores when de-camelcasing? - * - * @var bool - */ - private $underscoreify; - - - /** - * @param array $namespaceStrip - * @param bool $underscoreify - */ - public function __construct(array $namespaceStrip = array(), $underscoreify = true) - { - $this->namespaceStrip = $namespaceStrip; - $this->underscoreify = $underscoreify; - - if (!empty($this->namespaceStrip) and empty($this->stripRules)) { - $search = array(); - $replace = array(); - - /* remove prefix/suffix/namespace items */ - foreach ($this->namespaceStrip as $skipPart => $context) { - if (in_array('prefix', $context)) { - $search[] = '/(\b'.$skipPart.'(?!\b))/'; - $replace[] = '\\'; - } - if (in_array('suffix', $context)) { - $search[] = '/((?stripRules = array_combine($search, $replace); - } - } - - /** * Returns a service name for an annotated class. * - * @param string $class The fully-qualified class name. + * @param string $name The fully-qualified class name. * * @return string A service name. */ public function classToServiceName($name) { - if (!empty($this->stripRules)) { - $name = preg_replace(array_keys($this->stripRules), array_values($this->stripRules), $name); - } - - if ($this->underscoreify) { - $name = preg_replace('/(?<=[a-zA-Z0-9])[A-Z]/', '_\\0', $name); - } + $name = preg_replace('/(?<=[a-zA-Z0-9])[A-Z]/', '_\\0', $name); return strtolower(strtr($name, '\\', '.')); } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 8bcb9af..bc75d12 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -27,8 +27,7 @@ - %jms_di_extra.service_naming_strategy.namespace_strip% - %jms_di_extra.service_naming_strategy.underscoreify% + From 63bd54823c34dc235d8ff16ce3b1ca43ee171831 Mon Sep 17 00:00:00 2001 From: Mihai Stancu Date: Tue, 19 May 2015 19:58:39 +0300 Subject: [PATCH 08/11] DefaultNamingStrategy now does exactly what it used to do. Replacing the naming strategy service with another class or service is still possible. --- DependencyInjection/Configuration.php | 1 + 1 file changed, 1 insertion(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 7f403a9..ea8ad9f 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -112,6 +112,7 @@ public function getConfigTreeBuilder() ->arrayNode('service_naming_strategy') ->children() ->scalarNode('id')->end() + ->scalarNode('class')->end() ->end() ->end() ->end() From 6e30fba6c8d3f4bbba4d9e6e467f0be4cdead043 Mon Sep 17 00:00:00 2001 From: Mihai Stancu Date: Wed, 20 May 2015 11:19:45 +0300 Subject: [PATCH 09/11] Reducing naming strategy config cruft. --- DependencyInjection/Configuration.php | 7 ++----- DependencyInjection/JMSDiExtraExtension.php | 20 +------------------- Resources/config/services.xml | 4 +--- 3 files changed, 4 insertions(+), 27 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ea8ad9f..4d1db3a 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -109,11 +109,8 @@ public function getConfigTreeBuilder() ->end() ->defaultValue(class_exists('Doctrine\ORM\EntityManager')) ->end() - ->arrayNode('service_naming_strategy') - ->children() - ->scalarNode('id')->end() - ->scalarNode('class')->end() - ->end() + ->scalarNode('service_naming_strategy') + ->defaultValue('jms_di_extra.service_naming_strategy.default') ->end() ->end() ->end(); diff --git a/DependencyInjection/JMSDiExtraExtension.php b/DependencyInjection/JMSDiExtraExtension.php index aba414b..e14dcec 100644 --- a/DependencyInjection/JMSDiExtraExtension.php +++ b/DependencyInjection/JMSDiExtraExtension.php @@ -54,25 +54,7 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('jms_di_extra.disable_grep', $config['disable_grep']); $container->setParameter('jms_di_extra.doctrine_integration', $config['doctrine_integration']); - $config['service_naming_strategy'] = array_merge( - array( - 'id' => 'jms_di_extra.service_naming_strategy.default', - 'class' => null, - ), - isset($config['service_naming_strategy']) ? $config['service_naming_strategy'] : array() - ); - - if ($config['service_naming_strategy']['class'] !== null) { - $container->register( - 'jms_di_extra.service_naming_strategy', - $config['service_naming_strategy']['class'] - ); - } else { - $container->setAlias( - 'jms_di_extra.service_naming_strategy', - new Alias($config['service_naming_strategy']['id']) - ); - } + $container->setAlias('jms_di_extra.service_naming_strategy', new Alias($config['service_naming_strategy'])); if ($config['cache_warmer']['enabled']) { foreach ($config['cache_warmer']['controller_file_blacklist'] as $filename) { diff --git a/Resources/config/services.xml b/Resources/config/services.xml index bc75d12..f0221a4 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -26,9 +26,7 @@ - - - + From 3590513fea3253c5aa3aeb44cd0ca4d9bdf4ae51 Mon Sep 17 00:00:00 2001 From: Mihai Stancu Date: Thu, 21 May 2015 16:14:22 +0300 Subject: [PATCH 10/11] Removing config values. --- DependencyInjection/JMSDiExtraExtension.php | 2 -- Resources/config/services.xml | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/DependencyInjection/JMSDiExtraExtension.php b/DependencyInjection/JMSDiExtraExtension.php index e14dcec..e5cac51 100644 --- a/DependencyInjection/JMSDiExtraExtension.php +++ b/DependencyInjection/JMSDiExtraExtension.php @@ -54,8 +54,6 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('jms_di_extra.disable_grep', $config['disable_grep']); $container->setParameter('jms_di_extra.doctrine_integration', $config['doctrine_integration']); - $container->setAlias('jms_di_extra.service_naming_strategy', new Alias($config['service_naming_strategy'])); - if ($config['cache_warmer']['enabled']) { foreach ($config['cache_warmer']['controller_file_blacklist'] as $filename) { $this->blackListControllerFile($filename); diff --git a/Resources/config/services.xml b/Resources/config/services.xml index f0221a4..5259eee 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -27,6 +27,7 @@ + From 4caf73b6786484d12a064a4dcf20b642598783a3 Mon Sep 17 00:00:00 2001 From: Mihai Stancu Date: Thu, 21 May 2015 16:46:40 +0300 Subject: [PATCH 11/11] Removing config values. --- DependencyInjection/Configuration.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 4d1db3a..5d0ac5e 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -107,11 +107,7 @@ public function getConfigTreeBuilder() return $v; }) ->end() - ->defaultValue(class_exists('Doctrine\ORM\EntityManager')) - ->end() - ->scalarNode('service_naming_strategy') - ->defaultValue('jms_di_extra.service_naming_strategy.default') - ->end() + ->defaultValue(class_exists('Doctrine\ORM\EntityManager'))->end() ->end() ->end();