Skip to content

Commit 3e4ac26

Browse files
Added orm entity and implemented provider matching; added orm configuration options
1 parent 65af83e commit 3e4ac26

File tree

11 files changed

+206
-2
lines changed

11 files changed

+206
-2
lines changed

CmfRoutingBundle.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Symfony\Cmf\Bundle\RoutingBundle;
44

55
use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass;
6+
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
67
use Symfony\Component\DependencyInjection\Definition;
78
use Symfony\Component\HttpKernel\Bundle\Bundle;
89
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -37,6 +38,17 @@ public function build(ContainerBuilder $container)
3738
)
3839
);
3940
}
41+
42+
if (class_exists('Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass')) {
43+
$container->addCompilerPass(
44+
DoctrineOrmMappingsPass::createXmlMappingDriver(
45+
array(
46+
realpath(__DIR__ . '/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingBundle\Model',
47+
realpath(__DIR__ . '/Resources/config/doctrine-orm') => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm',
48+
)
49+
)
50+
);
51+
}
4052
}
4153

4254
/**

DependencyInjection/CmfRoutingExtension.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container,
9393
$hasProvider = true;
9494
$hasContentRepository = true;
9595
}
96+
97+
if (!empty($config['persistence']['orm']['enabled'])) {
98+
$this->loadOrmProvider($config['persistence']['orm'], $loader, $container);
99+
$hasProvider = true;
100+
}
101+
96102
if (isset($config['route_provider_service_id'])) {
97103
$container->setAlias('cmf_routing.route_provider', $config['route_provider_service_id']);
98104
$hasProvider = true;
@@ -171,6 +177,12 @@ public function loadSonataPhpcrAdmin($config, XmlFileLoader $loader, ContainerBu
171177
$loader->load('admin_phpcr.xml');
172178
}
173179

180+
public function loadOrmProvider($config, XmlFileLoader $loader, ContainerBuilder $container)
181+
{
182+
$container->setParameter($this->getAlias() . '.manager_name', $config['manager_name']);
183+
$loader->load('provider_orm.xml');
184+
}
185+
174186
/**
175187
* Returns the base path for the XSD files.
176188
*

DependencyInjection/Configuration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ public function getConfigTreeBuilder()
7272
->end()
7373
->end()
7474
->end()
75+
->arrayNode('orm')
76+
->children()
77+
->scalarNode('enabled')->defaultNull()->end()
78+
->scalarNode('manager_name')->defaultNull()->end()
79+
->end()
80+
->end()
7581
->end()
7682
->end()
7783
->scalarNode('uri_filter_regexp')->defaultValue('')->end()

Doctrine/DoctrineProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ abstract class DoctrineProvider
4040
public function __construct(ManagerRegistry $managerRegistry, $className = null)
4141
{
4242
$this->managerRegistry = $managerRegistry;
43+
$this->className = $className;
4344
}
4445

4546
/**

Doctrine/Orm/ContentRepository.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm;
44

5+
use Symfony\Cmf\Component\Routing\ContentRepositoryInterface;
6+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\DoctrineProvider;
7+
58
/**
6-
* Abstract content repository for PHPCR-ODM
9+
* Abstract content repository for ORM
710
*
811
* @author teito
912
*/
@@ -46,6 +49,7 @@ public function getContentId($content)
4649
if (0 !== count($ids)) {
4750
throw new \Exception('Multi identifier values not supported in ' . get_class($content));
4851
}
52+
4953
return implode(':', array(
5054
get_class($content),
5155
reset($ids)

Doctrine/Orm/Route.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm;
4+
5+
use Symfony\Cmf\Bundle\RoutingBundle\Model\Route as RouteModel;
6+
7+
/**
8+
* ORM route version.
9+
* @author matteo caberlotto [email protected]
10+
*/
11+
class Route extends RouteModel
12+
{
13+
/**
14+
* {@inheritDoc}
15+
*/
16+
protected $name;
17+
18+
/**
19+
* {@inheritDoc}
20+
*/
21+
protected $position;
22+
23+
/**
24+
* {@inheritDoc}
25+
*/
26+
protected $addTrailingSlash;
27+
28+
/**
29+
* {@inheritDoc}
30+
*/
31+
protected $host;
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
protected $defaults;
37+
38+
/**
39+
* {@inheritDoc}
40+
*/
41+
protected $requirements;
42+
43+
/**
44+
* {@inheritDoc}
45+
*/
46+
protected $options;
47+
}

Doctrine/Orm/RouteProvider.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm;
44

5-
use Symfony\Component\Routing\Route as SymfonyRoute;
65
use Symfony\Component\Routing\RouteCollection;
76
use Symfony\Component\Routing\Exception\RouteNotFoundException;
87

98
use Symfony\Component\HttpFoundation\Request;
109

1110
use Symfony\Cmf\Component\Routing\RouteProviderInterface;
11+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\DoctrineProvider;
1212

1313
/**
1414
* Provider loading routes from Doctrine
@@ -47,9 +47,57 @@ protected function getCandidates($url)
4747
*/
4848
public function getRouteByName($name, $parameters = array())
4949
{
50+
$route = $this->getRoutesRepository()->findOneByName($name);
51+
if (!$route) {
52+
throw new RouteNotFoundException("No route found for name '$name'");
53+
}
54+
55+
return $route;
5056
}
5157

5258
public function getRoutesByNames($names, $parameters = array())
5359
{
5460
}
61+
62+
public function getRouteCollectionForRequest(Request $request)
63+
{
64+
$url = $request->getPathInfo();
65+
66+
$candidates = $this->getCandidates($url);
67+
68+
$collection = new RouteCollection();
69+
70+
if (empty($candidates)) {
71+
return $collection;
72+
}
73+
74+
try {
75+
$routes = $this->getRoutesRepository()->findByStaticPrefix($candidates, array('position' => 'ASC'));
76+
77+
foreach ($routes as $key => $route) {
78+
if (preg_match('/.+\.([a-z]+)$/i', $url, $matches)) {
79+
if ($route->getDefault('_format') === $matches[1]) {
80+
continue;
81+
}
82+
83+
$route->setDefault('_format', $matches[1]);
84+
}
85+
// SYMFONY 2.1 COMPATIBILITY: tweak route name
86+
$key = trim(preg_replace('/[^a-z0-9A-Z_.]/', '_', $key), '_');
87+
$collection->add($key, $route);
88+
}
89+
} catch (RepositoryException $e) {
90+
// TODO: how to determine whether this is a relevant exception or not?
91+
// for example, getting /my//test (note the double /) is just an invalid path
92+
// and means another router might handle this.
93+
// but if the PHPCR backend is down for example, we want to alert the user
94+
}
95+
96+
return $collection;
97+
}
98+
99+
protected function getRoutesRepository()
100+
{
101+
return $this->getObjectManager()->getRepository($this->className);
102+
}
55103
}

Model/Route.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ public function compile()
259259
return parent::compile();
260260
}
261261

