Cache the Module class

This commit is contained in:
Philipp 2022-11-27 01:44:12 +01:00
parent 29190fae73
commit 4e53ba0c20
No known key found for this signature in database
GPG key ID: 24A7501396EB5432

View file

@ -40,6 +40,7 @@ use Friendica\Module\HTTPException\MethodNotAllowed;
use Friendica\Module\HTTPException\PageNotFound; use Friendica\Module\HTTPException\PageNotFound;
use Friendica\Module\Special\Options; use Friendica\Module\Special\Options;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Network\HTTPException\MethodNotAllowedException; use Friendica\Network\HTTPException\MethodNotAllowedException;
use Friendica\Network\HTTPException\NotFoundException; use Friendica\Network\HTTPException\NotFoundException;
use Friendica\Util\Router\FriendicaGroupCountBased; use Friendica\Util\Router\FriendicaGroupCountBased;
@ -114,6 +115,9 @@ class Router
/** @var array */ /** @var array */
private $server; private $server;
/** @var string|null */
protected $moduleClass = null;
/** /**
* @param array $server The $_SERVER variable * @param array $server The $_SERVER variable
* @param string $baseRoutesFilepath The path to a base routes file to leverage cache, can be empty * @param string $baseRoutesFilepath The path to a base routes file to leverage cache, can be empty
@ -216,7 +220,7 @@ class Router
* *
* @return bool * @return bool
*/ */
private function isGroup(array $config) private function isGroup(array $config): bool
{ {
return return
is_array($config) && is_array($config) &&
@ -252,21 +256,39 @@ class Router
* *
* @return RouteCollector|null * @return RouteCollector|null
*/ */
public function getRouteCollector() public function getRouteCollector(): ?RouteCollector
{ {
return $this->routeCollector; return $this->routeCollector;
} }
/**
* Returns the Friendica\BaseModule-extending class name if a route rule matched
*
* @return string
*
* @throws InternalServerErrorException
* @throws MethodNotAllowedException
* @throws NotFoundException
*/
public function getModuleClass(): string
{
if (empty($this->moduleClass)) {
$this->determineModuleClass();
}
return $this->moduleClass;
}
/** /**
* Returns the relevant module class name for the given page URI or NULL if no route rule matched. * Returns the relevant module class name for the given page URI or NULL if no route rule matched.
* *
* @return string A Friendica\BaseModule-extending class name if a route rule matched * @return void
* *
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws HTTPException\MethodNotAllowedException If a rule matched but the method didn't * @throws HTTPException\MethodNotAllowedException If a rule matched but the method didn't
* @throws HTTPException\NotFoundException If no rule matched * @throws HTTPException\NotFoundException If no rule matched
*/ */
public function getModuleClass(): string private function determineModuleClass(): void
{ {
$cmd = $this->args->getCommand(); $cmd = $this->args->getCommand();
$cmd = '/' . ltrim($cmd, '/'); $cmd = '/' . ltrim($cmd, '/');
@ -277,21 +299,19 @@ class Router
// Check if the HTTP method is OPTIONS and return the special Options Module with the possible HTTP methods // Check if the HTTP method is OPTIONS and return the special Options Module with the possible HTTP methods
if ($this->args->getMethod() === static::OPTIONS) { if ($this->args->getMethod() === static::OPTIONS) {
$moduleClass = Options::class; $this->moduleClass = Options::class;
$this->parameters = ['allowedMethods' => $dispatcher->getOptions($cmd)]; $this->parameters = ['allowedMethods' => $dispatcher->getOptions($cmd)];
} else { } else {
$routeInfo = $dispatcher->dispatch($this->args->getMethod(), $cmd); $routeInfo = $dispatcher->dispatch($this->args->getMethod(), $cmd);
if ($routeInfo[0] === Dispatcher::FOUND) { if ($routeInfo[0] === Dispatcher::FOUND) {
$moduleClass = $routeInfo[1]; $this->moduleClass = $routeInfo[1];
$this->parameters = $routeInfo[2]; $this->parameters = $routeInfo[2];
} elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) { } elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
throw new HTTPException\MethodNotAllowedException($this->l10n->t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1]))); throw new HTTPException\MethodNotAllowedException($this->l10n->t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1])));
} else { } else {
throw new HTTPException\NotFoundException($this->l10n->t('Page not found.')); throw new HTTPException\NotFoundException($this->l10n->t('Page not found.'));
} }
} }
return $moduleClass;
} }
public function getModule(?string $module_class = null): ICanHandleRequests public function getModule(?string $module_class = null): ICanHandleRequests