-
Notifications
You must be signed in to change notification settings - Fork 77
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
port features from simple cms into routing bundle to simplify things #210
Changes from 3 commits
d81035a
3eef9cd
004dced
623a6ad
badf1d8
f3c27b8
0b2ce04
b6729db
a1205ec
dbbd077
6e546e1
2c9293e
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 |
---|---|---|
|
@@ -77,9 +77,32 @@ public function getConfigTreeBuilder() | |
->arrayNode('phpcr') | ||
->addDefaultsIfNotSet() | ||
->canBeEnabled() | ||
->fixXmlConfig('route_basepath') | ||
->beforeNormalization() | ||
->ifTrue(function ($v) { | ||
return isset($v['route_basepath']); | ||
}) | ||
->then(function ($v) { | ||
$base = isset($v['route_basepaths']) ? $v['route_basepaths'] : array(); | ||
if (is_array($v['route_basepath'])) { | ||
// xml configuration | ||
$base += $v['route_basepath']; | ||
} else { | ||
$base[] = $v['route_basepath']; | ||
} | ||
$v['route_basepaths'] = array_unique($base); | ||
unset($v['route_basepath']); | ||
|
||
return $v; | ||
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. @wouterj there is no easier way to do this, right? i kind of collide with the fixXmlConfig thing. should i even remove fixXmlConfig('route_basepath') above, as it won't ever happen? 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. I would handle it something different. Or you configure the route_basepaths option or the route_basepath, but not both. It'll look like: ->validate()
->ifTrue(function ($v) { isset($v['route_basepath']) && isset($v['route_basepaths']); })
->thenInvalid('Found values for both "route_basepath" and "route_basepaths", use "route_basepaths" instead.')
->end() 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. And then you can remove the normalizer and just use the 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. oh, that will "accidentally" fix it even if i am using the old format with yml / php configuration, right? 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. It will fix it if you use the old format, and it'll break if you use both the old and new format |
||
}) | ||
->end() | ||
->children() | ||
->scalarNode('manager_name')->defaultNull()->end() | ||
->scalarNode('route_basepath')->defaultValue('/cms/routes')->end() | ||
->arrayNode('route_basepaths') | ||
->prototype('scalar')->end() | ||
->defaultValue(array('/cms/routes')) | ||
->end() | ||
->scalarNode('admin_basepath')->end() | ||
->scalarNode('content_basepath')->defaultValue('/cms/content')->end() | ||
->enumNode('use_sonata_admin') | ||
->beforeNormalization() | ||
|
@@ -122,6 +145,8 @@ public function getConfigTreeBuilder() | |
->arrayNode('locales') | ||
->prototype('scalar')->end() | ||
->end() | ||
->booleanNode('match_implicit_locale')->defaultValue(true)->end() | ||
->booleanNode('auto_locale_pattern')->defaultValue(false)->end() | ||
->end() | ||
->end() | ||
->end() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ | |
|
||
namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm; | ||
|
||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Doctrine\Common\Persistence\ObjectRepository; | ||
use Symfony\Cmf\Component\Routing\Candidates\CandidatesInterface; | ||
use Symfony\Component\Routing\RouteCollection; | ||
use Symfony\Component\Routing\Exception\RouteNotFoundException; | ||
|
||
|
@@ -32,37 +35,46 @@ | |
class RouteProvider extends DoctrineProvider implements RouteProviderInterface | ||
{ | ||
/** | ||
* @param $url | ||
* | ||
* @return array | ||
* @var CandidatesInterface | ||
*/ | ||
protected function getCandidates($url) | ||
private $candidatesStrategy; | ||
|
||
public function __construct(ManagerRegistry $managerRegistry, CandidatesInterface $candidatesStrategy, $className) | ||
{ | ||
$candidates = array(); | ||
if ('/' !== $url) { | ||
if (preg_match('/(.+)\.[a-z]+$/i', $url, $matches)) { | ||
$candidates[] = $url; | ||
$url = $matches[1]; | ||
} | ||
parent::__construct($managerRegistry, $className); | ||
$this->candidatesStrategy = $candidatesStrategy; | ||
} | ||
|
||
$part = $url; | ||
while (false !== ($pos = strrpos($part, '/'))) { | ||
$candidates[] = $part; | ||
$part = substr($url, 0, $pos); | ||
} | ||
} | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getRouteCollectionForRequest(Request $request) | ||
{ | ||
$collection = new RouteCollection(); | ||
|
||
$candidates[] = '/'; | ||
$candidates = $this->candidatesStrategy->getCandidates($request); | ||
if (empty($candidates)) { | ||
return $collection; | ||
} | ||
$routes = $this->getRouteRepository()->findByStaticPrefix($candidates, array('position' => 'ASC')); | ||
/** @var $route Route */ | ||
foreach ($routes as $route) { | ||
$collection->add($route->getName(), $route); | ||
} | ||
|
||
return $candidates; | ||
return $collection; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getRouteByName($name) | ||
{ | ||
$route = $this->getRoutesRepository()->findOneBy(array('name' => $name)); | ||
if (!$this->candidatesStrategy->isCandidate($name)) { | ||
throw new RouteNotFoundException(sprintf('Route "%s" is not handled by this route provider', $name)); | ||
} | ||
|
||
$route = $this->getRouteRepository()->findOneBy(array('name' => $name)); | ||
if (!$route) { | ||
throw new RouteNotFoundException("No route found for name '$name'"); | ||
} | ||
|
@@ -76,21 +88,18 @@ public function getRouteByName($name) | |
public function getRoutesByNames($names = null) | ||
{ | ||
if (null === $names) { | ||
$names = array(); | ||
if (0 === $this->routeCollectionLimit) { | ||
return $names; | ||
} | ||
if (null !== $this->routeCollectionLimit) { | ||
return $this->getRoutesRepository()->findBy(array(), null, $this->routeCollectionLimit); | ||
return array(); | ||
} | ||
|
||
return $this->getRoutesRepository()->findAll(); | ||
return $this->getRouteRepository()->findBy(array(), null, $this->routeCollectionLimit); | ||
} | ||
|
||
$routes = array(); | ||
foreach ($names as $name) { | ||
// TODO: if we do findByName with multivalue, we need to filter with isCandidate afterwards | ||
try { | ||
$routes[] = $this->getRouteByName($name, $parameters); | ||
$routes[] = $this->getRouteByName($name); | ||
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. why is this not $routeRepository->findByName instead? in the request candidates we assume that passing an array to findByYX works. 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. since when does PHPCR support dynamic findByXx methods? I don't think passing an array to it works for ORM. 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. we are in the orm part here so that works. |
||
} catch (RouteNotFoundException $e) { | ||
// not found | ||
} | ||
|
@@ -100,39 +109,9 @@ public function getRoutesByNames($names = null) | |
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getRouteCollectionForRequest(Request $request) | ||
{ | ||
$url = $request->getPathInfo(); | ||
|
||
$candidates = $this->getCandidates($url); | ||
|
||
$collection = new RouteCollection(); | ||
|
||
if (empty($candidates)) { | ||
return $collection; | ||
} | ||
|
||
$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]); | ||
} | ||
$collection->add($key, $route); | ||
} | ||
|
||
return $collection; | ||
} | ||
|
||
/** | ||
* @return \Doctrine\Common\Persistence\ObjectRepository | ||
* @return ObjectRepository | ||
*/ | ||
protected function getRoutesRepository() | ||
protected function getRouteRepository() | ||
{ | ||
return $this->getObjectManager()->getRepository($this->className); | ||
} | ||
|
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.
do we need to keep the route_basepath parameter in the DI for BC or not?
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 don't think so