1
1
Fork 0

Add App\Router dependency injection to App

- Moved collectRoutes method from App to App\Router
This commit is contained in:
Hypolite Petovan 2019-04-05 23:16:12 -04:00
parent 7383511855
commit d6d31f43a1
6 changed files with 65 additions and 36 deletions

View file

@ -78,6 +78,11 @@ class App
*/ */
private $mode; private $mode;
/**
* @var App\Router
*/
private $router;
/** /**
* @var string The App URL path * @var string The App URL path
*/ */
@ -173,6 +178,11 @@ class App
return $this->mode; return $this->mode;
} }
public function getRouter()
{
return $this->router;
}
/** /**
* Register a stylesheet file path to be included in the <head> tag of every page. * Register a stylesheet file path to be included in the <head> tag of every page.
* Inclusion is done in App->initHead(). * Inclusion is done in App->initHead().
@ -218,20 +228,22 @@ class App
* *
* @param Configuration $config The Configuration * @param Configuration $config The Configuration
* @param App\Mode $mode The mode of this Friendica app * @param App\Mode $mode The mode of this Friendica app
* @param App\Router $router The router of this Friendica app
* @param LoggerInterface $logger The current app logger * @param LoggerInterface $logger The current app logger
* @param Profiler $profiler The profiler of this application * @param Profiler $profiler The profiler of this application
* @param bool $isBackend Whether it is used for backend or frontend (Default true=backend) * @param bool $isBackend Whether it is used for backend or frontend (Default true=backend)
* *
* @throws Exception if the Basepath is not usable * @throws Exception if the Basepath is not usable
*/ */
public function __construct(Configuration $config, App\Mode $mode, LoggerInterface $logger, Profiler $profiler, $isBackend = true) public function __construct(Configuration $config, App\Mode $mode, App\Router $router, LoggerInterface $logger, Profiler $profiler, $isBackend = true)
{ {
BaseObject::setApp($this); BaseObject::setApp($this);
$this->logger = $logger;
$this->config = $config; $this->config = $config;
$this->profiler = $profiler;
$this->mode = $mode; $this->mode = $mode;
$this->router = $router;
$this->profiler = $profiler;
$this->logger = $logger;
$this->checkBackend($isBackend); $this->checkBackend($isBackend);
$this->checkFriendicaApp(); $this->checkFriendicaApp();
@ -1248,14 +1260,24 @@ class App
$this->module = "login"; $this->module = "login";
} }
$router = new App\Router(); /*
$this->collectRoutes($router->getRouteCollector()); * ROUTING
*
* From the request URL, routing consists of obtaining the name of a BaseModule-extending class of which the
* post() and/or content() static methods can be respectively called to produce a data change or an output.
*/
$this->module_class = $router->getModuleClass($this->cmd); // First we try explicit routes defined in App\Router
$this->router->collectRoutes();
$privateapps = $this->config->get('config', 'private_addons', false); Hook::callAll('route_collection', $this->router->getRouteCollector());
$this->module_class = $this->router->getModuleClass($this->cmd);
// Then we try addon-provided modules that we wrap in the LegacyModule class
if (!$this->module_class && Core\Addon::isEnabled($this->module) && file_exists("addon/{$this->module}/{$this->module}.php")) { if (!$this->module_class && Core\Addon::isEnabled($this->module) && file_exists("addon/{$this->module}/{$this->module}.php")) {
//Check if module is an app and if public access to apps is allowed or not //Check if module is an app and if public access to apps is allowed or not
$privateapps = $this->config->get('config', 'private_addons', false);
if ((!local_user()) && Core\Hook::isAddonApp($this->module) && $privateapps) { if ((!local_user()) && Core\Hook::isAddonApp($this->module) && $privateapps) {
info(Core\L10n::t("You must be logged in to use addons. ")); info(Core\L10n::t("You must be logged in to use addons. "));
} else { } else {
@ -1267,12 +1289,12 @@ class App
} }
} }
// Controller class routing // Then we try name-matching a Friendica\Module class
if (!$this->module_class && class_exists('Friendica\\Module\\' . ucfirst($this->module))) { if (!$this->module_class && class_exists('Friendica\\Module\\' . ucfirst($this->module))) {
$this->module_class = 'Friendica\\Module\\' . ucfirst($this->module); $this->module_class = 'Friendica\\Module\\' . ucfirst($this->module);
} }
/* If not, next look for a 'standard' program module in the 'mod' directory /* Finally, we look for a 'standard' program module in the 'mod' directory
* We emulate a Module class through the LegacyModule class * We emulate a Module class through the LegacyModule class
*/ */
if (!$this->module_class && file_exists("mod/{$this->module}.php")) { if (!$this->module_class && file_exists("mod/{$this->module}.php")) {
@ -1505,27 +1527,4 @@ class App
$this->internalRedirect($toUrl); $this->internalRedirect($toUrl);
} }
} }
/**
* Static declaration of Friendica routes.
*
* Supports:
* - Route groups
* - Variable parts
* Disregards:
* - HTTP method other than GET
* - Named parameters
*
* Handler must be the name of a class extending Friendica\BaseModule.
*
* @brief Static declaration of Friendica routes.
* @param RouteCollector $routeCollector
* @throws InternalServerErrorException
*/
private function collectRoutes(RouteCollector $routeCollector)
{
$routeCollector->addRoute(['GET', 'POST'], '/itemsource[/{guid}]', Module\Itemsource::class);
Hook::callAll('route_collection', $routeCollector);
}
} }

