3 namespace Friendica\App;
6 use FastRoute\DataGenerator\GroupCountBased;
7 use FastRoute\Dispatcher;
8 use FastRoute\RouteCollector;
9 use FastRoute\RouteParser\Std;
10 use Friendica\Core\Hook;
11 use Friendica\Core\L10n;
12 use Friendica\Network\HTTPException;
15 * Wrapper for FastRoute\Router
17 * This wrapper only makes use of a subset of the router features, mainly parses a route rule to return the relevant
20 * Actual routes are defined in App->collectRoutes.
22 * @package Friendica\App
29 const ALLOWED_METHODS = [
34 /** @var RouteCollector */
35 protected $routeCollector;
38 * @var string The HTTP method
43 * @var array Module parameters
45 private $parameters = [];
48 * @param array $server The $_SERVER variable
49 * @param RouteCollector|null $routeCollector Optional the loaded Route collector
51 public function __construct(array $server, RouteCollector $routeCollector = null)
53 $httpMethod = $server['REQUEST_METHOD'] ?? self::GET;
54 $this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET;
56 $this->routeCollector = isset($routeCollector) ?
58 new RouteCollector(new Std(), new GroupCountBased());
62 * @param array $routes The routes to add to the Router
64 * @return self The router instance with the loaded routes
66 * @throws HTTPException\InternalServerErrorException In case of invalid configs
68 public function loadRoutes(array $routes)
70 $routeCollector = (isset($this->routeCollector) ?
71 $this->routeCollector :
72 new RouteCollector(new Std(), new GroupCountBased()));
74 $this->addRoutes($routeCollector, $routes);
76 $this->routeCollector = $routeCollector;
81 private function addRoutes(RouteCollector $routeCollector, array $routes)
83 foreach ($routes as $route => $config) {
84 if ($this->isGroup($config)) {
85 $this->addGroup($route, $config, $routeCollector);
86 } elseif ($this->isRoute($config)) {
87 $routeCollector->addRoute($config[1], $route, $config[0]);
89 throw new HTTPException\InternalServerErrorException("Wrong route config for route '" . print_r($route, true) . "'");
95 * Adds a group of routes to a given group
97 * @param string $groupRoute The route of the group
98 * @param array $routes The routes of the group
99 * @param RouteCollector $routeCollector The route collector to add this group
101 private function addGroup(string $groupRoute, array $routes, RouteCollector $routeCollector)
103 $routeCollector->addGroup($groupRoute, function (RouteCollector $routeCollector) use ($routes) {
104 $this->addRoutes($routeCollector, $routes);
109 * Returns true in case the config is a group config
111 * @param array $config
115 private function isGroup(array $config)
119 is_string(array_keys($config)[0]) &&
120 // This entry should NOT be a BaseModule
121 (substr(array_keys($config)[0], 0, strlen('Friendica\Module')) !== 'Friendica\Module') &&
122 // The second argument is an array (another routes)
123 is_array(array_values($config)[0]);
127 * Returns true in case the config is a route config
129 * @param array $config
133 private function isRoute(array $config)
136 // The config array should at least have one entry
137 !empty($config[0]) &&
138 // This entry should be a BaseModule
139 (substr($config[0], 0, strlen('Friendica\Module')) === 'Friendica\Module') &&
140 // Either there is no other argument
141 (empty($config[1]) ||
142 // Or the second argument is an array (HTTP-Methods)
143 is_array($config[1]));
147 * The current route collector
149 * @return RouteCollector|null
151 public function getRouteCollector()
153 return $this->routeCollector;
157 * Returns the relevant module class name for the given page URI or NULL if no route rule matched.
159 * @param string $cmd The path component of the request URL without the query string
161 * @return string A Friendica\BaseModule-extending class name if a route rule matched
163 * @throws HTTPException\InternalServerErrorException
164 * @throws HTTPException\MethodNotAllowedException If a rule matched but the method didn't
165 * @throws HTTPException\NotFoundException If no rule matched
167 public function getModuleClass($cmd)
169 // Add routes from addons
170 Hook::callAll('route_collection', $this->routeCollector);
172 $cmd = '/' . ltrim($cmd, '/');
174 $dispatcher = new Dispatcher\GroupCountBased($this->routeCollector->getData());
177 $this->parameters = [];
179 $routeInfo = $dispatcher->dispatch($this->httpMethod, $cmd);
180 if ($routeInfo[0] === Dispatcher::FOUND) {
181 $moduleClass = $routeInfo[1];
182 $this->parameters = $routeInfo[2];
183 } elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
184 throw new HTTPException\MethodNotAllowedException(L10n::t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1])));
186 throw new HTTPException\NotFoundException(L10n::t('Page not found.'));
193 * Returns the module parameters.
195 * @return array parameters
197 public function getModuleParameters()
199 return $this->parameters;