-
Notifications
You must be signed in to change notification settings - Fork 76
[WIP] ORM based dynamic routing #122
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
65af83e
3e4ac26
2a2b2da
74cd6f3
6a34a0f
f361535
f5dfd9f
efb7f45
48194cb
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 |
---|---|---|
|
@@ -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. this too |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm; | ||
|
||
use Symfony\Cmf\Component\Routing\ContentRepositoryInterface; | ||
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\DoctrineProvider; | ||
|
||
/** | ||
* Abstract content repository for ORM | ||
* | ||
* @author teito | ||
*/ | ||
class ContentRepository extends DoctrineProvider implements ContentRepositoryInterface | ||
{ | ||
/** | ||
* Determine target class and id for this content | ||
* | ||
* @param mixed $identifier as produced by getContentId | ||
* | ||
* @return array with model first element, id second | ||
*/ | ||
protected function getModelAndId($identifier) | ||
{ | ||
return explode(':', $identifier, 2); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function findById($id) | ||
{ | ||
list($model, $modelId) = $this->getModelAndId($id); | ||
|
||
return $this->getObjectManager()->getRepository($model)->find($modelId); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getContentId($content) | ||
{ | ||
if (! is_object($content)) { | ||
return null; | ||
} | ||
|
||
try { | ||
$meta = $this->getObjectManager()->getClassMetadata(get_class($content)); | ||
$ids = $meta->getIdentifierValues($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) | ||
)); | ||
} catch (\Exception $e) { | ||
return null; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm; | ||
|
||
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 | ||
* | ||
* This is <strong>NOT</strong> not a doctrine repository but just the route | ||
* provider for the NestedMatcher. (you could of course implement this | ||
* interface in a repository class, if you need that) | ||
* | ||
* @author [email protected] | ||
*/ | ||
class RouteProvider extends DoctrineProvider implements RouteProviderInterface | ||
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. Did you forget a use statement for 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. yes, there's some more missing which is going to be submitted in a pull request tonite, then code will be available for review |
||
{ | ||
protected function getCandidates($url) | ||
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. Is this logic the same as with the ODM? Should we encapsulate it in a class? 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. no, the phpcr is using id prefix all over the place here |
||
{ | ||
$candidates = array(); | ||
if ('/' !== $url) { | ||
if (preg_match('/(.+)\.[a-z]+$/i', $url, $matches)) { | ||
$candidates[] = $url; | ||
$url = $matches[1]; | ||
} | ||
|
||
$part = $url; | ||
while (false !== ($pos = strrpos($part, '/'))) { | ||
$candidates[] = $part; | ||
$part = substr($url, 0, $pos); | ||
} | ||
} | ||
|
||
$candidates[] = '/'; | ||
|
||
return $candidates; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
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()) | ||
{ | ||
} | ||
|
||
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,16 @@ | ||
<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"> | ||
<field name="variablePattern" type="string"/> | ||
<field name="addFormatPattern" type="boolean"/> | ||
<field name="staticPrefix" type="string"/> | ||
|
||
<indexes> | ||
<index name="name_idx" columns="name"/> | ||
<index name="prefix_idx" columns="staticPrefix"/> | ||
</indexes> | ||
</mapped-superclass> | ||
|
||
</doctrine-mapping> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<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="position" type="integer"/> | ||
|
||
</entity> | ||
|
||
</doctrine-mapping> |
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.
i guess we need this fix even if we do not merge this PR