3 namespace Friendica\App;
6 use FastRoute\DataGenerator\GroupCountBased;
7 use FastRoute\Dispatcher;
8 use FastRoute\RouteCollector;
9 use FastRoute\RouteParser\Std;
13 * Wrapper for FastRoute\Router
15 * This wrapper only makes use of a subset of the router features, mainly parses a route rule to return the relevant
18 * Actual routes are defined in App->collectRoutes.
20 * @package Friendica\App
24 /** @var RouteCollector */
25 protected $routeCollector;
28 * Static declaration of Friendica routes.
34 * - HTTP method other than GET
37 * Handler must be the name of a class extending Friendica\BaseModule.
39 * @brief Static declaration of Friendica routes.
41 public function collectRoutes()
43 $this->routeCollector->addRoute(['GET', 'POST'], '/itemsource[/{guid}]', Module\Itemsource::class);
44 $this->routeCollector->addRoute(['GET'], '/amcd', Module\AccountManagementControlDocument::class);
45 $this->routeCollector->addRoute(['GET'], '/host-meta', Module\HostMeta::class);
46 $this->routeCollector->addRoute(['GET'], '/hostxrd', Module\HostMeta::class);
47 $this->routeCollector->addRoute(['GET'], '/nodeinfo/1.0', Module\NodeInfo::class);
48 $this->routeCollector->addRoute(['GET'], '/xrd', Module\WebFinger::class);
49 $this->routeCollector->addGroup('/.well-known', function (RouteCollector $collector) {
50 $collector->addRoute(['GET'], '/host-meta' , Module\WellKnown\HostMeta::class);
51 $collector->addRoute(['GET'], '/nodeinfo[/1.0]' , Module\NodeInfo::class);
52 $collector->addRoute(['GET'], '/webfinger[/xrd]' , Module\WebFinger::class);
53 $collector->addRoute(['GET'], '/x-social-relay' , Module\WellKnown\XSocialRelay::class);
57 public function __construct(RouteCollector $routeCollector = null)
59 if (!$routeCollector) {
60 $routeCollector = new RouteCollector(new Std(), new GroupCountBased());
63 $this->routeCollector = $routeCollector;
66 public function getRouteCollector()
68 return $this->routeCollector;
72 * Returns the relevant module class name for the given page URI or NULL if no route rule matched.
74 * @param string $cmd The path component of the request URL without the query string
75 * @return string|null A Friendica\BaseModule-extending class name if a route rule matched
77 public function getModuleClass($cmd)
79 $cmd = '/' . ltrim($cmd, '/');
81 $dispatcher = new \FastRoute\Dispatcher\GroupCountBased($this->routeCollector->getData());
85 // @TODO: Enable method-specific modules
87 $routeInfo = $dispatcher->dispatch($httpMethod, $cmd);
88 if ($routeInfo[0] === Dispatcher::FOUND) {
89 $moduleClass = $routeInfo[1];