diff --git a/AutoRoute/Factory.php b/AutoRoute/Factory.php index 9500448..7576ccd 100644 --- a/AutoRoute/Factory.php +++ b/AutoRoute/Factory.php @@ -132,7 +132,7 @@ protected function generateRouteStackChain($classFqn) $routeStackChain = new BuilderUnitChain($this->builder); - foreach ($mapping['content_path'] as $builderName => $builderConfig) { + foreach ($mapping['content_path']['path_units'] as $builderName => $builderConfig) { $builderUnit = $this->generateBuilderUnit($builderConfig); $routeStackChain->addBuilderUnit($builderName, $builderUnit); } @@ -230,7 +230,7 @@ private function getBuilderService($builderConfig, $type, $aliasKey) // to be stateless (which is good here) $service = $this->container->get($serviceId); unset($builderConfig[$type][$aliasKey]); - $service->init($builderConfig[$type]); + $service->init($builderConfig[$type]['options']); return $service; } diff --git a/DependencyInjection/CmfRoutingAutoExtension.php b/DependencyInjection/CmfRoutingAutoExtension.php index f57c16c..c2a5626 100644 --- a/DependencyInjection/CmfRoutingAutoExtension.php +++ b/DependencyInjection/CmfRoutingAutoExtension.php @@ -30,9 +30,14 @@ public function load(array $configs, ContainerBuilder $container) $chainFactoryDef = $container->getDefinition('cmf_routing_auto.factory'); // normalize configuration - foreach ($config['auto_route_mapping'] as $classFqn => $config) { + foreach ($config['mappings'] as $classFqn => $config) { $chainFactoryDef->addMethodCall('registerMapping', array($classFqn, $config)); } } + + public function getNamespace() + { + return 'http://cmf.symfony.com/schema/dic/routing_auto'; + } } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 145bfac..1646d01 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -14,85 +14,92 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - $needsNormalization = function ($v) { - if (!is_array($v)) { - return false; - } - - return isset($v['option']); - }; - $doNormalization = function ($v) { - $value = array(); - foreach ($v['option'] as $option) { - $value[$option['name']] = $option['value']; - } - - return $value; - }; - $treeBuilder = new TreeBuilder(); $treeBuilder->root('cmf_routing_auto') + ->fixXmlConfig('mapping') ->children() - ->arrayNode('auto_route_mapping') - ->useAttributeAsKey('class') - ->prototype('array') - ->children() - ->arrayNode('content_path') - ->useAttributeAsKey('name') - ->prototype('array') - ->children() - ->arrayNode('provider') + ->arrayNode('mappings') + ->useAttributeAsKey('class') + ->prototype('array') + ->children() + ->arrayNode('content_path') ->beforeNormalization() - ->ifTrue($needsNormalization) - ->then($doNormalization) + ->ifTrue(function ($v) { + return !isset($v['path_unit']) && !isset($v['path_units']); + }) + ->then(function ($v) { + return array( + 'path_units' => $v, + ); + }) ->end() - ->prototype('scalar')->end() - ->end() - ->arrayNode('exists_action') - ->beforeNormalization() - ->ifTrue($needsNormalization) - ->then($doNormalization) + ->fixXmlConfig('path_unit') + ->children() + ->arrayNode('path_units') + ->useAttributeAsKey('name') + ->prototype('array') + ->children() + ->append($this->getUnitConfigOption('provider', 'name')) + ->append($this->getUnitConfigOption('exists_action')) + ->append($this->getUnitConfigOption('not_exists_action')) + ->end() + ->end() + ->end() // path_units ->end() - ->prototype('scalar')->end() - ->end() - ->arrayNode('not_exists_action') - ->beforeNormalization() - ->ifTrue($needsNormalization) - ->then($doNormalization) + ->end() // content_path + ->arrayNode('content_name') + ->children() + ->append($this->getUnitConfigOption('provider', 'name')) + ->append($this->getUnitConfigOption('exists_action')) + ->append($this->getUnitConfigOption('not_exists_action')) ->end() - ->prototype('scalar')->end() - ->end() - ->end() + ->end() // content_name ->end() ->end() - ->arrayNode('content_name') - ->children() - ->arrayNode('provider') - ->beforeNormalization() - ->ifTrue($needsNormalization) - ->then($doNormalization) - ->end() - ->prototype('scalar')->end() - ->end() - ->arrayNode('exists_action') - ->beforeNormalization() - ->ifTrue($needsNormalization) - ->then($doNormalization) - ->end() - ->prototype('scalar')->end() - ->end() - ->arrayNode('not_exists_action') - ->beforeNormalization() - ->ifTrue($needsNormalization) - ->then($doNormalization) - ->end() - ->prototype('scalar')->end() - ->end() - ->end() - ->end() + ->end() // mappings ->end(); return $treeBuilder; } + + protected function getUnitConfigOption($name, $nameOption = 'strategy') + { + $builder = new TreeBuilder(); + $node = $builder->root($name); + + $node + ->fixXmlConfig('option') + ->beforeNormalization() + ->ifTrue(function ($v) { + return is_string($v); + }) + ->then(function ($v) use ($nameOption) { + return array( + $nameOption => $v, + 'options' => array(), + ); + }) + ->end() + ->beforeNormalization() + ->ifTrue(function ($v) use ($nameOption) { + return !isset($v[$nameOption]); + }) + ->then(function ($v) use ($nameOption) { + return array( + $nameOption => $v[0], + 'options' => isset($v[1]) ? $v[1] : array(), + ); + }) + ->end() + ->children() + ->scalarNode($nameOption)->isRequired()->cannotBeEmpty()->end() + ->arrayNode('options') + ->useAttributeAsKey('name') + ->prototype('scalar')->end() + ->end() + ->end(); + + return $node; + } } diff --git a/Tests/Resources/Fixtures/config/config.php b/Tests/Resources/Fixtures/config/config.php new file mode 100644 index 0000000..4288ab6 --- /dev/null +++ b/Tests/Resources/Fixtures/config/config.php @@ -0,0 +1,27 @@ +loadFromExtension('cmf_routing_auto', array( + 'mappings' => array( + 'Acme\BasicCmsBundle\Document\Page' => array( + 'content_path' => array( + 'pages' => array( + 'provider' => array('specified', array('path' => '/cms/routes/page')), + 'exists_action' => 'use', + 'not_exists_action' => array( + 'strategy' => 'create', + ), + ), + ), + 'content_name' => array( + 'provider' => array('content_method', array('method' => 'getTitle')), + 'exists_action' => array( + 'strategy' => 'auto_increment', + 'options' => array( + 'pattern' => '-%d', + ), + ), + 'not_exists_action' => array('create'), + ), + ), + ), +)); diff --git a/Tests/Resources/Fixtures/config/config.xml b/Tests/Resources/Fixtures/config/config.xml new file mode 100644 index 0000000..cb963c7 --- /dev/null +++ b/Tests/Resources/Fixtures/config/config.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Tests/Resources/Fixtures/config/config.yml b/Tests/Resources/Fixtures/config/config.yml new file mode 100644 index 0000000..d3ebad2 --- /dev/null +++ b/Tests/Resources/Fixtures/config/config.yml @@ -0,0 +1,16 @@ +cmf_routing_auto: + mappings: + Acme\BasicCmsBundle\Document\Page: + content_path: + pages: + provider: [ specified, { path: /cms/routes/page } ] + exists_action: use + not_exists_action: + strategy: create + content_name: + provider: [ content_method, { method: getTitle } ] + exists_action: + strategy: auto_increment + options: + pattern: -%d + not_exists_action: [ create ] diff --git a/Tests/Resources/app/config/routingautoroute.yml b/Tests/Resources/app/config/routingautoroute.yml index 45eb682..bb391ec 100644 --- a/Tests/Resources/app/config/routingautoroute.yml +++ b/Tests/Resources/app/config/routingautoroute.yml @@ -15,75 +15,51 @@ cmf_routing: route_basepath: /test/routing cmf_routing_auto: - - auto_route_mapping: - - ## + mappings: # e.g. /cms/auto-route/blog/my-blogs-title Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Blog: - # generate or use path components leading up to the final part of the path content_path: - base: - provider: - name: specified - path: test/auto-route - exists_action: - strategy: use - not_exists_action: - strategy: create - namespace: - provider: - name: specified - path: blog - exists_action: - strategy: use - not_exists_action: - strategy: create - + path_units: + base: + provider: [ specified, { path: test/auto-route } ] + exists_action: use + not_exists_action: create + namespace: + provider: [ specified, { path: blog } ] + exists_action: use + not_exists_action: create + # using alternative syntax content_name: provider: name: content_method - method: getTitle + options: + method: getTitle exists_action: strategy: auto_increment - pattern: -%d - not_exists_action: + options: + pattern: -%d + not_exists_action: strategy: create - - ## # e.g. /cms/auto-route/blog/my-blogs-title/2013-04-09/my-post-title Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Post: - content_path: - - # /cms/auto-route/blog/my-blogs-title - blog_path: - provider: - name: content_object - method: getBlog - exists_action: - strategy: use - not_exists_action: - strategy: throw_exception - - date: - provider: - name: content_datetime - method: getDate - date_format: Y/m/d - exists_action: - strategy: use - not_exists_action: - strategy: create - + path_units: + # /cms/auto-route/blog/my-blogs-title + blog_path: + provider: [ content_object, { method: getBlog } ] + exists_action: use + not_exists_action: throw_exception + date: + provider: + name: content_datetime + options: + method: getDate + date_format: Y/m/d + exists_action: use + not_exists_action: create content_name: # my-post-title - provider: - name: content_method - method: getTitle - exists_action: - strategy: auto_increment - pattern: -%d - not_exists_action: - strategy: create + provider: [ content_method, { method: getTitle } ] + exists_action: [ auto_increment, { pattern: -%d } ] + not_exists_action: [ create ] diff --git a/Tests/Unit/AutoRoute/FactoryTest.php b/Tests/Unit/AutoRoute/FactoryTest.php index 4f4bbe8..d16e06b 100644 --- a/Tests/Unit/AutoRoute/FactoryTest.php +++ b/Tests/Unit/AutoRoute/FactoryTest.php @@ -61,29 +61,39 @@ public function provideTestGetChain() array( array( 'content_path' => array( - 'base' => array( - 'provider' => array( - 'name' => 'fixed', - 'message' => 'foobar' - ), - 'exists_action' => array( - 'strategy' => 'create' - ), - 'not_exists_action' => array( - 'strategy' => 'throw_excep', + 'path_units' => array( + 'base' => array( + 'provider' => array( + 'name' => 'fixed', + 'options' => array( + 'message' => 'foobar', + ), + ), + 'exists_action' => array( + 'strategy' => 'create', + 'options' => array(), + ), + 'not_exists_action' => array( + 'strategy' => 'throw_excep', + 'options' => array(), + ), ), ), ), 'content_name' => array( 'provider' => array( 'name' => 'fixed', - 'message' => 'barfoo' + 'options' => array( + 'message' => 'barfoo', + ), ), 'exists_action' => array( - 'strategy' => 'create' + 'strategy' => 'create', + 'options' => array(), ), 'not_exists_action' => array( 'strategy' => 'throw_excep', + 'options' => array(), ), ), ), diff --git a/Tests/Unit/DependencyInjection/ConfigurationTest.php b/Tests/Unit/DependencyInjection/ConfigurationTest.php index 9848c11..6e2cecb 100644 --- a/Tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/Tests/Unit/DependencyInjection/ConfigurationTest.php @@ -2,145 +2,77 @@ namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Unit\DependencyInjection; -use Matthias\SymfonyConfigTest\PhpUnit\AbstractConfigurationTestCase; use Symfony\Cmf\Bundle\RoutingAutoBundle\DependencyInjection\Configuration; +use Symfony\Cmf\Bundle\RoutingAutoBundle\DependencyInjection\CmfRoutingAutoExtension; +use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionConfigurationTestCase; -class ConfigurationTest extends AbstractConfigurationTestCase +class ConfigurationTest extends AbstractExtensionConfigurationTestCase { - protected $inputConfig; + protected function getContainerExtension() + { + return new CmfRoutingAutoExtension(); + } + + protected function getConfiguration() + { + return new Configuration(); + } - public function setUp() + public function testSupportsAllConfigFormats() { - $this->inputConfig = array( - 'auto_route_mapping' => array( + $expectedConfiguration = array( + 'mappings' => array( 'Acme\BasicCmsBundle\Document\Page' => array( 'content_path' => array( - 'pages' => array( - 'provider' => array( - 'name' => 'specified', - 'path' => '/cms/routes/page', - ), - 'exists_action' => array( - 'strategy' => 'use', - ), - 'not_exists_action' => array( - 'strategy' => 'create', + 'path_units' => array( + 'pages' => array( + 'provider' => array( + 'name' => 'specified', + 'options' => array( + 'path' => '/cms/routes/page', + ), + ), + 'exists_action' => array( + 'strategy' => 'use', + 'options' => array(), + ), + 'not_exists_action' => array( + 'strategy' => 'create', + 'options' => array(), + ), ), ), ), 'content_name' => array( 'provider' => array( 'name' => 'content_method', - 'method' => 'getTitle', + 'options' => array( + 'method' => 'getTitle', + ), ), 'exists_action' => array( 'strategy' => 'auto_increment', - 'pattern' => '-%d', + 'options' => array( + 'pattern' => '-%d', + ), ), 'not_exists_action' => array( 'strategy' => 'create', + 'options' => array(), ), ), ), ), ); - } - protected function getConfiguration() - { - return new Configuration(); - } + $sources = array_map(function ($path) { + return __DIR__.'/../../Resources/Fixtures/'.$path; + }, array( + 'config/config.yml', + 'config/config.xml', + 'config/config.php', + )); - public function testYamlConfig() - { - $this->assertProcessedConfigurationEquals( - array( - $this->inputConfig, - ), - $this->inputConfig - ); - } - - public function testXmlConfig() - { - $this->assertProcessedConfigurationEquals( - array( - array( - 'auto-route-mapping' => array( - array( - 'class' => 'Acme\BasicCmsBundle\Document\Page', - 'content-path' => array( - array( - 'name' => 'pages', - 'provider' => array( - 'option' => array( - array( - 'name' => 'name', - 'value' => 'specified', - ), - array( - 'name' => 'path', - 'value' => '/cms/routes/page', - ), - ), - ), - 'exists-action' => array( - 'option' => array( - array( - 'name' => 'strategy', - 'value' => 'use', - ) - ), - ), - 'not-exists-action' => array( - 'option' => array( - array( - 'name' => 'strategy', - 'value' => 'create', - ), - ), - ), - ), - ), - 'content-name' => array( - 'provider' => array( - 'option' => array( - array( - 'name' => 'name', - 'value' => 'content_method', - ), - array( - 'name' => 'method', - 'value' => 'getTitle', - ), - ), - ), - 'exists-action' => array( - 'option' => array( - array( - 'name' => 'strategy', - 'value' => 'auto_increment', - ), - array( - 'name' => 'pattern', - 'value' => '-%d', - ), - ), - ), - 'not-exists-action' => array( - 'option' => array( - array( - 'name' => 'strategy', - 'value' => 'create', - ), - ), - ), - ), - ), - ), - ), - ), - $this->inputConfig - ); + $this->assertProcessedConfigurationEquals($expectedConfiguration, $sources); } } diff --git a/composer.json b/composer.json index 864f5bb..d121e6f 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ }, "require-dev": { "symfony-cmf/testing": "1.0.*", + "matthiasnoback/symfony-dependency-injection-test": "0.*", "matthiasnoback/symfony-config-test": "0.*" }, "suggest": {