From ebb760b2dbfe7aed8000c65b23630d247480c9ed Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sat, 31 Oct 2015 13:47:51 +0100 Subject: [PATCH] Start with a resource provider --- DependencyInjection/CmfRoutingExtension.php | 24 ++++- DependencyInjection/Configuration.php | 6 ++ Provider/ResourceRouteProvider.php | 111 ++++++++++++++++++++ Resources/config/provider-resource.xml | 32 ++++++ 4 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 Provider/ResourceRouteProvider.php create mode 100644 Resources/config/provider-resource.xml diff --git a/DependencyInjection/CmfRoutingExtension.php b/DependencyInjection/CmfRoutingExtension.php index c3442cc4..92f9c681 100644 --- a/DependencyInjection/CmfRoutingExtension.php +++ b/DependencyInjection/CmfRoutingExtension.php @@ -114,7 +114,7 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container, if ($config['persistence']['phpcr']['enabled']) { $this->loadPhpcrProvider($config['persistence']['phpcr'], $loader, $container, $locales, $config['match_implicit_locale']); $hasProvider = true; - $hasContentRepository = true; + //$hasContentRepository = true; } if ($config['persistence']['orm']['enabled']) { @@ -123,6 +123,11 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container, $hasContentRepository = true; } + if ($this->isConfigEnabled($container, $config['persistence']['resource'])) { + $this->loadResourceProvider($config['persistence']['resource'], $loader, $container); + $hasProvider = true; + } + if (isset($config['route_provider_service_id'])) { $container->setAlias($this->getAlias() . '.route_provider', $config['route_provider_service_id']); $hasProvider = true; @@ -261,10 +266,10 @@ public function loadPhpcrProvider($config, XmlFileLoader $loader, ContainerBuild $config['manager_name'] ); - $container->setAlias( + /*$container->setAlias( $this->getAlias() . '.route_provider', $this->getAlias() . '.phpcr_route_provider' - ); + );*/ $container->setAlias( $this->getAlias() . '.content_repository', $this->getAlias() . '.phpcr_content_repository' @@ -317,6 +322,19 @@ public function loadOrmProvider($config, XmlFileLoader $loader, ContainerBuilder ); } + public function loadResourceProvider($config, XmlFileLoader $loader, ContainerBuilder $container) + { + $loader->load('provider-resource.xml'); + + $definition = $container->getDefinition('cmf_routing.resource.route_provider'); + $definition->replaceArgument(0, new Reference($config['discovery_id'])); + + $container->setAlias( + $this->getAlias() . '.route_provider', + $this->getAlias() . '.resource.route_provider' + ); + } + /** * Returns the base path for the XSD files. * diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index f9e923bf..b7913264 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -73,6 +73,12 @@ public function getConfigTreeBuilder() ->arrayNode('persistence') ->addDefaultsIfNotSet() ->children() + ->arrayNode('resource') + ->canBeEnabled() + ->children() + ->scalarNode('discovery_id')->defaultValue('cmf_resource.discovery')->end() + ->end() + ->end() ->arrayNode('phpcr') ->addDefaultsIfNotSet() ->canBeEnabled() diff --git a/Provider/ResourceRouteProvider.php b/Provider/ResourceRouteProvider.php new file mode 100644 index 00000000..0c7137ef --- /dev/null +++ b/Provider/ResourceRouteProvider.php @@ -0,0 +1,111 @@ +discovery = $discovery; + $this->routesByUri = $routesByUri; + $this->candidatesStrategy = $candidatesStrategy; + } + + public function getRouteCollectionForRequest(Request $request) + { + $this->discoverRoutes(); + + $candidates = $this->getCandidates($request); + $collection = new RouteCollection(); + + if (empty($candidates)) { + return $collection; + } + + foreach (array_intersect(array_keys($this->routesByUri), $candidates) as $key => $uri) { + $collection->add($key, $this->routesByUri[$uri]); + } + + return $collection; + } + + public function getRouteByName($name) + { + $this->discoverRoutes(); + + if (isset($this->routesByPath[$name])) { + return $this->routesByPath[$name]; + } + } + + public function getRoutesByNames($names) + { + // todo implement this method + } + + /** + * @param Request $request + * + * @return array a list of PHPCR-ODM ids + */ + private function getCandidates(Request $request) + { + if (false !== strpos($request->getPathInfo(), ':')) { + return array(); + } + + return $this->candidatesStrategy->getCandidates($request); + } + + private function discoverRoutes() + { + if (0 !== count($this->routesByUri)) { + return; + } + + foreach ($this->discovery->findBindings('cmf/routes') as $binding) { + foreach ($binding->getResources() as $resource) { + if (!$resource instanceof CmfResource) { + continue; + } + + $route = $resource->getPayload(); + if (!$route instanceof SymfonyRoute) { + continue; + } + + $this->routesByUri[$route->getStaticPrefix()] = $route; + $this->routesByPath[$resource->getPath()] = $route; + } + } + } +} diff --git a/Resources/config/provider-resource.xml b/Resources/config/provider-resource.xml new file mode 100644 index 00000000..7435732b --- /dev/null +++ b/Resources/config/provider-resource.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + %cmf_routing.resource.paths% + + + + + %cmf_routing.dynamic.locales% + %cmf_routing.dynamic.limit_candidates% + + + + cmf/routes + + + + + + +