View file

@ -7,6 +7,7 @@ use FastRoute\DataGenerator\GroupCountBased;
use FastRoute\Dispatcher; use FastRoute\Dispatcher;
use FastRoute\RouteCollector; use FastRoute\RouteCollector;
use FastRoute\RouteParser\Std; use FastRoute\RouteParser\Std;
use Friendica\Module;
/** /**
* Wrapper for FastRoute\Router * Wrapper for FastRoute\Router
@ -23,6 +24,25 @@ class Router
/** @var RouteCollector */ /** @var RouteCollector */
protected $routeCollector; protected $routeCollector;
/**
* Static declaration of Friendica routes.
*
* Supports:
* - Route groups
* - Variable parts
* Disregards:
* - HTTP method other than GET
* - Named parameters
*
* Handler must be the name of a class extending Friendica\BaseModule.
*
* @brief Static declaration of Friendica routes.
*/
public function collectRoutes()
{
$this->routeCollector->addRoute(['GET', 'POST'], '/itemsource[/{guid}]', Module\Itemsource::class);
}
public function __construct(RouteCollector $routeCollector = null) public function __construct(RouteCollector $routeCollector = null)
{ {
if (!$routeCollector) { if (!$routeCollector) {
@ -37,6 +57,12 @@ class Router
return $this->routeCollector; return $this->routeCollector;
} }
/**
* Returns the relevant module class name for the given page URI or NULL if no route rule matched.
*
* @param string $cmd The path component of the request URL without the query string
* @return string|null A Friendica\BaseModule-extending class name if a route rule matched
*/
public function getModuleClass($cmd) public function getModuleClass($cmd)
{ {
$cmd = '/' . ltrim($cmd, '/'); $cmd = '/' . ltrim($cmd, '/');

View file

@ -24,6 +24,7 @@ class DependencyFactory
{ {
$basePath = BasePath::create($directory, $_SERVER); $basePath = BasePath::create($directory, $_SERVER);
$mode = new App\Mode($basePath); $mode = new App\Mode($basePath);
$router = new App\Router();
$configLoader = new Config\ConfigFileLoader($basePath, $mode); $configLoader = new Config\ConfigFileLoader($basePath, $mode);
$configCache = Factory\ConfigFactory::createCache($configLoader); $configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache); $profiler = Factory\ProfilerFactory::create($configCache);
@ -34,6 +35,6 @@ class DependencyFactory
$logger = Factory\LoggerFactory::create($channel, $config, $profiler); $logger = Factory\LoggerFactory::create($channel, $config, $profiler);
Factory\LoggerFactory::createDev($channel, $config, $profiler); Factory\LoggerFactory::createDev($channel, $config, $profiler);
return new App($config, $mode, $logger, $profiler, $isBackend); return new App($config, $mode, $router, $logger, $profiler, $isBackend);
} }
} }

View file

@ -50,6 +50,7 @@ class ApiTest extends DatabaseTest
{ {
$basePath = BasePath::create(dirname(__DIR__) . '/../'); $basePath = BasePath::create(dirname(__DIR__) . '/../');
$mode = new App\Mode($basePath); $mode = new App\Mode($basePath);
$router = new App\Router();
$configLoader = new ConfigFileLoader($basePath, $mode); $configLoader = new ConfigFileLoader($basePath, $mode);
$configCache = Factory\ConfigFactory::createCache($configLoader); $configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache); $profiler = Factory\ProfilerFactory::create($configCache);
@ -57,7 +58,7 @@ class ApiTest extends DatabaseTest
$config = Factory\ConfigFactory::createConfig($configCache); $config = Factory\ConfigFactory::createConfig($configCache);
Factory\ConfigFactory::createPConfig($configCache); Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('test', $config, $profiler); $logger = Factory\LoggerFactory::create('test', $config, $profiler);
$this->app = new App($config, $mode, $logger, $profiler, false); $this->app = new App($config, $mode, $router, $logger, $profiler, false);
parent::setUp(); parent::setUp();

View file

@ -15,6 +15,7 @@ class DBATest extends DatabaseTest
{ {
$basePath = BasePath::create(dirname(__DIR__) . '/../../'); $basePath = BasePath::create(dirname(__DIR__) . '/../../');
$mode = new App\Mode($basePath); $mode = new App\Mode($basePath);
$router = new App\Router();
$configLoader = new ConfigFileLoader($basePath, $mode); $configLoader = new ConfigFileLoader($basePath, $mode);
$configCache = Factory\ConfigFactory::createCache($configLoader); $configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache); $profiler = Factory\ProfilerFactory::create($configCache);
@ -22,7 +23,7 @@ class DBATest extends DatabaseTest
$config = Factory\ConfigFactory::createConfig($configCache); $config = Factory\ConfigFactory::createConfig($configCache);
Factory\ConfigFactory::createPConfig($configCache); Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('test', $config, $profiler); $logger = Factory\LoggerFactory::create('test', $config, $profiler);
$this->app = new App($config, $mode, $logger, $profiler, false); $this->app = new App($config, $mode, $router, $logger, $profiler, false);
parent::setUp(); parent::setUp();

View file

@ -15,6 +15,7 @@ class DBStructureTest extends DatabaseTest
{ {
$basePath = BasePath::create(dirname(__DIR__) . '/../../'); $basePath = BasePath::create(dirname(__DIR__) . '/../../');
$mode = new App\Mode($basePath); $mode = new App\Mode($basePath);
$router = new App\Router();
$configLoader = new ConfigFileLoader($basePath, $mode); $configLoader = new ConfigFileLoader($basePath, $mode);
$configCache = Factory\ConfigFactory::createCache($configLoader); $configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache); $profiler = Factory\ProfilerFactory::create($configCache);
@ -22,7 +23,7 @@ class DBStructureTest extends DatabaseTest
$config = Factory\ConfigFactory::createConfig($configCache); $config = Factory\ConfigFactory::createConfig($configCache);
Factory\ConfigFactory::createPConfig($configCache); Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('test', $config, $profiler); $logger = Factory\LoggerFactory::create('test', $config, $profiler);
$this->app = new App($config, $mode, $logger, $profiler, false); $this->app = new App($config, $mode, $router, $logger, $profiler, false);
parent::setUp(); parent::setUp();
} }