-
Notifications
You must be signed in to change notification settings - Fork 76
[WIP] orm - entities and configuration #133
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
Changes from all commits
3e4ac26
2a2b2da
74cd6f3
6a34a0f
f361535
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace Symfony\Cmf\Bundle\RoutingBundle; | ||
|
||
use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass; | ||
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
@@ -37,6 +38,34 @@ public function build(ContainerBuilder $container) | |
) | ||
); | ||
} | ||
|
||
if (class_exists('Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass')) { | ||
$container->addCompilerPass($this->buildBaseOrmCompilerPass()); | ||
$container->addCompilerPass( | ||
DoctrineOrmMappingsPass::createXmlMappingDriver( | ||
array( | ||
realpath(__DIR__ . '/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingBundle\Model', | ||
realpath(__DIR__ . '/Resources/config/doctrine-orm') => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm', | ||
), | ||
array('cmf_routing.dynamic.persistence.orm.manager_name'), | ||
'cmf_routing.persistence.orm.enabled' | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is missing the arguments to know the container parameter names of the manager name and of the parameter that tells if orm is activated. |
||
); | ||
} | ||
} | ||
|
||
private function buildBaseOrmCompilerPass() | ||
{ | ||
$arguments = array(array(realpath(__DIR__ . '/Resources/config/doctrine-base')), '.orm.xml'); | ||
$locator = new Definition('Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator', $arguments); | ||
$driver = new Definition('Doctrine\ORM\Mapping\Driver\XmlDriver', array($locator)); | ||
|
||
return new DoctrineOrmMappingsPass( | ||
$driver, | ||
array('Symfony\Component\Routing'), | ||
array('cmf_routing.dynamic.persistence.orm.manager_name'), | ||
'cmf_routing.persistence.orm.enabled' | ||
); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ abstract class DoctrineProvider | |
public function __construct(ManagerRegistry $managerRegistry, $className = null) | ||
{ | ||
$this->managerRegistry = $managerRegistry; | ||
$this->className = $className; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ups. thanks |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm; | ||
|
||
use Symfony\Cmf\Bundle\RoutingBundle\Model\Route as RouteModel; | ||
|
||
/** | ||
* ORM route version. | ||
* @author matteo caberlotto [email protected] | ||
*/ | ||
class Route extends RouteModel | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected $position; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected $addTrailingSlash; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,13 @@ | |
|
||
namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm; | ||
|
||
use Symfony\Component\Routing\Route as SymfonyRoute; | ||
use Symfony\Component\Routing\RouteCollection; | ||
use Symfony\Component\Routing\Exception\RouteNotFoundException; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
use Symfony\Cmf\Component\Routing\RouteProviderInterface; | ||
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\DoctrineProvider; | ||
|
||
/** | ||
* Provider loading routes from Doctrine | ||
|
@@ -47,9 +47,57 @@ protected function getCandidates($url) | |
*/ | ||
public function getRouteByName($name, $parameters = array()) | ||
{ | ||
$route = $this->getRoutesRepository()->findBy(array('name' => $name)); | ||
if (!$route) { | ||
throw new RouteNotFoundException("No route found for name '$name'"); | ||
} | ||
|
||
return $route; | ||
} | ||
|
||
public function getRoutesByNames($names, $parameters = array()) | ||
{ | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we implement this with a findMany or something like that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should go outdate... |
||
|
||
public function getRouteCollectionForRequest(Request $request) | ||
{ | ||
$url = $request->getPathInfo(); | ||
|
||
$candidates = $this->getCandidates($url); | ||
|
||
$collection = new RouteCollection(); | ||
|
||
if (empty($candidates)) { | ||
return $collection; | ||
} | ||
|
||
try { | ||
$routes = $this->getRoutesRepository()->findByStaticPrefix($candidates, array('position' => 'ASC')); | ||
|
||
foreach ($routes as $key => $route) { | ||
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; | ||
} | ||
|
||
protected function getRoutesRepository() | ||
{ | ||
return $this->getObjectManager()->getRepository($this->className); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd"> | ||
|
||
<mapped-superclass name="Symfony\Component\Routing\Route"> | ||
<field name="host" type="string"/> | ||
<field name="defaults" type="array"/> | ||
<field name="requirements" type="array"/> | ||
<field name="options" type="array"/> | ||
</mapped-superclass> | ||
|
||
</doctrine-mapping> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd"> | ||
|
||
<mapped-superclass name="Symfony\Cmf\Bundle\RoutingBundle\Model\Route" referenceable="true"> | ||
<field name="variablePattern" type="string"/> | ||
<field name="addFormatPattern" type="boolean"/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should be able to map most fields in this mapping instead of copying the properties There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thx |
||
</mapped-superclass> | ||
|
||
</doctrine-mapping> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd"> | ||
|
||
<entity name="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route" table="orm_routes"> | ||
|
||
<id name="id" type="integer" column="id"> | ||
<generator strategy="AUTO"/> | ||
</id> | ||
|
||
<field name="name" type="string" unique="true"/> | ||
<field name="staticPrefix" type="string"/> | ||
<field name="position" type="integer"/> | ||
<field name="addTrailingSlash" type="boolean"/> | ||
|
||
<indexes> | ||
<index name="name_idx" columns="name"/> | ||
<index name="prefix_idx" columns="staticPrefix"/> | ||
</indexes> | ||
|
||
</entity> | ||
|
||
</doctrine-mapping> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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.route_entity_class">Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route</parameter> | ||
<parameter key="cmf_routing.route_entity_provider">Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\RouteProvider</parameter> | ||
<parameter key="cmf_routing.orm.content_repository_class">Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\ContentRepository</parameter> | ||
</parameters> | ||
|
||
<services> | ||
|
||
<service id="cmf_routing.content_repository" class="%cmf_routing.orm.content_repository_class%"> | ||
<argument type="service" id="doctrine.orm.default_entity_manager" /> | ||
</service> | ||
|
||
<service id="cmf_routing.route_provider" class="%cmf_routing.route_entity_provider%"> | ||
<argument type="service" id="doctrine"/> | ||
<argument>%cmf_routing.route_entity_class%</argument> | ||
<call method="setManagerName"><argument>%cmf_routing.persistence.orm.manager_name%</argument></call> | ||
</service> | ||
</services> | ||
</container> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, i just noticed that we provide a orm mapping pass in corebundle so that we do not need to up the symfony and doctrine version too much. we should use Symfony\Cmf\Bundle\CoreBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass instead