Skip to content

[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

Merged
merged 5 commits into from
Jul 30, 2013
Merged
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
29 changes: 29 additions & 0 deletions CmfRoutingBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,6 +38,34 @@ public function build(ContainerBuilder $container)
)
);
}

if (class_exists('Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass')) {
Copy link
Member

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

$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'
)
Copy link
Member

Choose a reason for hiding this comment

The 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'
);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions DependencyInjection/CmfRoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container,
$hasProvider = true;
$hasContentRepository = true;
}

if (!empty($config['persistence']['orm']['enabled'])) {
$this->loadOrmProvider($config['persistence']['orm'], $loader, $container);
$hasProvider = true;
}

if (isset($config['route_provider_service_id'])) {
$container->setAlias('cmf_routing.route_provider', $config['route_provider_service_id']);
$hasProvider = true;
Expand Down Expand Up @@ -171,6 +177,13 @@ public function loadSonataPhpcrAdmin($config, XmlFileLoader $loader, ContainerBu
$loader->load('admin_phpcr.xml');
}

public function loadOrmProvider($config, XmlFileLoader $loader, ContainerBuilder $container)
{
$container->setParameter($this->getAlias() . '.persistence.orm.manager_name', $config['manager_name']);
$container->setParameter($this->getAlias() . '.persistence.orm.enabled', $config['enabled']);
$loader->load('provider_orm.xml');
}

/**
* 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 @@ -72,6 +72,12 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('orm')
->children()
->scalarNode('enabled')->defaultNull()->end()
->scalarNode('manager_name')->defaultNull()->end()
->end()
->end()
->end()
->end()
->scalarNode('uri_filter_regexp')->defaultValue('')->end()
Expand Down
1 change: 1 addition & 0 deletions Doctrine/DoctrineProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ abstract class DoctrineProvider
public function __construct(ManagerRegistry $managerRegistry, $className = null)
{
$this->managerRegistry = $managerRegistry;
$this->className = $className;
Copy link
Member

Choose a reason for hiding this comment

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

ups. thanks

}

/**
Expand Down
6 changes: 5 additions & 1 deletion Doctrine/Orm/ContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

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

use Symfony\Cmf\Component\Routing\ContentRepositoryInterface;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\DoctrineProvider;

/**
* Abstract content repository for PHPCR-ODM
* Abstract content repository for ORM
*
* @author teito
*/
Expand Down Expand Up @@ -46,6 +49,7 @@ public function getContentId($content)
if (0 !== count($ids)) {
throw new \Exception('Multi identifier values not supported in ' . get_class($content));
}

return implode(':', array(
get_class($content),
reset($ids)
Expand Down
27 changes: 27 additions & 0 deletions Doctrine/Orm/Route.php
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;
}
50 changes: 49 additions & 1 deletion Doctrine/Orm/RouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
{
}
Copy link
Member

Choose a reason for hiding this comment

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

can we implement this with a findMany or something like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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>
10 changes: 10 additions & 0 deletions Resources/config/doctrine-model/Route.orm.xml
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"/>
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thx

</mapped-superclass>

</doctrine-mapping>
23 changes: 23 additions & 0 deletions Resources/config/doctrine-orm/Route.orm.xml
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>
24 changes: 24 additions & 0 deletions Resources/config/provider_orm.xml
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>