|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm; |
| 4 | + |
| 5 | +use Symfony\Component\Routing\RouteCollection; |
| 6 | +use Symfony\Component\Routing\Exception\RouteNotFoundException; |
| 7 | + |
| 8 | +use Symfony\Component\HttpFoundation\Request; |
| 9 | + |
| 10 | +use Symfony\Cmf\Component\Routing\RouteProviderInterface; |
| 11 | +use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\DoctrineProvider; |
| 12 | + |
| 13 | +/** |
| 14 | + * Provider loading routes from Doctrine |
| 15 | + * |
| 16 | + * This is <strong>NOT</strong> not a doctrine repository but just the route |
| 17 | + * provider for the NestedMatcher. (you could of course implement this |
| 18 | + * interface in a repository class, if you need that) |
| 19 | + * |
| 20 | + |
| 21 | + */ |
| 22 | +class RouteProvider extends DoctrineProvider implements RouteProviderInterface |
| 23 | +{ |
| 24 | + protected function getCandidates($url) |
| 25 | + { |
| 26 | + $candidates = array(); |
| 27 | + if ('/' !== $url) { |
| 28 | + if (preg_match('/(.+)\.[a-z]+$/i', $url, $matches)) { |
| 29 | + $candidates[] = $url; |
| 30 | + $url = $matches[1]; |
| 31 | + } |
| 32 | + |
| 33 | + $part = $url; |
| 34 | + while (false !== ($pos = strrpos($part, '/'))) { |
| 35 | + $candidates[] = $part; |
| 36 | + $part = substr($url, 0, $pos); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + $candidates[] = '/'; |
| 41 | + |
| 42 | + return $candidates; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * {@inheritDoc} |
| 47 | + */ |
| 48 | + public function getRouteByName($name, $parameters = array()) |
| 49 | + { |
| 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; |
| 56 | + } |
| 57 | + |
| 58 | + public function getRoutesByNames($names, $parameters = array()) |
| 59 | + { |
| 60 | + } |
| 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 | + } |
| 103 | +} |
0 commit comments