diff options
author | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-03 19:26:54 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-03 19:26:54 +0200 |
commit | 4f5b44bd3bd490309eb2ba7b44df4769816ba729 (patch) | |
tree | 6cefe170dfe0a5a361cb1e2d1fc4d580a3316d02 /vendor/symfony/routing/Symfony/Component/Routing/Generator | |
parent | 2b840e0cfb63a453bea67a98541f3df9c273c5f5 (diff) | |
download | wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.gz wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.zst wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.zip |
twig implementation
Diffstat (limited to 'vendor/symfony/routing/Symfony/Component/Routing/Generator')
6 files changed, 673 insertions, 0 deletions
diff --git a/vendor/symfony/routing/Symfony/Component/Routing/Generator/ConfigurableRequirementsInterface.php b/vendor/symfony/routing/Symfony/Component/Routing/Generator/ConfigurableRequirementsInterface.php new file mode 100644 index 00000000..5925838c --- /dev/null +++ b/vendor/symfony/routing/Symfony/Component/Routing/Generator/ConfigurableRequirementsInterface.php | |||
@@ -0,0 +1,55 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
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\Component\Routing\Generator; | ||
13 | |||
14 | /** | ||
15 | * ConfigurableRequirementsInterface must be implemented by URL generators that | ||
16 | * can be configured whether an exception should be generated when the parameters | ||
17 | * do not match the requirements. It is also possible to disable the requirements | ||
18 | * check for URL generation completely. | ||
19 | * | ||
20 | * The possible configurations and use-cases: | ||
21 | * - setStrictRequirements(true): Throw an exception for mismatching requirements. This | ||
22 | * is mostly useful in development environment. | ||
23 | * - setStrictRequirements(false): Don't throw an exception but return null as URL for | ||
24 | * mismatching requirements and log the problem. Useful when you cannot control all | ||
25 | * params because they come from third party libs but don't want to have a 404 in | ||
26 | * production environment. It should log the mismatch so one can review it. | ||
27 | * - setStrictRequirements(null): Return the URL with the given parameters without | ||
28 | * checking the requirements at all. When generating an URL you should either trust | ||
29 | * your params or you validated them beforehand because otherwise it would break your | ||
30 | * link anyway. So in production environment you should know that params always pass | ||
31 | * the requirements. Thus this option allows to disable the check on URL generation for | ||
32 | * performance reasons (saving a preg_match for each requirement every time a URL is | ||
33 | * generated). | ||
34 | * | ||
35 | * @author Fabien Potencier <fabien@symfony.com> | ||
36 | * @author Tobias Schultze <http://tobion.de> | ||
37 | */ | ||
38 | interface ConfigurableRequirementsInterface | ||
39 | { | ||
40 | /** | ||
41 | * Enables or disables the exception on incorrect parameters. | ||
42 | * Passing null will deactivate the requirements check completely. | ||
43 | * | ||
44 | * @param Boolean|null $enabled | ||
45 | */ | ||
46 | public function setStrictRequirements($enabled); | ||
47 | |||
48 | /** | ||
49 | * Returns whether to throw an exception on incorrect parameters. | ||
50 | * Null means the requirements check is deactivated completely. | ||
51 | * | ||
52 | * @return Boolean|null | ||
53 | */ | ||
54 | public function isStrictRequirements(); | ||
55 | } | ||
diff --git a/vendor/symfony/routing/Symfony/Component/Routing/Generator/Dumper/GeneratorDumper.php b/vendor/symfony/routing/Symfony/Component/Routing/Generator/Dumper/GeneratorDumper.php new file mode 100644 index 00000000..4739bd83 --- /dev/null +++ b/vendor/symfony/routing/Symfony/Component/Routing/Generator/Dumper/GeneratorDumper.php | |||
@@ -0,0 +1,45 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
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\Component\Routing\Generator\Dumper; | ||
13 | |||
14 | use Symfony\Component\Routing\RouteCollection; | ||
15 | |||
16 | /** | ||
17 | * GeneratorDumper is the base class for all built-in generator dumpers. | ||
18 | * | ||
19 | * @author Fabien Potencier <fabien@symfony.com> | ||
20 | */ | ||
21 | abstract class GeneratorDumper implements GeneratorDumperInterface | ||
22 | { | ||
23 | /** | ||
24 | * @var RouteCollection | ||
25 | */ | ||
26 | private $routes; | ||
27 | |||
28 | /** | ||
29 | * Constructor. | ||
30 | * | ||
31 | * @param RouteCollection $routes The RouteCollection to dump | ||
32 | */ | ||
33 | public function __construct(RouteCollection $routes) | ||
34 | { | ||
35 | $this->routes = $routes; | ||
36 | } | ||
37 | |||
38 | /** | ||
39 | * {@inheritdoc} | ||
40 | */ | ||
41 | public function getRoutes() | ||
42 | { | ||
43 | return $this->routes; | ||
44 | } | ||
45 | } | ||
diff --git a/vendor/symfony/routing/Symfony/Component/Routing/Generator/Dumper/GeneratorDumperInterface.php b/vendor/symfony/routing/Symfony/Component/Routing/Generator/Dumper/GeneratorDumperInterface.php new file mode 100644 index 00000000..deb0c0a2 --- /dev/null +++ b/vendor/symfony/routing/Symfony/Component/Routing/Generator/Dumper/GeneratorDumperInterface.php | |||
@@ -0,0 +1,41 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
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\Component\Routing\Generator\Dumper; | ||
13 | |||
14 | use Symfony\Component\Routing\RouteCollection; | ||
15 | |||
16 | /** | ||
17 | * GeneratorDumperInterface is the interface that all generator dumper classes must implement. | ||
18 | * | ||
19 | * @author Fabien Potencier <fabien@symfony.com> | ||
20 | * | ||
21 | * @api | ||
22 | */ | ||
23 | interface GeneratorDumperInterface | ||
24 | { | ||
25 | /** | ||
26 | * Dumps a set of routes to a string representation of executable code | ||
27 | * that can then be used to generate a URL of such a route. | ||
28 | * | ||
29 | * @param array $options An array of options | ||
30 | * | ||
31 | * @return string Executable code | ||
32 | */ | ||
33 | public function dump(array $options = array()); | ||
34 | |||
35 | /** | ||
36 | * Gets the routes to dump. | ||
37 | * | ||
38 | * @return RouteCollection A RouteCollection instance | ||
39 | */ | ||
40 | public function getRoutes(); | ||
41 | } | ||
diff --git a/vendor/symfony/routing/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php b/vendor/symfony/routing/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php new file mode 100644 index 00000000..42cd9108 --- /dev/null +++ b/vendor/symfony/routing/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php | |||
@@ -0,0 +1,123 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
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\Component\Routing\Generator\Dumper; | ||
13 | |||
14 | /** | ||
15 | * PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes. | ||
16 | * | ||
17 | * @author Fabien Potencier <fabien@symfony.com> | ||
18 | * @author Tobias Schultze <http://tobion.de> | ||
19 | * | ||
20 | * @api | ||
21 | */ | ||
22 | class PhpGeneratorDumper extends GeneratorDumper | ||
23 | { | ||
24 | /** | ||
25 | * Dumps a set of routes to a PHP class. | ||
26 | * | ||
27 | * Available options: | ||
28 | * | ||
29 | * * class: The class name | ||
30 | * * base_class: The base class name | ||
31 | * | ||
32 | * @param array $options An array of options | ||
33 | * | ||
34 | * @return string A PHP class representing the generator class | ||
35 | * | ||
36 | * @api | ||
37 | */ | ||
38 | public function dump(array $options = array()) | ||
39 | { | ||
40 | $options = array_merge(array( | ||
41 | 'class' => 'ProjectUrlGenerator', | ||
42 | 'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator', | ||
43 | ), $options); | ||
44 | |||
45 | return <<<EOF | ||
46 | <?php | ||
47 | |||
48 | use Symfony\Component\Routing\RequestContext; | ||
49 | use Symfony\Component\Routing\Exception\RouteNotFoundException; | ||
50 | use Psr\Log\LoggerInterface; | ||
51 | |||
52 | /** | ||
53 | * {$options['class']} | ||
54 | * | ||
55 | * This class has been auto-generated | ||
56 | * by the Symfony Routing Component. | ||
57 | */ | ||
58 | class {$options['class']} extends {$options['base_class']} | ||
59 | { | ||
60 | static private \$declaredRoutes = {$this->generateDeclaredRoutes()}; | ||
61 | |||
62 | /** | ||
63 | * Constructor. | ||
64 | */ | ||
65 | public function __construct(RequestContext \$context, LoggerInterface \$logger = null) | ||
66 | { | ||
67 | \$this->context = \$context; | ||
68 | \$this->logger = \$logger; | ||
69 | } | ||
70 | |||
71 | {$this->generateGenerateMethod()} | ||
72 | } | ||
73 | |||
74 | EOF; | ||
75 | } | ||
76 | |||
77 | /** | ||
78 | * Generates PHP code representing an array of defined routes | ||
79 | * together with the routes properties (e.g. requirements). | ||
80 | * | ||
81 | * @return string PHP code | ||
82 | */ | ||
83 | private function generateDeclaredRoutes() | ||
84 | { | ||
85 | $routes = "array(\n"; | ||
86 | foreach ($this->getRoutes()->all() as $name => $route) { | ||
87 | $compiledRoute = $route->compile(); | ||
88 | |||
89 | $properties = array(); | ||
90 | $properties[] = $compiledRoute->getVariables(); | ||
91 | $properties[] = $route->getDefaults(); | ||
92 | $properties[] = $route->getRequirements(); | ||
93 | $properties[] = $compiledRoute->getTokens(); | ||
94 | $properties[] = $compiledRoute->getHostTokens(); | ||
95 | |||
96 | $routes .= sprintf(" '%s' => %s,\n", $name, str_replace("\n", '', var_export($properties, true))); | ||
97 | } | ||
98 | $routes .= ' )'; | ||
99 | |||
100 | return $routes; | ||
101 | } | ||
102 | |||
103 | /** | ||
104 | * Generates PHP code representing the `generate` method that implements the UrlGeneratorInterface. | ||
105 | * | ||
106 | * @return string PHP code | ||
107 | */ | ||
108 | private function generateGenerateMethod() | ||
109 | { | ||
110 | return <<<EOF | ||
111 | public function generate(\$name, \$parameters = array(), \$referenceType = self::ABSOLUTE_PATH) | ||
112 | { | ||
113 | if (!isset(self::\$declaredRoutes[\$name])) { | ||
114 | throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', \$name)); | ||
115 | } | ||
116 | |||
117 | list(\$variables, \$defaults, \$requirements, \$tokens, \$hostTokens) = self::\$declaredRoutes[\$name]; | ||
118 | |||
119 | return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$referenceType, \$hostTokens); | ||
120 | } | ||
121 | EOF; | ||
122 | } | ||
123 | } | ||
diff --git a/vendor/symfony/routing/Symfony/Component/Routing/Generator/UrlGenerator.php b/vendor/symfony/routing/Symfony/Component/Routing/Generator/UrlGenerator.php new file mode 100644 index 00000000..f224cb3f --- /dev/null +++ b/vendor/symfony/routing/Symfony/Component/Routing/Generator/UrlGenerator.php | |||
@@ -0,0 +1,322 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
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\Component\Routing\Generator; | ||
13 | |||
14 | use Symfony\Component\Routing\RouteCollection; | ||
15 | use Symfony\Component\Routing\RequestContext; | ||
16 | use Symfony\Component\Routing\Exception\InvalidParameterException; | ||
17 | use Symfony\Component\Routing\Exception\RouteNotFoundException; | ||
18 | use Symfony\Component\Routing\Exception\MissingMandatoryParametersException; | ||
19 | use Psr\Log\LoggerInterface; | ||
20 | |||
21 | /** | ||
22 | * UrlGenerator can generate a URL or a path for any route in the RouteCollection | ||
23 | * based on the passed parameters. | ||
24 | * | ||
25 | * @author Fabien Potencier <fabien@symfony.com> | ||
26 | * @author Tobias Schultze <http://tobion.de> | ||
27 | * | ||
28 | * @api | ||
29 | */ | ||
30 | class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInterface | ||
31 | { | ||
32 | /** | ||
33 | * @var RouteCollection | ||
34 | */ | ||
35 | protected $routes; | ||
36 | |||
37 | /** | ||
38 | * @var RequestContext | ||
39 | */ | ||
40 | protected $context; | ||
41 | |||
42 | /** | ||
43 | * @var Boolean|null | ||
44 | */ | ||
45 | protected $strictRequirements = true; | ||
46 | |||
47 | /** | ||
48 | * @var LoggerInterface|null | ||
49 | */ | ||
50 | protected $logger; | ||
51 | |||
52 | /** | ||
53 | * This array defines the characters (besides alphanumeric ones) that will not be percent-encoded in the path segment of the generated URL. | ||
54 | * | ||
55 | * PHP's rawurlencode() encodes all chars except "a-zA-Z0-9-._~" according to RFC 3986. But we want to allow some chars | ||
56 | * to be used in their literal form (reasons below). Other chars inside the path must of course be encoded, e.g. | ||
57 | * "?" and "#" (would be interpreted wrongly as query and fragment identifier), | ||
58 | * "'" and """ (are used as delimiters in HTML). | ||
59 | */ | ||
60 | protected $decodedChars = array( | ||
61 | // the slash can be used to designate a hierarchical structure and we want allow using it with this meaning | ||
62 | // some webservers don't allow the slash in encoded form in the path for security reasons anyway | ||
63 | // see http://stackoverflow.com/questions/4069002/http-400-if-2f-part-of-get-url-in-jboss | ||
64 | '%2F' => '/', | ||
65 | // the following chars are general delimiters in the URI specification but have only special meaning in the authority component | ||
66 | // so they can safely be used in the path in unencoded form | ||
67 | '%40' => '@', | ||
68 | '%3A' => ':', | ||
69 | // these chars are only sub-delimiters that have no predefined meaning and can therefore be used literally | ||
70 | // so URI producing applications can use these chars to delimit subcomponents in a path segment without being encoded for better readability | ||
71 | '%3B' => ';', | ||
72 | '%2C' => ',', | ||
73 | '%3D' => '=', | ||
74 | '%2B' => '+', | ||
75 | '%21' => '!', | ||
76 | '%2A' => '*', | ||
77 | '%7C' => '|', | ||
78 | ); | ||
79 | |||
80 | /** | ||
81 | * Constructor. | ||
82 | * | ||
83 | * @param RouteCollection $routes A RouteCollection instance | ||
84 | * @param RequestContext $context The context | ||
85 | * @param LoggerInterface|null $logger A logger instance | ||
86 | * | ||
87 | * @api | ||
88 | */ | ||
89 | public function __construct(RouteCollection $routes, RequestContext $context, LoggerInterface $logger = null) | ||
90 | { | ||
91 | $this->routes = $routes; | ||
92 | $this->context = $context; | ||
93 | $this->logger = $logger; | ||
94 | } | ||
95 | |||
96 | /** | ||
97 | * {@inheritdoc} | ||
98 | */ | ||
99 | public function setContext(RequestContext $context) | ||
100 | { | ||
101 | $this->context = $context; | ||
102 | } | ||
103 | |||
104 | /** | ||
105 | * {@inheritdoc} | ||
106 | */ | ||
107 | public function getContext() | ||
108 | { | ||
109 | return $this->context; | ||
110 | } | ||
111 | |||
112 | /** | ||
113 | * {@inheritdoc} | ||
114 | */ | ||
115 | public function setStrictRequirements($enabled) | ||
116 | { | ||
117 | $this->strictRequirements = null === $enabled ? null : (Boolean) $enabled; | ||
118 | } | ||
119 | |||
120 | /** | ||
121 | * {@inheritdoc} | ||
122 | */ | ||
123 | public function isStrictRequirements() | ||
124 | { | ||
125 | return $this->strictRequirements; | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * {@inheritDoc} | ||
130 | */ | ||
131 | public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) | ||
132 | { | ||
133 | if (null === $route = $this->routes->get($name)) { | ||
134 | throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name)); | ||
135 | } | ||
136 | |||
137 | // the Route has a cache of its own and is not recompiled as long as it does not get modified | ||
138 | $compiledRoute = $route->compile(); | ||
139 | |||
140 | return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $name, $referenceType, $compiledRoute->getHostTokens()); | ||
141 | } | ||
142 | |||
143 | /** | ||
144 | * @throws MissingMandatoryParametersException When some parameters are missing that are mandatory for the route | ||
145 | * @throws InvalidParameterException When a parameter value for a placeholder is not correct because | ||
146 | * it does not match the requirement | ||
147 | */ | ||
148 | protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens) | ||
149 | { | ||
150 | $variables = array_flip($variables); | ||
151 | $mergedParams = array_replace($defaults, $this->context->getParameters(), $parameters); | ||
152 | |||
153 | // all params must be given | ||
154 | if ($diff = array_diff_key($variables, $mergedParams)) { | ||
155 | throw new MissingMandatoryParametersException(sprintf('Some mandatory parameters are missing ("%s") to generate a URL for route "%s".', implode('", "', array_keys($diff)), $name)); | ||
156 | } | ||
157 | |||
158 | $url = ''; | ||
159 | $optional = true; | ||
160 | foreach ($tokens as $token) { | ||
161 | if ('variable' === $token[0]) { | ||
162 | if (!$optional || !array_key_exists($token[3], $defaults) || null !== $mergedParams[$token[3]] && (string) $mergedParams[$token[3]] !== (string) $defaults[$token[3]]) { | ||
163 | // check requirement | ||
164 | if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) { | ||
165 | $message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given) to generate a corresponding URL.', $token[3], $name, $token[2], $mergedParams[$token[3]]); | ||
166 | if ($this->strictRequirements) { | ||
167 | throw new InvalidParameterException($message); | ||
168 | } | ||
169 | |||
170 | if ($this->logger) { | ||
171 | $this->logger->error($message); | ||
172 | } | ||
173 | |||
174 | return null; | ||
175 | } | ||
176 | |||
177 | $url = $token[1].$mergedParams[$token[3]].$url; | ||
178 | $optional = false; | ||
179 | } | ||
180 | } else { | ||
181 | // static text | ||
182 | $url = $token[1].$url; | ||
183 | $optional = false; | ||
184 | } | ||
185 | } | ||
186 | |||
187 | if ('' === $url) { | ||
188 | $url = '/'; | ||
189 | } | ||
190 | |||
191 | // the contexts base url is already encoded (see Symfony\Component\HttpFoundation\Request) | ||
192 | $url = strtr(rawurlencode($url), $this->decodedChars); | ||
193 | |||
194 | // the path segments "." and ".." are interpreted as relative reference when resolving a URI; see http://tools.ietf.org/html/rfc3986#section-3.3 | ||
195 | // so we need to encode them as they are not used for this purpose here | ||
196 | // otherwise we would generate a URI that, when followed by a user agent (e.g. browser), does not match this route | ||
197 | $url = strtr($url, array('/../' => '/%2E%2E/', '/./' => '/%2E/')); | ||
198 | if ('/..' === substr($url, -3)) { | ||
199 | $url = substr($url, 0, -2).'%2E%2E'; | ||
200 | } elseif ('/.' === substr($url, -2)) { | ||
201 | $url = substr($url, 0, -1).'%2E'; | ||
202 | } | ||
203 | |||
204 | $schemeAuthority = ''; | ||
205 | if ($host = $this->context->getHost()) { | ||
206 | $scheme = $this->context->getScheme(); | ||
207 | if (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme !== $req) { | ||
208 | $referenceType = self::ABSOLUTE_URL; | ||
209 | $scheme = $req; | ||
210 | } | ||
211 | |||
212 | if ($hostTokens) { | ||
213 | $routeHost = ''; | ||
214 | foreach ($hostTokens as $token) { | ||
215 | if ('variable' === $token[0]) { | ||
216 | if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) { | ||
217 | $message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given) to generate a corresponding URL.', $token[3], $name, $token[2], $mergedParams[$token[3]]); | ||
218 | |||
219 | if ($this->strictRequirements) { | ||
220 | throw new InvalidParameterException($message); | ||
221 | } | ||
222 | |||
223 | if ($this->logger) { | ||
224 | $this->logger->error($message); | ||
225 | } | ||
226 | |||
227 | return null; | ||
228 | } | ||
229 | |||
230 | $routeHost = $token[1].$mergedParams[$token[3]].$routeHost; | ||
231 | } else { | ||
232 | $routeHost = $token[1].$routeHost; | ||
233 | } | ||
234 | } | ||
235 | |||
236 | if ($routeHost !== $host) { | ||
237 | $host = $routeHost; | ||
238 | if (self::ABSOLUTE_URL !== $referenceType) { | ||
239 | $referenceType = self::NETWORK_PATH; | ||
240 | } | ||
241 | } | ||
242 | } | ||
243 | |||
244 | if (self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) { | ||
245 | $port = ''; | ||
246 | if ('http' === $scheme && 80 != $this->context->getHttpPort()) { | ||
247 | $port = ':'.$this->context->getHttpPort(); | ||
248 | } elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) { | ||
249 | $port = ':'.$this->context->getHttpsPort(); | ||
250 | } | ||
251 | |||
252 | $schemeAuthority = self::NETWORK_PATH === $referenceType ? '//' : "$scheme://"; | ||
253 | $schemeAuthority .= $host.$port; | ||
254 | } | ||
255 | } | ||
256 | |||
257 | if (self::RELATIVE_PATH === $referenceType) { | ||
258 | $url = self::getRelativePath($this->context->getPathInfo(), $url); | ||
259 | } else { | ||
260 | $url = $schemeAuthority.$this->context->getBaseUrl().$url; | ||
261 | } | ||
262 | |||
263 | // add a query string if needed | ||
264 | $extra = array_diff_key($parameters, $variables, $defaults); | ||
265 | if ($extra && $query = http_build_query($extra, '', '&')) { | ||
266 | $url .= '?'.$query; | ||
267 | } | ||
268 | |||
269 | return $url; | ||
270 | } | ||
271 | |||
272 | /** | ||
273 | * Returns the target path as relative reference from the base path. | ||
274 | * | ||
275 | * Only the URIs path component (no schema, host etc.) is relevant and must be given, starting with a slash. | ||
276 | * Both paths must be absolute and not contain relative parts. | ||
277 | * Relative URLs from one resource to another are useful when generating self-contained downloadable document archives. | ||
278 | * Furthermore, they can be used to reduce the link size in documents. | ||
279 | * | ||
280 | * Example target paths, given a base path of "/a/b/c/d": | ||
281 | * - "/a/b/c/d" -> "" | ||
282 | * - "/a/b/c/" -> "./" | ||
283 | * - "/a/b/" -> "../" | ||
284 | * - "/a/b/c/other" -> "other" | ||
285 | * - "/a/x/y" -> "../../x/y" | ||
286 | * | ||
287 | * @param string $basePath The base path | ||
288 | * @param string $targetPath The target path | ||
289 | * | ||
290 | * @return string The relative target path | ||
291 | */ | ||
292 | public static function getRelativePath($basePath, $targetPath) | ||
293 | { | ||
294 | if ($basePath === $targetPath) { | ||
295 | return ''; | ||
296 | } | ||
297 | |||
298 | $sourceDirs = explode('/', isset($basePath[0]) && '/' === $basePath[0] ? substr($basePath, 1) : $basePath); | ||
299 | $targetDirs = explode('/', isset($targetPath[0]) && '/' === $targetPath[0] ? substr($targetPath, 1) : $targetPath); | ||
300 | array_pop($sourceDirs); | ||
301 | $targetFile = array_pop($targetDirs); | ||
302 | |||
303 | foreach ($sourceDirs as $i => $dir) { | ||
304 | if (isset($targetDirs[$i]) && $dir === $targetDirs[$i]) { | ||
305 | unset($sourceDirs[$i], $targetDirs[$i]); | ||
306 | } else { | ||
307 | break; | ||
308 | } | ||
309 | } | ||
310 | |||
311 | $targetDirs[] = $targetFile; | ||
312 | $path = str_repeat('../', count($sourceDirs)).implode('/', $targetDirs); | ||
313 | |||
314 | // A reference to the same base directory or an empty subdirectory must be prefixed with "./". | ||
315 | // This also applies to a segment with a colon character (e.g., "file:colon") that cannot be used | ||
316 | // as the first segment of a relative-path reference, as it would be mistaken for a scheme name | ||
317 | // (see http://tools.ietf.org/html/rfc3986#section-4.2). | ||
318 | return '' === $path || '/' === $path[0] | ||
319 | || false !== ($colonPos = strpos($path, ':')) && ($colonPos < ($slashPos = strpos($path, '/')) || false === $slashPos) | ||
320 | ? "./$path" : $path; | ||
321 | } | ||
322 | } | ||
diff --git a/vendor/symfony/routing/Symfony/Component/Routing/Generator/UrlGeneratorInterface.php b/vendor/symfony/routing/Symfony/Component/Routing/Generator/UrlGeneratorInterface.php new file mode 100644 index 00000000..8e3b2778 --- /dev/null +++ b/vendor/symfony/routing/Symfony/Component/Routing/Generator/UrlGeneratorInterface.php | |||
@@ -0,0 +1,87 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
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\Component\Routing\Generator; | ||
13 | |||
14 | use Symfony\Component\Routing\Exception\InvalidParameterException; | ||
15 | use Symfony\Component\Routing\Exception\MissingMandatoryParametersException; | ||
16 | use Symfony\Component\Routing\Exception\RouteNotFoundException; | ||
17 | use Symfony\Component\Routing\RequestContextAwareInterface; | ||
18 | |||
19 | /** | ||
20 | * UrlGeneratorInterface is the interface that all URL generator classes must implement. | ||
21 | * | ||
22 | * The constants in this interface define the different types of resource references that | ||
23 | * are declared in RFC 3986: http://tools.ietf.org/html/rfc3986 | ||
24 | * We are using the term "URL" instead of "URI" as this is more common in web applications | ||
25 | * and we do not need to distinguish them as the difference is mostly semantical and | ||
26 | * less technical. Generating URIs, i.e. representation-independent resource identifiers, | ||
27 | * is also possible. | ||
28 | * | ||
29 | * @author Fabien Potencier <fabien@symfony.com> | ||
30 | * @author Tobias Schultze <http://tobion.de> | ||
31 | * | ||
32 | * @api | ||
33 | */ | ||
34 | interface UrlGeneratorInterface extends RequestContextAwareInterface | ||
35 | { | ||
36 | /** | ||
37 | * Generates an absolute URL, e.g. "http://example.com/dir/file". | ||
38 | */ | ||
39 | const ABSOLUTE_URL = true; | ||
40 | |||
41 | /** | ||
42 | * Generates an absolute path, e.g. "/dir/file". | ||
43 | */ | ||
44 | const ABSOLUTE_PATH = false; | ||
45 | |||
46 | /** | ||
47 | * Generates a relative path based on the current request path, e.g. "../parent-file". | ||
48 | * @see UrlGenerator::getRelativePath() | ||
49 | */ | ||
50 | const RELATIVE_PATH = 'relative'; | ||
51 | |||
52 | /** | ||
53 | * Generates a network path, e.g. "//example.com/dir/file". | ||
54 | * Such reference reuses the current scheme but specifies the host. | ||
55 | */ | ||
56 | const NETWORK_PATH = 'network'; | ||
57 | |||
58 | /** | ||
59 | * Generates a URL or path for a specific route based on the given parameters. | ||
60 | * | ||
61 | * Parameters that reference placeholders in the route pattern will substitute them in the | ||
62 | * path or host. Extra params are added as query string to the URL. | ||
63 | * | ||
64 | * When the passed reference type cannot be generated for the route because it requires a different | ||
65 | * host or scheme than the current one, the method will return a more comprehensive reference | ||
66 | * that includes the required params. For example, when you call this method with $referenceType = ABSOLUTE_PATH | ||
67 | * but the route requires the https scheme whereas the current scheme is http, it will instead return an | ||
68 | * ABSOLUTE_URL with the https scheme and the current host. This makes sure the generated URL matches | ||
69 | * the route in any case. | ||
70 | * | ||
71 | * If there is no route with the given name, the generator must throw the RouteNotFoundException. | ||
72 | * | ||
73 | * @param string $name The name of the route | ||
74 | * @param mixed $parameters An array of parameters | ||
75 | * @param Boolean|string $referenceType The type of reference to be generated (one of the constants) | ||
76 | * | ||
77 | * @return string The generated URL | ||
78 | * | ||
79 | * @throws RouteNotFoundException If the named route doesn't exist | ||
80 | * @throws MissingMandatoryParametersException When some parameters are missing that are mandatory for the route | ||
81 | * @throws InvalidParameterException When a parameter value for a placeholder is not correct because | ||
82 | * it does not match the requirement | ||
83 | * | ||
84 | * @api | ||
85 | */ | ||
86 | public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH); | ||
87 | } | ||