|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony CMF package. |
| 5 | + * |
| 6 | + * (c) 2011-2014 Symfony CMF |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Cmf\Component\Routing\Candidates; |
| 13 | + |
| 14 | +use Doctrine\ODM\PHPCR\DocumentManager; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | + |
| 17 | +/** |
| 18 | + * A straightforward strategy that splits the URL on "/". |
| 19 | + * |
| 20 | + * If locales is set, additionally generates candidates removing the locale if |
| 21 | + * it is one of the configured locales, for non-locale specific URLs. |
| 22 | + * |
| 23 | + * @author David Buchmann <[email protected]> |
| 24 | + */ |
| 25 | +class Candidates implements CandidatesInterface |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var array |
| 29 | + */ |
| 30 | + protected $locales; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param array $locales The locales to support. |
| 34 | + */ |
| 35 | + public function __construct(array $locales = array()) |
| 36 | + { |
| 37 | + $this->setLocales($locales); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Set the locales to support by this strategy. |
| 42 | + * |
| 43 | + * @param array $locales The locales to support. |
| 44 | + */ |
| 45 | + public function setLocales(array $locales) |
| 46 | + { |
| 47 | + $this->locales = $locales; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritDoc} |
| 52 | + * |
| 53 | + * Always returns true. |
| 54 | + */ |
| 55 | + public function isCandidate($name) |
| 56 | + { |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * {@inheritDoc} |
| 62 | + * |
| 63 | + * Does nothing. |
| 64 | + */ |
| 65 | + public function restrictQuery($queryBuilder) |
| 66 | + { |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * {@inheritDoc} |
| 71 | + */ |
| 72 | + public function getCandidates(Request $request) |
| 73 | + { |
| 74 | + $url = $request->getPathInfo(); |
| 75 | + $candidates = $this->getCandidatesFor($url); |
| 76 | + |
| 77 | + $locale = $this->determineLocale($url); |
| 78 | + if ($locale) { |
| 79 | + $candidates = array_unique(array_merge($candidates, $this->getCandidatesFor(substr($url, strlen($locale) + 1)))); |
| 80 | + } |
| 81 | + |
| 82 | + return $candidates; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Determine the locale of this URL. |
| 87 | + * |
| 88 | + * @param string $url The url to determine the locale from. |
| 89 | + * |
| 90 | + * @return string|boolean The locale if $url starts with one of the allowed locales. |
| 91 | + */ |
| 92 | + protected function determineLocale($url) |
| 93 | + { |
| 94 | + if (!count($this->locales)) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + $matches = array(); |
| 99 | + if (preg_match('#(' . implode('|', $this->locales) . ')(/|$)#', $url, $matches)) { |
| 100 | + return $matches[1]; |
| 101 | + } |
| 102 | + |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Handle a possible format extension and split the $url on "/". |
| 108 | + * |
| 109 | + * $prefix is prepended to every candidate generated. |
| 110 | + * |
| 111 | + * @param string $url The URL to split. |
| 112 | + * @param string $prefix A prefix to prepend to every pattern. |
| 113 | + * |
| 114 | + * @return array Paths that could represent routes that match $url and are |
| 115 | + * child of $prefix. |
| 116 | + */ |
| 117 | + protected function getCandidatesFor($url, $prefix = '') |
| 118 | + { |
| 119 | + $candidates = array(); |
| 120 | + if ('/' !== $url) { |
| 121 | + // handle format extension, like .html or .json |
| 122 | + if (preg_match('/(.+)\.[a-z]+$/i', $url, $matches)) { |
| 123 | + $candidates[] = $prefix . $url; |
| 124 | + $url = $matches[1]; |
| 125 | + } |
| 126 | + |
| 127 | + $part = $url; |
| 128 | + while (false !== ($pos = strrpos($part, '/'))) { |
| 129 | + $candidates[] = $prefix . $part; |
| 130 | + $part = substr($url, 0, $pos); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + $candidates[] = $prefix ?: '/'; |
| 135 | + |
| 136 | + return $candidates; |
| 137 | + } |
| 138 | +} |
0 commit comments