]> git.mxchange.org Git - friendica.git/blob - src/App/Router.php
67b1e828a0345c599015e816b5cd618d249b7fe7
[friendica.git] / src / App / Router.php
1 <?php
2
3 namespace Friendica\App;
4
5
6 use FastRoute\DataGenerator\GroupCountBased;
7 use FastRoute\Dispatcher;
8 use FastRoute\RouteCollector;
9 use FastRoute\RouteParser\Std;
10 use Friendica\Module;
11
12 /**
13  * Wrapper for FastRoute\Router
14  *
15  * This wrapper only makes use of a subset of the router features, mainly parses a route rule to return the relevant
16  * module class.
17  *
18  * Actual routes are defined in App->collectRoutes.
19  *
20  * @package Friendica\App
21  */
22 class Router
23 {
24         /** @var RouteCollector */
25         protected $routeCollector;
26
27         /**
28          * Static declaration of Friendica routes.
29          *
30          * Supports:
31          * - Route groups
32          * - Variable parts
33          * Disregards:
34          * - HTTP method other than GET
35          * - Named parameters
36          *
37          * Handler must be the name of a class extending Friendica\BaseModule.
38          *
39          * @brief Static declaration of Friendica routes.
40          */
41         public function collectRoutes()
42         {
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'],         '/nodeinfo/1.0',        Module\NodeInfo::class);
46                 $this->routeCollector->addRoute(['GET'],         '/xrd',                 Module\Xrd::class);
47                 $this->routeCollector->addGroup('/.well-known', function (RouteCollector $collector) {
48                         $collector->addRoute(['GET'], '/host-meta'       , Module\WellKnown\HostMeta::class);
49                         $collector->addRoute(['GET'], '/nodeinfo[/1.0]'  , Module\NodeInfo::class);
50                         $collector->addRoute(['GET'], '/webfinger'       , Module\WellKnown\WebFinger::class);
51                         $collector->addRoute(['GET'], '/x-social-relay'  , Module\WellKnown\XSocialRelay::class);
52                 });
53         }
54
55         public function __construct(RouteCollector $routeCollector = null)
56         {
57                 if (!$routeCollector) {
58                         $routeCollector = new RouteCollector(new Std(), new GroupCountBased());
59                 }
60
61                 $this->routeCollector = $routeCollector;
62         }
63
64         public function getRouteCollector()
65         {
66                 return $this->routeCollector;
67         }
68
69         /**
70          * Returns the relevant module class name for the given page URI or NULL if no route rule matched.
71          *
72          * @param string $cmd The path component of the request URL without the query string
73          * @return string|null A Friendica\BaseModule-extending class name if a route rule matched
74          */
75         public function getModuleClass($cmd)
76         {
77                 $cmd = '/' . ltrim($cmd, '/');
78
79                 $dispatcher = new \FastRoute\Dispatcher\GroupCountBased($this->routeCollector->getData());
80
81                 $moduleClass = null;
82
83                 // @TODO: Enable method-specific modules
84                 $httpMethod = 'GET';
85                 $routeInfo = $dispatcher->dispatch($httpMethod, $cmd);
86                 if ($routeInfo[0] === Dispatcher::FOUND) {
87                         $moduleClass = $routeInfo[1];
88                 }
89
90                 return $moduleClass;
91         }
92 }