Router.php 2.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

19
namespace Q2A\Http;
20

21 22
use Q2A\Http\Exceptions\MethodNotAllowedException;

23 24
class Router
{
25
	/** @var Route[] */
Scott committed
26
	protected $routes = [];
27

28
	/** @var array */
Scott committed
29 30 31 32
	private $paramsConverter = [
		'{str}' => '([^/]+)',
		'{int}' => '([0-9]+)',
	];
33

34
	/** @var string */
Scott committed
35
	private $httpMethod = '';
36

37
	public function __construct()
38
	{
Scott committed
39
		// implicity support HEAD requests (PHP takes care of removing the response body for us)
Scott committed
40
		$method = isset($_SERVER['REQUEST_METHOD']) ? strtoupper($_SERVER['REQUEST_METHOD']) : 'GET';
Scott committed
41
		$this->httpMethod = ($method === 'HEAD' ? 'GET' : $method);
42 43
	}

Scott committed
44 45 46 47 48 49 50 51 52
	/**
	 * Add a new URI handler to the router.
	 * @param string $httpMethod
	 * @param string $routePath
	 * @param string $class The controller to use.
	 * @param string $func The class method to call.
	 * @param array $options Extra parameters e.g. Q2A template the page should use.
	 */
	public function addRoute($httpMethod, $routePath, $class, $func, array $options = [])
53
	{
Scott committed
54
		$this->routes[] = new Route($httpMethod, $routePath, $class, $func, $options);
55 56
	}

57 58 59
	/**
	 * Return the route definition that matches the given request. If none is found then null is
	 * returned.
60
	 * @throws MethodNotAllowedException
61 62 63
	 * @param string $request Request that will be looked for a match
	 * @return Route|null
	 */
64 65
	public function match($request)
	{
Scott committed
66 67
		$matchedRoute = false;

68 69
		foreach ($this->routes as $route) {
			$pathRegex = $this->buildPathRegex($route->getRoutePath());
70
			if (preg_match($pathRegex, $request, $matches)) {
Scott committed
71 72 73 74
				$matchedRoute = true;
				if ($route->getHttpMethod() === $this->httpMethod) {
					$route->setParameters(array_slice($matches, 1));
					return $route;
75
				}
76 77 78
			}
		}

Scott committed
79 80 81 82 83
		// we matched a route but not the HTTP method
		if ($matchedRoute) {
			throw new MethodNotAllowedException;
		}

84
		return null;
85
	}
86 87

	/**
88 89 90
	 * Build the final regular expression to match the request. This method replaces all the
	 * parameters' placeholders with the given regular expression.
	 * @param string $routePath Route that might have placeholders
91 92 93 94
	 * @return string
	 */
	private function buildPathRegex($routePath)
	{
95
		return '#^' . strtr($routePath, $this->paramsConverter) . '$#';
96
	}
97 98 99 100 101 102 103 104 105

	/**
	 * Return the HTTP method of the current request.
	 * @return string
	 */
	public function getHttpMethod()
	{
		return $this->httpMethod;
	}
106
}