aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/routing/router.php
blob: f19886fb0b458dd721262c42c3d080613f545284 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license       GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\routing;

use phpbb\routing\resources_locator\resources_locator_interface;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;

/**
 * Integration of all pieces of the routing system for easier use.
 */
class router implements RouterInterface
{
	/**
	 * @var ContainerInterface
	 */
	protected $container;

	/**
	 * @var resources_locator_interface
	 */
	protected $resources_locator;

	/**
	 * @var LoaderInterface
	 */
	protected $loader;

	/**
	 * PHP file extensions
	 *
	 * @var string
	 */
	protected $php_ext;

	/**
	 * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface|null
	 */
	protected $matcher;

	/**
	 * @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface|null
	 */
	protected $generator;

	/**
	 * @var RequestContext
	 */
	protected $context;

	/**
	 * @var RouteCollection
	 */
	protected $route_collection;

	/**
	 * @var string
	 */
	protected $cache_dir;

	/**
	 * Construct method
	 *
	 * @param ContainerInterface			$container			DI container
	 * @param resources_locator_interface	$resources_locator	Resources locator
	 * @param LoaderInterface				$loader				Resources loader
	 * @param string						$php_ext			PHP file extension
	 * @param string						$cache_dir			phpBB cache directory
	 */
	public function __construct(ContainerInterface $container, resources_locator_interface $resources_locator, LoaderInterface $loader, $php_ext, $cache_dir)
	{
		$this->container			= $container;
		$this->resources_locator	= $resources_locator;
		$this->loader				= $loader;
		$this->php_ext				= $php_ext;
		$this->context				= new RequestContext();
		$this->cache_dir			= $cache_dir;
	}

	/**
	 * Get the list of routes
	 *
	 * @return RouteCollection Get the route collection
	 */
	public function get_routes()
	{
		if ($this->route_collection === null /*|| $this->route_collection->count() === 0*/)
		{
			$this->route_collection = new RouteCollection;
			foreach ($this->resources_locator->locate_resources() as $resource)
			{
				if (is_array($resource))
				{
					$this->route_collection->addCollection($this->loader->load($resource[0], $resource[1]));
				}
				else
				{
					$this->route_collection->addCollection($this->loader->load($resource));
				}
			}

			$this->resolveParameters($this->route_collection);
		}

		return $this->route_collection;
	}

	/**
	 * {@inheritdoc}
	 */
	public function getRouteCollection()
	{
		return $this->get_routes();
	}

	/**
	 * {@inheritdoc}
	 */
	public function setContext(RequestContext $context)
	{
		$this->context = $context;

		if ($this->matcher !== null)
		{
			$this->get_matcher()->setContext($context);
		}
		if ($this->generator !== null)
		{
			$this->get_generator()->setContext($context);
		}
	}

	/**
	 * {@inheritdoc}
	 */
	public function getContext()
	{
		return $this->context;
	}

	/**
	 * {@inheritdoc}
	 */
	public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
	{
		return $this->get_generator()->generate($name, $parameters, $referenceType);
	}

	/**
	 * {@inheritdoc}
	 */
	public function match($pathinfo)
	{
		return $this->get_matcher()->match($pathinfo);
	}

	/**
	 * Gets the UrlMatcher instance associated with this Router.
	 *
	 * @return \Symfony\Component\Routing\Matcher\UrlMatcherInterface A UrlMatcherInterface instance
	 */
	public function get_matcher()
	{
		if ($this->matcher !== null)
		{
			return $this->matcher;
		}

		$this->create_dumped_url_matcher();

		return $this->matcher;
	}

	/**
	 * Creates a new dumped URL Matcher (dump it if necessary)
	 */
	protected function create_dumped_url_matcher()
	{
		try
		{
			$cache = new ConfigCache("{$this->cache_dir}url_matcher.{$this->php_ext}", defined('DEBUG'));
			if (!$cache->isFresh())
			{
				$dumper = new PhpMatcherDumper($this->get_routes());

				$options = array(
					'class'      => 'phpbb_url_matcher',
					'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
				);

				$cache->write($dumper->dump($options), $this->get_routes()->getResources());
			}

			require_once($cache->getPath());

			$this->matcher = new \phpbb_url_matcher($this->context);
		}
		catch (IOException $e)
		{
			$this->create_new_url_matcher();
		}
	}

