|
| 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 | + * A limit to apply to the number of candidates generated. |
| 34 | + * |
| 35 | + * This is to prevent abusive requests with a lot of "/". The limit is per |
| 36 | + * batch, that is if a locale matches you could get as many as 2 * $limit |
| 37 | + * candidates if the URL has that many slashes. |
| 38 | + * |
| 39 | + * @var int |
| 40 | + */ |
| 41 | + protected $limit; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param array $locales The locales to support. |
| 45 | + * @param int $limit A limit to apply to the candidates generated. |
| 46 | + */ |
| 47 | + public function __construct(array $locales = array(), $limit = 20) |
| 48 | + { |
| 49 | + $this->setLocales($locales); |
| 50 | + $this->limit = $limit; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Set the locales to support by this strategy. |
| 55 | + * |
| 56 | + * @param array $locales The locales to support. |
| 57 | + */ |
| 58 | + public function setLocales(array $locales) |
| 59 | + { |
| 60 | + $this->locales = $locales; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * {@inheritDoc} |
| 65 | + * |
| 66 | + * Always returns true. |
| 67 | + */ |
| 68 | + public function isCandidate($name) |
| 69 | + { |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * {@inheritDoc} |
| 75 | + * |
| 76 | + * Does nothing. |
| 77 | + */ |
| 78 | + public function restrictQuery($queryBuilder) |
| 79 | + { |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * {@inheritDoc} |
| 84 | + */ |
| 85 | + public function getCandidates(Request $request) |
| 86 | + { |
| 87 | + $url = $request->getPathInfo(); |
| 88 | + $candidates = $this->getCandidatesFor($url); |
| 89 | + |
| 90 | + $locale = $this->determineLocale($url); |
| 91 | + if ($locale) { |
| 92 | + $candidates = array_unique(array_merge($candidates, $this->getCandidatesFor(substr($url, strlen($locale) + 1)))); |
| 93 | + } |
| 94 | + |
| 95 | + return $candidates; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Determine the locale of this URL. |
| 100 | + * |
| 101 | + * @param string $url The url to determine the locale from. |
| 102 | + * |
| 103 | + * @return string|boolean The locale if $url starts with one of the allowed locales. |
| 104 | + */ |
| 105 | + protected function determineLocale($url) |
| 106 | + { |
| 107 | + if (!count($this->locales)) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + $matches = array(); |
| 112 | + if (preg_match('#(' . implode('|', $this->locales) . ')(/|$)#', $url, $matches)) { |
| 113 | + return $matches[1]; |
| 114 | + } |
| 115 | + |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Handle a possible format extension and split the $url on "/". |
| 121 | + * |
| 122 | + * $prefix is prepended to every candidate generated. |
| 123 | + * |
| 124 | + * @param string $url The URL to split. |
| 125 | + * @param string $prefix A prefix to prepend to every pattern. |
| 126 | + * |
| 127 | + * @return array Paths that could represent routes that match $url and are |
| 128 | + * child of $prefix. |
| 129 | + */ |
| 130 | + protected function getCandidatesFor($url, $prefix = '') |
| 131 | + { |
| 132 | + $candidates = array(); |
| 133 | + if ('/' !== $url) { |
| 134 | + // handle format extension, like .html or .json |
| 135 | + if (preg_match('/(.+)\.[a-z]+$/i', $url, $matches)) { |
| 136 | + $candidates[] = $prefix . $url; |
| 137 | + $url = $matches[1]; |
| 138 | + } |
| 139 | + |
| 140 | + $part = $url; |
| 141 | + $count = 0; |
| 142 | + while (false !== ($pos = strrpos($part, '/'))) { |
| 143 | + if (++$count > $this->limit) { |
| 144 | + return $candidates; |
| 145 | + } |
| 146 | + $candidates[] = $prefix . $part; |
| 147 | + $part = substr($url, 0, $pos); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + $candidates[] = $prefix ?: '/'; |
| 152 | + |
| 153 | + return $candidates; |
| 154 | + } |
| 155 | +} |
0 commit comments