Skip to content

[POC/WIP] Start with a resource provider #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions DependencyInjection/CmfRoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand All @@ -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;
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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.
*
Expand Down
6 changes: 6 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
111 changes: 111 additions & 0 deletions Provider/ResourceRouteProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Symfony\Cmf\Bundle\RoutingBundle\Provider;

use Puli\Discovery\Api\Discovery;
use Symfony\Cmf\Component\Routing\Candidates\CandidatesInterface;
use Symfony\Cmf\Component\Routing\RouteProviderInterface;
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route as SymfonyRoute;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Exception\RouteNotFoundException;

class ResourceRouteProvider implements RouteProviderInterface
{
/**
* @var SymfonyRoute[]
*/
private $routesByUri;

/**
* @var SymfonyRoute[]
*/
private $routesByPath = array();

/**
* @var Discovery
*/
private $discovery;

/**
* @var CandidatesInterface
*/
private $candidatesStrategy;

public function __construct(Discovery $discovery, array $routesByUri, CandidatesInterface $candidatesStrategy)
{
$this->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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not so nice to do this every request, we should add some caching here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we eagerly load all the routes in the resource tree into memory? What about depth > 1 routes? e.g. /cms/simple/*/**

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;
}
}
}
}
32 changes: 32 additions & 0 deletions Resources/config/provider-resource.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="cmf_routing.resource.paths" type="collection"/>
</parameters>

<services>

<service id="cmf_routing.resource.route_provider" class="Symfony\Cmf\Bundle\RoutingBundle\Provider\ResourceRouteProvider">
<argument /><!-- discovery -->
<argument>%cmf_routing.resource.paths%</argument>
<argument type="service" id="cmf_routing.resource.candidates" />
</service>

<service id="cmf_routing.resource.candidates" class="Symfony\Cmf\Component\Routing\Candidates\Candidates">
<argument>%cmf_routing.dynamic.locales%</argument>
<argument>%cmf_routing.dynamic.limit_candidates%</argument>
</service>

<service id="cmf_routing.resource.type" class="Puli\Discovery\Api\Type\BindingType">
<argument>cmf/routes</argument>

<tag name="cmf_resource.binding_type"/>
</service>

</services>

</container>