From 9dd19bec1df34d60dd5ccbdc02db43e28c9989a7 Mon Sep 17 00:00:00 2001 From: Matteo Caberlotto Date: Fri, 17 May 2013 01:18:01 +0200 Subject: [PATCH 1/7] fix: class name --- Tests/Functional/Document/RouteProviderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Functional/Document/RouteProviderTest.php b/Tests/Functional/Document/RouteProviderTest.php index d7e8abe4..8d506d8f 100644 --- a/Tests/Functional/Document/RouteProviderTest.php +++ b/Tests/Functional/Document/RouteProviderTest.php @@ -10,7 +10,7 @@ use Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional\BaseTestCase; -class RouteRepositoryTest extends BaseTestCase +class RouteProviderTest extends BaseTestCase { const ROUTE_ROOT = '/test/routing'; From 6dff543e9e7a3787ea64e927d4d5dfdb1ab30132 Mon Sep 17 00:00:00 2001 From: Matteo Caberlotto Date: Mon, 20 May 2013 01:04:45 +0200 Subject: [PATCH 2/7] orm patch --- DependencyInjection/Configuration.php | 3 + .../SymfonyCmfRoutingExtension.php | 42 +++-- Entity/ContentRepository.php | 57 ++++++ Entity/ExternalRoute.php | 135 ++++++++++++++ Entity/ExternalRouteRepository.php | 15 ++ Entity/Route.php | 132 ++++++++++++++ Entity/RouteProvider.php | 165 ++++++++++++++++++ Resources/config/cmf_orm_routing.xml | 43 +++++ Resources/config/cmf_routing.xml | 1 - Resources/config/doctrine/Route.orm.xml | 32 ++++ Tests/Functional/config/doctrine.yml | 4 + composer.json | 1 + 12 files changed, 610 insertions(+), 20 deletions(-) create mode 100644 Entity/ContentRepository.php create mode 100644 Entity/ExternalRoute.php create mode 100644 Entity/ExternalRouteRepository.php create mode 100644 Entity/Route.php create mode 100644 Entity/RouteProvider.php create mode 100644 Resources/config/cmf_orm_routing.xml create mode 100644 Resources/config/doctrine/Route.orm.xml diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index abec643a..5eb16c5b 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -45,6 +45,9 @@ public function getConfigTreeBuilder() ->fixXmlConfig('locale') ->children() ->scalarNode('enabled')->defaultNull()->end() + ->arrayNode('instances') + ->prototype('scalar')->end() + ->end() ->scalarNode('generic_controller')->defaultValue('symfony_cmf_content.controller:indexAction')->end() ->arrayNode('controllers_by_type') ->useAttributeAsKey('type') diff --git a/DependencyInjection/SymfonyCmfRoutingExtension.php b/DependencyInjection/SymfonyCmfRoutingExtension.php index 4d623c7f..2cbd14d5 100644 --- a/DependencyInjection/SymfonyCmfRoutingExtension.php +++ b/DependencyInjection/SymfonyCmfRoutingExtension.php @@ -94,6 +94,7 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container, $container->setParameter($this->getAlias() . '.uri_filter_regexp', $config['uri_filter_regexp']); $loader->load('cmf_routing.xml'); + $loader->load('cmf_orm_routing.xml'); $container->setParameter($this->getAlias() . '.routing_repositoryroot', $config['routing_repositoryroot']); if (isset($config['locales']) && $config['locales']) { $container->setParameter($this->getAlias() . '.locales', $config['locales']); @@ -108,26 +109,29 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container, $managerRegistry->setFactoryService(new Reference($config['manager_registry'])); $managerRegistry->replaceArgument(0, $config['manager_name']); - $dynamic = $container->getDefinition($this->getAlias().'.dynamic_router'); - // if any mappings are defined, set the respective route enhancer - if (!empty($config['generic_controller'])) { - $dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer_explicit_template'))); - } - if (!empty($config['controllers_by_type'])) { - $dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer_controllers_by_type'))); - } - if (!empty($config['controllers_by_class'])) { - $dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer_controllers_by_class'))); - } - if (!empty($config['generic_controller']) && !empty($config['templates_by_class'])) { - $dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer_controller_for_templates_by_class'))); - $dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer_templates_by_class'))); - } - if (!empty($config['route_filters_by_id'])) { - $matcher = $container->getDefinition('symfony_cmf_routing.nested_matcher'); - foreach ($config['route_filters_by_id'] as $id => $priority) { - $matcher->addMethodCall('addRouteFilter', array(new Reference($id), $priority)); + foreach ($config['instances'] as $dynRouterServiceId) { + + $dynamic = $container->getDefinition($dynRouterServiceId); + // if any mappings are defined, set the respective route enhancer + if (!empty($config['generic_controller'])) { + $dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer_explicit_template'))); + } + if (!empty($config['controllers_by_type'])) { + $dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer_controllers_by_type'))); + } + if (!empty($config['controllers_by_class'])) { + $dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer_controllers_by_class'))); + } + if (!empty($config['generic_controller']) && !empty($config['templates_by_class'])) { + $dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer_controller_for_templates_by_class'))); + $dynamic->addMethodCall('addRouteEnhancer', array(new Reference($this->getAlias() . '.enhancer_templates_by_class'))); + } + if (!empty($config['route_filters_by_id'])) { + $matcher = $container->getDefinition('symfony_cmf_routing.nested_matcher'); + foreach ($config['route_filters_by_id'] as $id => $priority) { + $matcher->addMethodCall('addRouteFilter', array(new Reference($id), $priority)); + } } } } diff --git a/Entity/ContentRepository.php b/Entity/ContentRepository.php new file mode 100644 index 00000000..67ba3c1e --- /dev/null +++ b/Entity/ContentRepository.php @@ -0,0 +1,57 @@ +orm = $orm; + } + + /** + * {@inheritDoc} + */ + public function findById($id) { + + $identifier = $id; + + list($model, $id) = $this->getModelAndId($identifier); + + return $this->orm->getRepository($model)->find($id); + } + + + /** + * {@inheritDoc} + */ + public function getContentId($content) + { + if (! is_object($content)) { + return null; + } + try { + return implode(':', array( + get_class($content), + $content->getId() + )); + } catch (\Exception $e) { + return null; + } + } + + public function getModelAndId($identifier) { + $model = substr($identifier, 0, strpos($identifier, ':')); + $id = substr($identifier, strpos($identifier, ':')); + return array($model, $id); + } +} + +?> diff --git a/Entity/ExternalRoute.php b/Entity/ExternalRoute.php new file mode 100644 index 00000000..3976021a --- /dev/null +++ b/Entity/ExternalRoute.php @@ -0,0 +1,135 @@ +id; + } + + /** + * Set uri + * + * @param string $uri + * @return ExternalRoute + */ + public function setUri($uri) + { + $this->uri = $uri; + + return $this; + } + + /** + * Get url + * + * @return string + */ + public function getUri() + { + return $this->uri; + } + + /** + * May come handy + * @return type + */ + public function getUrl() { + return $this->getUri(); + } + + /** + * Set permanent + * + * @param boolean $permanent + * @return ExternalRoute + */ + public function setPermanent($permanent = true) + { + $this->permanent = $permanent; + + return $this; + } + + /** + * Get permanent + * + * @return boolean + */ + public function getPermanent() + { + return $this->permanent; + } + + public function isPermanent() { + return $this->getPermanent(); + } + + /** + * @TODO + */ + public function getParameters() { + return null; + } + + /** + * @TODO + */ + public function getRouteTarget() { + throw new LogicException('There is no route target for an external route, ask for uri instead'); + } + + /** + * @TODO + */ + public function getRouteName() { + throw new LogicException('There is no name for an external route, ask for id or uri'); + } + + /** + * The external route points to self ?? + * + * @return \Raindrop\RoutingBundle\Entity\ExternalRoute + */ + public function getRouteContent() { + return $this; + } + + public function getRouteKey() { + return null; + } +} diff --git a/Entity/ExternalRouteRepository.php b/Entity/ExternalRouteRepository.php new file mode 100644 index 00000000..338506b3 --- /dev/null +++ b/Entity/ExternalRouteRepository.php @@ -0,0 +1,15 @@ +priority = $priority; + + return $priority; + } + + /** + * Returns the route priority + */ + public function getPriority() + { + return $this->priority; + } + + /** + * Get id + * + * @return integer + */ + public function getId() + { + return $this->id; + } + + /** + * {@inheritDoc} + */ + public function getDefaults() + { + return $this->defaults; + } + + /** + * {@inheritDoc} + */ + public function getStaticPrefix() + { + return $this->path; + } +} diff --git a/Entity/RouteProvider.php b/Entity/RouteProvider.php new file mode 100644 index 00000000..7e248c2f --- /dev/null +++ b/Entity/RouteProvider.php @@ -0,0 +1,165 @@ +idPrefix = $prefix; + } + + protected function getCandidates($url) + { + $candidates = array(); + if ('/' !== $url) { + if (preg_match('/(.+)\.[a-z]+$/i', $url, $matches)) { + $candidates[] = $this->idPrefix.$url; + $url = $matches[1]; + } + + $part = $url; + while (false !== ($pos = strrpos($part, '/'))) { + $candidates[] = $this->idPrefix.$part; + $part = substr($url, 0, $pos); + } + } + + if (!empty($this->idPrefix)) { + $candidates[] = $this->idPrefix; + } + + return $candidates; + } + + /** + * {@inheritDoc} + * + * This will return any document found at the url or up the path to the + * prefix. If any of the documents does not extend the symfony Route + * object, it is filtered out. In the extreme case this can also lead to an + * empty list being returned. + */ + public function findRouteByUrl($url) + { + if (! is_string($url) || strlen($url) < 1 || '/' != $url[0]) { + throw new RouteNotFoundException("$url is not a valid route"); + } + + $candidates = $this->getCandidates($url); + + $collection = new RouteCollection(); + + try { + $routes = $this->findByPath($candidates, array('position' => 'ASC')); + + foreach ($routes as $route) { + if ($route instanceof SymfonyRoute) { + $collection->add($route->getName(), $route); + } + } + } catch (RepositoryException $e) { + // TODO: how to determine whether this is a relevant exception or not? + // for example, getting /my//test (note the double /) is just an invalid path + // and means another router might handle this. + // but if the PHPCR backend is down for example, we want to alert the user + } + + return $collection; + } + + /** + * {@inheritDoc} + */ + public function getRouteByName($name, $parameters = array()) + { + $route = $this->findOneByName($name); + if (!$route) { + throw new RouteNotFoundException("No route found for name '$name'"); + } + + return $route; + } + + public function getRoutesByNames($names, $parameters = array()) { + // @TODO: must implement this + } + + public function getRouteCollectionForRequest(\Symfony\Component\HttpFoundation\Request $request) { + + $url = $request->getPathInfo(); + + $candidates = $this->getCandidates($url); + + $collection = new RouteCollection(); + if (empty($candidates)) { + return $collection; + } + + try { + $routes = $this->findByPath($candidates, array('position' => 'ASC')); + + // filter for valid route objects + // we can not search for a specific class as PHPCR does not know class inheritance + // but optionally we could define a node type + foreach ($routes as $key => $route) { + if ($route instanceof SymfonyRoute) { + if (preg_match('/.+\.([a-z]+)$/i', $url, $matches)) { + if ($route->getDefault('_format') === $matches[1]) { + continue; + } + + $route->setDefault('_format', $matches[1]); + } + // SYMFONY 2.1 COMPATIBILITY: tweak route name + $key = trim(preg_replace('/[^a-z0-9A-Z_.]/', '_', $key), '_'); + $collection->add($key, $route); + } + } + } catch (RepositoryException $e) { + // TODO: how to determine whether this is a relevant exception or not? + // for example, getting /my//test (note the double /) is just an invalid path + // and means another router might handle this. + // but if the PHPCR backend is down for example, we want to alert the user + } + + return $collection; + } +} diff --git a/Resources/config/cmf_orm_routing.xml b/Resources/config/cmf_orm_routing.xml new file mode 100644 index 00000000..c1c90d1e --- /dev/null +++ b/Resources/config/cmf_orm_routing.xml @@ -0,0 +1,43 @@ + + + + + Symfony\Cmf\Bundle\RoutingBundle\Entity\Route + Symfony\Cmf\Bundle\RoutingBundle\Entity\RouteProvider + Symfony\Cmf\Bundle\RoutingBundle\Entity\ContentRepository + + + + + + + + %symfony_cmf_routing.uri_filter_regexp% + + + + + + + + + + + + + + + + + + + + + + %symfony_cmf_routing.route_entity_class% + + + + diff --git a/Resources/config/cmf_routing.xml b/Resources/config/cmf_routing.xml index 4aeccab8..a6ed30ee 100644 --- a/Resources/config/cmf_routing.xml +++ b/Resources/config/cmf_routing.xml @@ -5,7 +5,6 @@ Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter - null null Symfony\Cmf\Component\Routing\NestedMatcher\NestedMatcher Symfony\Cmf\Component\Routing\NestedMatcher\UrlMatcher diff --git a/Resources/config/doctrine/Route.orm.xml b/Resources/config/doctrine/Route.orm.xml new file mode 100644 index 00000000..805752c2 --- /dev/null +++ b/Resources/config/doctrine/Route.orm.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Tests/Functional/config/doctrine.yml b/Tests/Functional/config/doctrine.yml index 4ebffe69..40b94091 100644 --- a/Tests/Functional/config/doctrine.yml +++ b/Tests/Functional/config/doctrine.yml @@ -3,3 +3,7 @@ doctrine: driver: %database_driver% path: %database_path% charset: UTF8 + + orm: + auto_generate_proxy_classes: %kernel.debug% + auto_mapping: true \ No newline at end of file diff --git a/composer.json b/composer.json index 17a7ae52..2e4ba162 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "symfony/form": ">=2.1,<2.3-dev", "symfony/finder": ">=2.1,<2.3-dev", "jackalope/jackalope-doctrine-dbal": "1.0.*", + "doctrine/orm": "~2.2,>=2.2.3", "doctrine/phpcr-odm": "1.0.*", "doctrine/phpcr-bundle": "1.0.*" }, From 4686bfbe71e279445c07cf2a60e01a210f186b5d Mon Sep 17 00:00:00 2001 From: Matteo Caberlotto Date: Wed, 22 May 2013 22:34:32 +0200 Subject: [PATCH 3/7] fix typo --- Entity/Route.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Entity/Route.php b/Entity/Route.php index b376ab36..8419a1e9 100644 --- a/Entity/Route.php +++ b/Entity/Route.php @@ -2,20 +2,13 @@ namespace Symfony\Cmf\Bundle\RoutingBundle\Entity; -use Symfony\Cmf\Component\Routing\RouteObjectInterface; -use Symfony\Component\Routing\Exception\InvalidParameterException; - -use Symfony\Cmf\Bundle\RoutingBundle\Model\Route as BaseRoute; - - -use Doctrine\ORM\Mapping as ORM; - +use Symfony\Cmf\Bundle\RoutingBundle\Document\Route as BaseRoute; /** * ORM route version. * @author matteo caberlotto mcaber@gmail.com */ -class Route extends baseRoute +class Route extends BaseRoute { /** * {@inheritDoc} From 0ad3486c70d85f3e4072a568b37631b9f53d8baf Mon Sep 17 00:00:00 2001 From: Matteo Caberlotto Date: Wed, 22 May 2013 22:35:19 +0200 Subject: [PATCH 4/7] removed unused class --- Entity/ExternalRouteRepository.php | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 Entity/ExternalRouteRepository.php diff --git a/Entity/ExternalRouteRepository.php b/Entity/ExternalRouteRepository.php deleted file mode 100644 index 338506b3..00000000 --- a/Entity/ExternalRouteRepository.php +++ /dev/null @@ -1,15 +0,0 @@ - Date: Wed, 22 May 2013 22:37:06 +0200 Subject: [PATCH 5/7] fix: group use statements --- Entity/RouteProvider.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Entity/RouteProvider.php b/Entity/RouteProvider.php index 7e248c2f..f4030845 100644 --- a/Entity/RouteProvider.php +++ b/Entity/RouteProvider.php @@ -3,15 +3,11 @@ namespace Symfony\Cmf\Bundle\RoutingBundle\Entity; use Doctrine\ORM\EntityManager; - +use Doctrine\ORM\EntityRepository; use Symfony\Component\Routing\Route as SymfonyRoute; use Symfony\Component\Routing\RouteCollection; -use Symfony\Cmf\Component\Routing\RouteProviderInterface; - use Symfony\Component\Routing\Exception\RouteNotFoundException; - - -use Doctrine\ORM\EntityRepository; +use Symfony\Cmf\Component\Routing\RouteProviderInterface; /** * Repository to load routes from Doctrine ORM From b8d9c41cfc26596cfd1ddbc199642100e9429df2 Mon Sep 17 00:00:00 2001 From: Matteo Caberlotto Date: Wed, 22 May 2013 22:46:23 +0200 Subject: [PATCH 6/7] fix: orm xml mapping --- Resources/config/doctrine/Route.orm.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Resources/config/doctrine/Route.orm.xml b/Resources/config/doctrine/Route.orm.xml index 805752c2..ec53305e 100644 --- a/Resources/config/doctrine/Route.orm.xml +++ b/Resources/config/doctrine/Route.orm.xml @@ -18,9 +18,9 @@ - - - + + + From f20754f611ef3972c2ebeae64a0e4da72da518dc Mon Sep 17 00:00:00 2001 From: Matteo Caberlotto Date: Wed, 22 May 2013 22:58:12 +0200 Subject: [PATCH 7/7] applied php-cs-fixer --- .../SymfonyCmfRoutingExtension.php | 1 - Entity/ContentRepository.php | 19 +++++++------- Entity/ExternalRoute.php | 26 ++++++++++++------- Entity/RouteProvider.php | 7 ++--- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/DependencyInjection/SymfonyCmfRoutingExtension.php b/DependencyInjection/SymfonyCmfRoutingExtension.php index 2cbd14d5..3416436f 100644 --- a/DependencyInjection/SymfonyCmfRoutingExtension.php +++ b/DependencyInjection/SymfonyCmfRoutingExtension.php @@ -109,7 +109,6 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container, $managerRegistry->setFactoryService(new Reference($config['manager_registry'])); $managerRegistry->replaceArgument(0, $config['manager_name']); - foreach ($config['instances'] as $dynRouterServiceId) { $dynamic = $container->getDefinition($dynRouterServiceId); diff --git a/Entity/ContentRepository.php b/Entity/ContentRepository.php index 67ba3c1e..ca80eefd 100644 --- a/Entity/ContentRepository.php +++ b/Entity/ContentRepository.php @@ -9,18 +9,18 @@ * * @author teito */ -class ContentRepository implements ContentRepositoryInterface { - - - public function __construct($orm) { +class ContentRepository implements ContentRepositoryInterface +{ + public function __construct($orm) + { $this->orm = $orm; } /** * {@inheritDoc} */ - public function findById($id) { - + public function findById($id) + { $identifier = $id; list($model, $id) = $this->getModelAndId($identifier); @@ -28,7 +28,6 @@ public function findById($id) { return $this->orm->getRepository($model)->find($id); } - /** * {@inheritDoc} */ @@ -47,11 +46,11 @@ public function getContentId($content) } } - public function getModelAndId($identifier) { + public function getModelAndId($identifier) + { $model = substr($identifier, 0, strpos($identifier, ':')); $id = substr($identifier, strpos($identifier, ':')); + return array($model, $id); } } - -?> diff --git a/Entity/ExternalRoute.php b/Entity/ExternalRoute.php index 3976021a..db68f1e0 100644 --- a/Entity/ExternalRoute.php +++ b/Entity/ExternalRoute.php @@ -30,7 +30,6 @@ class ExternalRoute implements RedirectRouteInterface */ protected $permanent; - /** * Get id * @@ -44,7 +43,7 @@ public function getId() /** * Set uri * - * @param string $uri + * @param string $uri * @return ExternalRoute */ public function setUri($uri) @@ -68,14 +67,15 @@ public function getUri() * May come handy * @return type */ - public function getUrl() { + public function getUrl() + { return $this->getUri(); } /** * Set permanent * - * @param boolean $permanent + * @param boolean $permanent * @return ExternalRoute */ public function setPermanent($permanent = true) @@ -95,28 +95,32 @@ public function getPermanent() return $this->permanent; } - public function isPermanent() { + public function isPermanent() + { return $this->getPermanent(); } /** * @TODO */ - public function getParameters() { + public function getParameters() + { return null; } /** * @TODO */ - public function getRouteTarget() { + public function getRouteTarget() + { throw new LogicException('There is no route target for an external route, ask for uri instead'); } /** * @TODO */ - public function getRouteName() { + public function getRouteName() + { throw new LogicException('There is no name for an external route, ask for id or uri'); } @@ -125,11 +129,13 @@ public function getRouteName() { * * @return \Raindrop\RoutingBundle\Entity\ExternalRoute */ - public function getRouteContent() { + public function getRouteContent() + { return $this; } - public function getRouteKey() { + public function getRouteKey() + { return null; } } diff --git a/Entity/RouteProvider.php b/Entity/RouteProvider.php index f4030845..4d2d99a1 100644 --- a/Entity/RouteProvider.php +++ b/Entity/RouteProvider.php @@ -114,12 +114,13 @@ public function getRouteByName($name, $parameters = array()) return $route; } - public function getRoutesByNames($names, $parameters = array()) { + public function getRoutesByNames($names, $parameters = array()) + { // @TODO: must implement this } - public function getRouteCollectionForRequest(\Symfony\Component\HttpFoundation\Request $request) { - + public function getRouteCollectionForRequest(\Symfony\Component\HttpFoundation\Request $request) + { $url = $request->getPathInfo(); $candidates = $this->getCandidates($url);