	/**
	 * Creates a new URL Matcher
	 */
	protected function create_new_url_matcher()
	{
		$this->matcher = new UrlMatcher($this->get_routes(), $this->context);
	}

	/**
	 * Gets the UrlGenerator instance associated with this Router.
	 *
	 * @return \Symfony\Component\Routing\Generator\UrlGeneratorInterface A UrlGeneratorInterface instance
	 */
	public function get_generator()
	{
		if ($this->generator !== null)
		{
			return $this->generator;
		}

		$this->create_dumped_url_generator();

		return $this->generator;
	}

	/**
	 * Creates a new dumped URL Generator (dump it if necessary)
	 */
	protected function create_dumped_url_generator()
	{
		try
		{
			$cache = new ConfigCache("{$this->cache_dir}url_generator.{$this->php_ext}", defined('DEBUG'));
			if (!$cache->isFresh())
			{
				$dumper = new PhpGeneratorDumper($this->get_routes());

				$options = array(
					'class'      => 'phpbb_url_generator',
					'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
				);

				$cache->write($dumper->dump($options), $this->get_routes()->getResources());
			}

			require_once($cache->getPath());

			$this->generator = new \phpbb_url_generator($this->context);
		}
		catch (IOException $e)
		{
			$this->create_new_url_generator();
		}
	}

	/**
	 * Creates a new URL Generator
	 */
	protected function create_new_url_generator()
	{
		$this->generator = new UrlGenerator($this->get_routes(), $this->context);
	}

	/**
	 * Replaces placeholders with service container parameter values in:
	 * - the route defaults,
	 * - the route requirements,
	 * - the route path,
	 * - the route host,
	 * - the route schemes,
	 * - the route methods.
	 *
	 * @param RouteCollection $collection
	 */
	protected function resolveParameters(RouteCollection $collection)
	{
		/** @var \Symfony\Component\Routing\Route $route */
		foreach ($collection as $route)
		{
			foreach ($route->getDefaults() as $name => $value)
			{
				$route->setDefault($name, $this->resolve($value));
			}

			$requirements = $route->getRequirements();
			unset($requirements['_scheme']);
			unset($requirements['_method']);

			foreach ($requirements as $name => $value)
			{
				$route->setRequirement($name, $this->resolve($value));
			}

			$route->setPath($this->resolve($route->getPath()));
			$route->setHost($this->resolve($route->getHost()));

			$schemes = array();
			foreach ($route->getSchemes() as $scheme)
			{
				$schemes = array_merge($schemes, explode('|', $this->resolve($scheme)));
			}

			$route->setSchemes($schemes);
			$methods = array();
			foreach ($route->getMethods() as $method)
			{
				$methods = array_merge($methods, explode('|', $this->resolve($method)));
			}

			$route->setMethods($methods);
			$route->setCondition($this->resolve($route->getCondition()));
		}
	}

	/**
	 * Recursively replaces placeholders with the service container parameters.
	 *
	 * @param mixed $value The source which might contain "%placeholders%"
	 *
	 * @return mixed The source with the placeholders replaced by the container
	 *               parameters. Arrays are resolved recursively.
	 *
	 * @throws ParameterNotFoundException When a placeholder does not exist as a container parameter
	 * @throws RuntimeException           When a container value is not a string or a numeric value
	 */
	private function resolve($value)
	{
		if (is_array($value))
		{
			foreach ($value as $key => $val)
			{
				$value[$key] = $this->resolve($val);
			}

			return $value;
		}

		if (!is_string($value))
		{
			return $value;
		}

		$container = $this->container;
		$escapedValue = preg_replace_callback('/%%|%([^%\s]++)%/', function ($match) use ($container, $value)
		{
			// skip %%
			if (!isset($match[1]))
			{
				return '%%';
			}

			$resolved = $container->getParameter($match[1]);
			if (is_string($resolved) || is_numeric($resolved))
			{
				return (string) $resolved;
			}

			throw new RuntimeException(sprintf(
					'The container parameter "%s", used in the route configuration value "%s", '.
					'must be a string or numeric, but it is of type %s.',
					$match[1],
					$value,
					gettype($resolved)
				)
			);
		}, $value);

		return str_replace('%%', '%', $escapedValue);
	}
}