262+
/**
263+
* {@inheritDoc}
264+
*
265+
* Required by orm
266+
*/
267+
public function getDefaults()
268+
{
269+
return $this->defaults;
270+
}
271+
262272
public function __toString()
263273
{
264274
return (string) $this->id;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">
4+
5+
<mapped-superclass name="Symfony\Cmf\Bundle\RoutingBundle\Model\Route" referenceable="true">
6+
<field name="variablePattern" type="string"/>
7+
<field name="addFormatPattern" type="boolean"/>
8+
</mapped-superclass>
9+
10+
</doctrine-mapping>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">
4+
5+
<entity name="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route" table="orm_routes">
6+
7+
<id name="id" type="integer" column="id">
8+
<generator strategy="AUTO"/>
9+
</id>
10+
11+
<field name="name" type="string" unique="true"/>
12+
<field name="staticPrefix" type="string"/>
13+
14+
<field name="position" type="integer"/>
15+
16+
<field name="addTrailingSlash" type="boolean"/>
17+
18+
<field name="host" type="string"/>
19+
<field name="defaults" type="array"/>
20+
<field name="requirements" type="array"/>
21+
<field name="options" type="array"/>
22+
23+
<indexes>
24+
<index name="name_idx" columns="name"/>
25+
<index name="prefix_idx" columns="staticPrefix"/>
26+
</indexes>
27+
28+
</entity>
29+
30+
</doctrine-mapping>

0 commit comments

Comments
 (0)