Skip to content

Commit f5dfd9f

Browse files
committed
Merge pull request #133 from raindropdevs/orm
[WIP] orm - entities and configuration
2 parents 65af83e + f361535 commit f5dfd9f

File tree

11 files changed

+199
-2
lines changed

11 files changed

+199
-2
lines changed

CmfRoutingBundle.php

Lines changed: 29 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,34 @@ public function build(ContainerBuilder $container)
3738
)
3839
);
3940
}
41+
42+
if (class_exists('Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass')) {
43+
$container->addCompilerPass($this->buildBaseOrmCompilerPass());
44+
$container->addCompilerPass(
45+
DoctrineOrmMappingsPass::createXmlMappingDriver(
46+
array(
47+
realpath(__DIR__ . '/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingBundle\Model',
48+
realpath(__DIR__ . '/Resources/config/doctrine-orm') => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm',
49+
),
50+
array('cmf_routing.dynamic.persistence.orm.manager_name'),
51+
'cmf_routing.persistence.orm.enabled'
52+
)
53+
);
54+
}
55+
}
56+
57+
private function buildBaseOrmCompilerPass()
58+
{
59+
$arguments = array(array(realpath(__DIR__ . '/Resources/config/doctrine-base')), '.orm.xml');
60+
$locator = new Definition('Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator', $arguments);
61+
$driver = new Definition('Doctrine\ORM\Mapping\Driver\XmlDriver', array($locator));
62+
63+
return new DoctrineOrmMappingsPass(
64+
$driver,
65+
array('Symfony\Component\Routing'),
66+
array('cmf_routing.dynamic.persistence.orm.manager_name'),
67+
'cmf_routing.persistence.orm.enabled'
68+
);
4069
}
4170

4271
/**

DependencyInjection/CmfRoutingExtension.php

Lines changed: 13 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,13 @@ 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() . '.persistence.orm.manager_name', $config['manager_name']);
183+
$container->setParameter($this->getAlias() . '.persistence.orm.enabled', $config['enabled']);
184+
$loader->load('provider_orm.xml');
185+
}
186+
174187
/**
175188
* Returns the base path for the XSD files.
176189
*

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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

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()->findBy(array('name' => $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
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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\Component\Routing\Route">
6+
<field name="host" type="string"/>
7+
<field name="defaults" type="array"/>
8+
<field name="requirements" type="array"/>
9+
<field name="options" type="array"/>
10+
</mapped-superclass>
11+
12+
</doctrine-mapping>
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
<field name="position" type="integer"/>
14+
<field name="addTrailingSlash" type="boolean"/>
15+
16+
<indexes>
17+
<index name="name_idx" columns="name"/>
18+
<index name="prefix_idx" columns="staticPrefix"/>
19+
</indexes>
20+
21+
</entity>
22+
23+
</doctrine-mapping>

0 commit comments

Comments
 (0)