Merge pull request #8931 from MrPetovan/task/2020-performance-improvements

Various performance improvements
This commit is contained in:
Michael Vogel 2020-07-28 07:15:02 +02:00 committed by GitHub
commit 74bc3de472
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 203 additions and 100 deletions

View File

@ -26,6 +26,8 @@ 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\Core\Cache\Duration;
use Friendica\Core\Cache\ICache;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
@ -66,14 +68,24 @@ class Router
/** @var L10n */ /** @var L10n */
private $l10n; private $l10n;
/** @var ICache */
private $cache;
/** @var string */
private $baseRoutesFilepath;
/** /**
* @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 L10n $l10n * @param L10n $l10n
* @param RouteCollector|null $routeCollector Optional the loaded Route collector * @param ICache $cache
* @param RouteCollector|null $routeCollector
*/ */
public function __construct(array $server, L10n $l10n, RouteCollector $routeCollector = null) public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICache $cache, RouteCollector $routeCollector = null)
{ {
$this->baseRoutesFilepath = $baseRoutesFilepath;
$this->l10n = $l10n; $this->l10n = $l10n;
$this->cache = $cache;
$httpMethod = $server['REQUEST_METHOD'] ?? self::GET; $httpMethod = $server['REQUEST_METHOD'] ?? self::GET;
$this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET; $this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET;
@ -84,6 +96,9 @@ class Router
} }
/** /**
* This will be called either automatically if a base routes file path was submitted,
* or can be called manually with a custom route array.
*
* @param array $routes The routes to add to the Router * @param array $routes The routes to add to the Router
* *
* @return self The router instance with the loaded routes * @return self The router instance with the loaded routes
@ -100,6 +115,9 @@ class Router
$this->routeCollector = $routeCollector; $this->routeCollector = $routeCollector;
// Add routes from addons
Hook::callAll('route_collection', $this->routeCollector);
return $this; return $this;
} }
@ -191,12 +209,9 @@ class Router
*/ */
public function getModuleClass($cmd) public function getModuleClass($cmd)
{ {
// Add routes from addons
Hook::callAll('route_collection', $this->routeCollector);
$cmd = '/' . ltrim($cmd, '/'); $cmd = '/' . ltrim($cmd, '/');
$dispatcher = new Dispatcher\GroupCountBased($this->routeCollector->getData()); $dispatcher = new Dispatcher\GroupCountBased($this->getCachedDispatchData());
$moduleClass = null; $moduleClass = null;
$this->parameters = []; $this->parameters = [];
@ -223,4 +238,51 @@ class Router
{ {
return $this->parameters; return $this->parameters;
} }
/**
* If a base routes file path has been provided, we can load routes from it if the cache misses.
*
* @return array
* @throws HTTPException\InternalServerErrorException
*/
private function getDispatchData()
{
$dispatchData = [];
if ($this->baseRoutesFilepath && file_exists($this->baseRoutesFilepath)) {
$dispatchData = require $this->baseRoutesFilepath;
if (!is_array($dispatchData)) {
throw new HTTPException\InternalServerErrorException('Invalid base routes file');
}
}
$this->loadRoutes($dispatchData);
return $this->routeCollector->getData();
}
/**
* We cache the dispatch data for speed, as computing the current routes (version 2020.09)
* takes about 850ms for each requests.
*
* The cached "routerDispatchData" lasts for a day, and must be cleared manually when there
* is any changes in the enabled addons list.
*
* @return array|mixed
* @throws HTTPException\InternalServerErrorException
*/
private function getCachedDispatchData()
{
$routerDispatchData = $this->cache->get('routerDispatchData');
if ($routerDispatchData) {
return $routerDispatchData;
}
$routerDispatchData = $this->getDispatchData();
$this->cache->set('routerDispatchData', $routerDispatchData, Duration::DAY);
return $routerDispatchData;
}
} }

View File

@ -1098,7 +1098,7 @@ class BBCode
@curl_exec($ch); @curl_exec($ch);
$curl_info = @curl_getinfo($ch); $curl_info = @curl_getinfo($ch);
DI::profiler()->saveTimestamp($stamp1, "network", System::callstack()); DI::profiler()->saveTimestamp($stamp1, "network");
if (substr($curl_info['content_type'], 0, 6) == 'image/') { if (substr($curl_info['content_type'], 0, 6) == 'image/') {
$text = "[url=" . $match[1] . ']' . $match[1] . "[/url]"; $text = "[url=" . $match[1] . ']' . $match[1] . "[/url]";
@ -1172,7 +1172,7 @@ class BBCode
@curl_exec($ch); @curl_exec($ch);
$curl_info = @curl_getinfo($ch); $curl_info = @curl_getinfo($ch);
DI::profiler()->saveTimestamp($stamp1, "network", System::callstack()); DI::profiler()->saveTimestamp($stamp1, "network");
// if its a link to a picture then embed this picture // if its a link to a picture then embed this picture
if (substr($curl_info['content_type'], 0, 6) == 'image/') { if (substr($curl_info['content_type'], 0, 6) == 'image/') {
@ -2045,7 +2045,7 @@ class BBCode
// Now convert HTML to Markdown // Now convert HTML to Markdown
$text = HTML::toMarkdown($text); $text = HTML::toMarkdown($text);
DI::profiler()->saveTimestamp($stamp1, "parser", System::callstack()); DI::profiler()->saveTimestamp($stamp1, "parser");
// Libertree has a problem with escaped hashtags. // Libertree has a problem with escaped hashtags.
$text = str_replace(['\#'], ['#'], $text); $text = str_replace(['\#'], ['#'], $text);

View File

@ -57,7 +57,7 @@ class Markdown
$html = $MarkdownParser->transform($text); $html = $MarkdownParser->transform($text);
DI::profiler()->saveTimestamp($stamp1, "parser", System::callstack()); DI::profiler()->saveTimestamp($stamp1, "parser");
return $html; return $html;
} }

View File

@ -136,7 +136,7 @@ class Addon
$func(); $func();
} }
DBA::delete('hook', ['file' => 'addon/' . $addon . '/' . $addon . '.php']); Hook::delete(['file' => 'addon/' . $addon . '/' . $addon . '.php']);
unset(self::$addons[array_search($addon, self::$addons)]); unset(self::$addons[array_search($addon, self::$addons)]);
} }
@ -204,17 +204,9 @@ class Addon
} }
Logger::notice("Addon {addon}: {action}", ['action' => 'reload', 'addon' => $addon['name']]); Logger::notice("Addon {addon}: {action}", ['action' => 'reload', 'addon' => $addon['name']]);
@include_once($fname);
if (function_exists($addonname . '_uninstall')) { self::uninstall($fname);
$func = $addonname . '_uninstall'; self::install($fname);
$func(DI::app());
}
if (function_exists($addonname . '_install')) {
$func = $addonname . '_install';
$func(DI::app());
}
DBA::update('addon', ['timestamp' => $t], ['id' => $addon['id']]);
} }
} }
@ -256,7 +248,7 @@ class Addon
$stamp1 = microtime(true); $stamp1 = microtime(true);
$f = file_get_contents("addon/$addon/$addon.php"); $f = file_get_contents("addon/$addon/$addon.php");
DI::profiler()->saveTimestamp($stamp1, "file", System::callstack()); DI::profiler()->saveTimestamp($stamp1, "file");
$r = preg_match("|/\*.*\*/|msU", $f, $m); $r = preg_match("|/\*.*\*/|msU", $f, $m);

View File

@ -56,7 +56,7 @@ class ProfilerCache implements ICache, IMemoryCache
$return = $this->cache->getAllKeys($prefix); $return = $this->cache->getAllKeys($prefix);
$this->profiler->saveTimestamp($time, 'cache', System::callstack()); $this->profiler->saveTimestamp($time, 'cache');
return $return; return $return;
} }
@ -70,7 +70,7 @@ class ProfilerCache implements ICache, IMemoryCache
$return = $this->cache->get($key); $return = $this->cache->get($key);
$this->profiler->saveTimestamp($time, 'cache', System::callstack()); $this->profiler->saveTimestamp($time, 'cache');
return $return; return $return;
} }
@ -84,7 +84,7 @@ class ProfilerCache implements ICache, IMemoryCache
$return = $this->cache->set($key, $value, $ttl); $return = $this->cache->set($key, $value, $ttl);
$this->profiler->saveTimestamp($time, 'cache', System::callstack()); $this->profiler->saveTimestamp($time, 'cache');
return $return; return $return;
} }
@ -98,7 +98,7 @@ class ProfilerCache implements ICache, IMemoryCache
$return = $this->cache->delete($key); $return = $this->cache->delete($key);
$this->profiler->saveTimestamp($time, 'cache', System::callstack()); $this->profiler->saveTimestamp($time, 'cache');
return $return; return $return;
} }
@ -112,7 +112,7 @@ class ProfilerCache implements ICache, IMemoryCache
$return = $this->cache->clear($outdated); $return = $this->cache->clear($outdated);
$this->profiler->saveTimestamp($time, 'cache', System::callstack()); $this->profiler->saveTimestamp($time, 'cache');
return $return; return $return;
} }
@ -127,7 +127,7 @@ class ProfilerCache implements ICache, IMemoryCache
$return = $this->cache->add($key, $value, $ttl); $return = $this->cache->add($key, $value, $ttl);
$this->profiler->saveTimestamp($time, 'cache', System::callstack()); $this->profiler->saveTimestamp($time, 'cache');
return $return; return $return;
} else { } else {
@ -145,7 +145,7 @@ class ProfilerCache implements ICache, IMemoryCache
$return = $this->cache->compareSet($key, $oldValue, $newValue, $ttl); $return = $this->cache->compareSet($key, $oldValue, $newValue, $ttl);
$this->profiler->saveTimestamp($time, 'cache', System::callstack()); $this->profiler->saveTimestamp($time, 'cache');
return $return; return $return;
} else { } else {
@ -163,7 +163,7 @@ class ProfilerCache implements ICache, IMemoryCache
$return = $this->cache->compareDelete($key, $value); $return = $this->cache->compareDelete($key, $value);
$this->profiler->saveTimestamp($time, 'cache', System::callstack()); $this->profiler->saveTimestamp($time, 'cache');
return $return; return $return;
} else { } else {

View File

@ -99,9 +99,7 @@ class Hook
return true; return true;
} }
$result = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]); return self::insert(['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
return $result;
} }
/** /**
@ -119,10 +117,10 @@ class Hook
// This here is only needed for fixing a problem that existed on the develop branch // This here is only needed for fixing a problem that existed on the develop branch
$condition = ['hook' => $hook, 'file' => $file, 'function' => $function]; $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
DBA::delete('hook', $condition); self::delete($condition);
$condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function]; $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
$result = DBA::delete('hook', $condition); $result = self::delete($condition);
return $result; return $result;
} }
@ -220,7 +218,7 @@ class Hook
} else { } else {
// remove orphan hooks // remove orphan hooks
$condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]]; $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
DBA::delete('hook', $condition, ['cascade' => false]); self::delete($condition, ['cascade' => false]);
} }
} }
@ -245,4 +243,45 @@ class Hook
return false; return false;
} }
/**
* Deletes one or more hook records
*
* We have to clear the cached routerDispatchData because addons can provide routes
*
* @param array $condition
* @param array $options
* @return bool
* @throws \Exception
*/
public static function delete(array $condition, array $options = [])
{
$result = DBA::delete('hook', $condition, $options);
if ($result) {
DI::cache()->delete('routerDispatchData');
}
return $result;
}
/**
* Inserts a hook record
*
* We have to clear the cached routerDispatchData because addons can provide routes
*
* @param array $condition
* @return bool
* @throws \Exception
*/
private static function insert(array $condition)
{
$result = DBA::insert('hook', $condition);
if ($result) {
DI::cache()->delete('routerDispatchData');
}
return $result;
}
} }

View File

@ -92,7 +92,7 @@ class Renderer
throw new InternalServerErrorException($message); throw new InternalServerErrorException($message);
} }
DI::profiler()->saveTimestamp($stamp1, "rendering", System::callstack()); DI::profiler()->saveTimestamp($stamp1, "rendering");
return $output; return $output;
} }
@ -121,7 +121,7 @@ class Renderer
throw new InternalServerErrorException($message); throw new InternalServerErrorException($message);
} }
DI::profiler()->saveTimestamp($stamp1, "file", System::callstack()); DI::profiler()->saveTimestamp($stamp1, "file");
return $template; return $template;
} }

View File

@ -33,16 +33,17 @@ class System
/** /**
* Returns a string with a callstack. Can be used for logging. * Returns a string with a callstack. Can be used for logging.
* *
* @param integer $depth optional, default 4 * @param integer $depth How many calls to include in the stacks after filtering
* @param int $offset How many calls to shave off the top of the stack, for example if
* this is called from a centralized method that isn't relevant to the callstack
* @return string * @return string
*/ */
public static function callstack($depth = 4) public static function callstack(int $depth = 4, int $offset = 0)
{ {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
// We remove the first two items from the list since they contain data that we don't need. // We remove at least the first two items from the list since they contain data that we don't need.
array_shift($trace); $trace = array_slice($trace, 2 + $offset);
array_shift($trace);
$callstack = []; $callstack = [];
$previous = ['class' => '', 'function' => '', 'database' => false]; $previous = ['class' => '', 'function' => '', 'database' => false];

View File

@ -90,7 +90,7 @@ class Theme
$stamp1 = microtime(true); $stamp1 = microtime(true);
$theme_file = file_get_contents("view/theme/$theme/theme.php"); $theme_file = file_get_contents("view/theme/$theme/theme.php");
DI::profiler()->saveTimestamp($stamp1, "file", System::callstack()); DI::profiler()->saveTimestamp($stamp1, "file");
$result = preg_match("|/\*.*\*/|msU", $theme_file, $matches); $result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);
@ -158,6 +158,8 @@ class Theme
if (function_exists($func)) { if (function_exists($func)) {
$func(); $func();
} }
Hook::delete(['file' => "view/theme/$theme/theme.php"]);
} }
$allowed_themes = Theme::getAllowedList(); $allowed_themes = Theme::getAllowedList();

View File

@ -699,7 +699,7 @@ class Database
$this->errorno = $errorno; $this->errorno = $errorno;
} }
$this->profiler->saveTimestamp($stamp1, 'database', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'database');
if ($this->configCache->get('system', 'db_log')) { if ($this->configCache->get('system', 'db_log')) {
$stamp2 = microtime(true); $stamp2 = microtime(true);
@ -783,7 +783,7 @@ class Database
$this->errorno = $errorno; $this->errorno = $errorno;
} }
$this->profiler->saveTimestamp($stamp, "database_write", System::callstack()); $this->profiler->saveTimestamp($stamp, "database_write");
return $retval; return $retval;
} }
@ -964,7 +964,7 @@ class Database
} }
} }
$this->profiler->saveTimestamp($stamp1, 'database', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'database');
return $columns; return $columns;
} }
@ -1644,7 +1644,7 @@ class Database
break; break;
} }
$this->profiler->saveTimestamp($stamp1, 'database', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'database');
return $ret; return $ret;
} }

View File

@ -85,7 +85,7 @@ class SessionFactory
$session = new Session\Native($baseURL, $handler); $session = new Session\Native($baseURL, $handler);
} }
} finally { } finally {
$profiler->saveTimestamp($stamp1, 'parser', System::callstack()); $profiler->saveTimestamp($stamp1, 'parser');
return $session; return $session;
} }
} }

View File

@ -199,7 +199,7 @@ class HTTPRequest implements IHTTPRequest
@curl_close($ch); @curl_close($ch);
$this->profiler->saveTimestamp($stamp1, 'network', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'network');
return $curlResponse; return $curlResponse;
} }
@ -285,7 +285,7 @@ class HTTPRequest implements IHTTPRequest
curl_close($ch); curl_close($ch);
$this->profiler->saveTimestamp($stamp1, 'network', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'network');
// Very old versions of Lighttpd don't like the "Expect" header, so we remove it when needed // Very old versions of Lighttpd don't like the "Expect" header, so we remove it when needed
if ($curlResponse->getReturnCode() == 417) { if ($curlResponse->getReturnCode() == 417) {
@ -335,7 +335,7 @@ class HTTPRequest implements IHTTPRequest
$http_code = $curl_info['http_code']; $http_code = $curl_info['http_code'];
curl_close($ch); curl_close($ch);
$this->profiler->saveTimestamp($stamp1, "network", System::callstack()); $this->profiler->saveTimestamp($stamp1, "network");
if ($http_code == 0) { if ($http_code == 0) {
return $url; return $url;
@ -377,7 +377,7 @@ class HTTPRequest implements IHTTPRequest
$body = curl_exec($ch); $body = curl_exec($ch);
curl_close($ch); curl_close($ch);
$this->profiler->saveTimestamp($stamp1, "network", System::callstack()); $this->profiler->saveTimestamp($stamp1, "network");
if (trim($body) == "") { if (trim($body) == "") {
return $url; return $url;

View File

@ -625,7 +625,7 @@ class Image
$stamp1 = microtime(true); $stamp1 = microtime(true);
file_put_contents($path, $string); file_put_contents($path, $string);
DI::profiler()->saveTimestamp($stamp1, "file", System::callstack()); DI::profiler()->saveTimestamp($stamp1, "file");
} }
/** /**

View File

@ -200,7 +200,7 @@ class Images
$stamp1 = microtime(true); $stamp1 = microtime(true);
file_put_contents($tempfile, $img_str); file_put_contents($tempfile, $img_str);
DI::profiler()->saveTimestamp($stamp1, "file", System::callstack()); DI::profiler()->saveTimestamp($stamp1, "file");
$data = getimagesize($tempfile); $data = getimagesize($tempfile);
unlink($tempfile); unlink($tempfile);

View File

@ -61,7 +61,7 @@ class ProfilerLogger implements LoggerInterface
{ {
$stamp1 = microtime(true); $stamp1 = microtime(true);
$this->logger->emergency($message, $context); $this->logger->emergency($message, $context);
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'file');
} }
/** /**
@ -71,7 +71,7 @@ class ProfilerLogger implements LoggerInterface
{ {
$stamp1 = microtime(true); $stamp1 = microtime(true);
$this->logger->alert($message, $context); $this->logger->alert($message, $context);
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'file');
} }
/** /**
@ -81,7 +81,7 @@ class ProfilerLogger implements LoggerInterface
{ {
$stamp1 = microtime(true); $stamp1 = microtime(true);
$this->logger->critical($message, $context); $this->logger->critical($message, $context);
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'file');
} }
/** /**
@ -91,7 +91,7 @@ class ProfilerLogger implements LoggerInterface
{ {
$stamp1 = microtime(true); $stamp1 = microtime(true);
$this->logger->error($message, $context); $this->logger->error($message, $context);
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'file');
} }
/** /**
@ -101,7 +101,7 @@ class ProfilerLogger implements LoggerInterface
{ {
$stamp1 = microtime(true); $stamp1 = microtime(true);
$this->logger->warning($message, $context); $this->logger->warning($message, $context);
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'file');
} }
/** /**
@ -111,7 +111,7 @@ class ProfilerLogger implements LoggerInterface
{ {
$stamp1 = microtime(true); $stamp1 = microtime(true);
$this->logger->notice($message, $context); $this->logger->notice($message, $context);
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'file');
} }
/** /**
@ -121,7 +121,7 @@ class ProfilerLogger implements LoggerInterface
{ {
$stamp1 = microtime(true); $stamp1 = microtime(true);
$this->logger->info($message, $context); $this->logger->info($message, $context);
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'file');
} }
/** /**
@ -131,7 +131,7 @@ class ProfilerLogger implements LoggerInterface
{ {
$stamp1 = microtime(true); $stamp1 = microtime(true);
$this->logger->debug($message, $context); $this->logger->debug($message, $context);
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'file');
} }
/** /**
@ -141,6 +141,6 @@ class ProfilerLogger implements LoggerInterface
{ {
$stamp1 = microtime(true); $stamp1 = microtime(true);
$this->logger->log($level, $message, $context); $this->logger->log($level, $message, $context);
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'file');
} }
} }

View File

@ -23,6 +23,7 @@ namespace Friendica\Util;
use Friendica\Core\Config\Cache; use Friendica\Core\Config\Cache;
use Friendica\Core\Config\IConfig; use Friendica\Core\Config\IConfig;
use Friendica\Core\System;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
@ -90,7 +91,7 @@ class Profiler implements ContainerInterface
* *
* @param int $timestamp the Timestamp * @param int $timestamp the Timestamp
* @param string $value A value to profile * @param string $value A value to profile
* @param string $callstack The callstack of the current profiling data * @param string $callstack A callstack string, generated if absent
*/ */
public function saveTimestamp($timestamp, $value, $callstack = '') public function saveTimestamp($timestamp, $value, $callstack = '')
{ {
@ -98,6 +99,8 @@ class Profiler implements ContainerInterface
return; return;
} }
$callstack = $callstack ?: System::callstack(4, 1);
$duration = floatval(microtime(true) - $timestamp); $duration = floatval(microtime(true) - $timestamp);
if (!isset($this->performance[$value])) { if (!isset($this->performance[$value])) {

View File

@ -191,10 +191,9 @@ return [
], ],
App\Router::class => [ App\Router::class => [
'constructParams' => [ 'constructParams' => [
$_SERVER, null $_SERVER,
], __DIR__ . '/routes.config.php',
'call' => [ null
['loadRoutes', [include __DIR__ . '/routes.config.php'], Dice::CHAIN_CALL],
], ],
], ],
L10n::class => [ L10n::class => [

View File

@ -22,6 +22,7 @@
namespace Friendica\Test\src\App; namespace Friendica\Test\src\App;
use Friendica\App; use Friendica\App;
use Friendica\Core\Cache\ICache;
use Friendica\Core\Config\IConfig; use Friendica\Core\Config\IConfig;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\LegacyModule; use Friendica\LegacyModule;
@ -175,7 +176,11 @@ class ModuleTest extends DatabaseTest
$l10n = \Mockery::mock(L10n::class); $l10n = \Mockery::mock(L10n::class);
$l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; }); $l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
$router = (new App\Router([], $l10n))->loadRoutes(include __DIR__ . '/../../../static/routes.config.php'); $cache = \Mockery::mock(ICache::class);
$cache->shouldReceive('get')->with('routerDispatchData')->andReturn('')->atMost()->once();
$cache->shouldReceive('set')->withAnyArgs()->andReturn(false)->atMost()->once();
$router = (new App\Router([], __DIR__ . '/../../../static/routes.config.php', $l10n, $cache));
$module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config); $module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config);

View File

@ -22,6 +22,7 @@
namespace Friendica\Test\src\App; namespace Friendica\Test\src\App;
use Friendica\App\Router; use Friendica\App\Router;
use Friendica\Core\Cache\ICache;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Module; use Friendica\Module;
use Friendica\Network\HTTPException\MethodNotAllowedException; use Friendica\Network\HTTPException\MethodNotAllowedException;
@ -33,6 +34,10 @@ class RouterTest extends TestCase
{ {
/** @var L10n|MockInterface */ /** @var L10n|MockInterface */
private $l10n; private $l10n;
/**
* @var ICache
*/
private $cache;
protected function setUp() protected function setUp()
{ {
@ -40,11 +45,15 @@ class RouterTest extends TestCase
$this->l10n = \Mockery::mock(L10n::class); $this->l10n = \Mockery::mock(L10n::class);
$this->l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; }); $this->l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
$this->cache = \Mockery::mock(ICache::class);
$this->cache->shouldReceive('get')->andReturn(null);
$this->cache->shouldReceive('set')->andReturn(false);
} }
public function testGetModuleClass() public function testGetModuleClass()
{ {
$router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n); $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/', 'IndexModuleClassName'); $routeCollector->addRoute([Router::GET], '/', 'IndexModuleClassName');
@ -68,7 +77,7 @@ class RouterTest extends TestCase
public function testPostModuleClass() public function testPostModuleClass()
{ {
$router = new Router(['REQUEST_METHOD' => Router::POST], $this->l10n); $router = new Router(['REQUEST_METHOD' => Router::POST], '', $this->l10n, $this->cache);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::POST], '/', 'IndexModuleClassName'); $routeCollector->addRoute([Router::POST], '/', 'IndexModuleClassName');
@ -94,7 +103,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(NotFoundException::class); $this->expectException(NotFoundException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n); $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache);
$router->getModuleClass('/unsupported'); $router->getModuleClass('/unsupported');
} }
@ -103,7 +112,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(NotFoundException::class); $this->expectException(NotFoundException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n); $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName'); $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
@ -115,7 +124,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(NotFoundException::class); $this->expectException(NotFoundException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n); $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName'); $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName');
@ -127,7 +136,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(NotFoundException::class); $this->expectException(NotFoundException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n); $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName'); $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName');
@ -139,7 +148,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(MethodNotAllowedException::class); $this->expectException(MethodNotAllowedException::class);
$router = new Router(['REQUEST_METHOD' => Router::POST], $this->l10n); $router = new Router(['REQUEST_METHOD' => Router::POST], '', $this->l10n, $this->cache);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName'); $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
@ -151,7 +160,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(MethodNotAllowedException::class); $this->expectException(MethodNotAllowedException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n); $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName'); $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName');
@ -189,9 +198,12 @@ class RouterTest extends TestCase
*/ */
public function testGetRoutes(array $routes) public function testGetRoutes(array $routes)
{ {
$router = (new Router([ $router = (new Router(
'REQUEST_METHOD' => Router::GET ['REQUEST_METHOD' => Router::GET],
], $this->l10n))->loadRoutes($routes); '',
$this->l10n,
$this->cache
))->loadRoutes($routes);
$this->assertEquals(Module\Home::class, $router->getModuleClass('/')); $this->assertEquals(Module\Home::class, $router->getModuleClass('/'));
$this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route')); $this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route'));
@ -206,7 +218,7 @@ class RouterTest extends TestCase
{ {
$router = (new Router([ $router = (new Router([
'REQUEST_METHOD' => Router::POST 'REQUEST_METHOD' => Router::POST
], $this->l10n))->loadRoutes($routes); ], '', $this->l10n, $this->cache))->loadRoutes($routes);
// Don't find GET // Don't find GET
$this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it')); $this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it'));

View File

@ -58,7 +58,7 @@ class ProfilerLoggerTest extends MockedTest
$logger = new ProfilerLogger($this->logger, $this->profiler); $logger = new ProfilerLogger($this->logger, $this->profiler);
$this->logger->shouldReceive($function)->with($message, $context)->once(); $this->logger->shouldReceive($function)->with($message, $context)->once();
$this->profiler->shouldReceive('saveTimestamp')->with(\Mockery::any(), 'file', \Mockery::any())->once(); $this->profiler->shouldReceive('saveTimestamp')->with(\Mockery::any(), 'file')->once();
$logger->$function($message, $context); $logger->$function($message, $context);
} }
@ -70,7 +70,7 @@ class ProfilerLoggerTest extends MockedTest
$logger = new ProfilerLogger($this->logger, $this->profiler); $logger = new ProfilerLogger($this->logger, $this->profiler);
$this->logger->shouldReceive('log')->with(LogLevel::WARNING, 'test', ['a' => 'context'])->once(); $this->logger->shouldReceive('log')->with(LogLevel::WARNING, 'test', ['a' => 'context'])->once();
$this->profiler->shouldReceive('saveTimestamp')->with(\Mockery::any(), 'file', \Mockery::any())->once(); $this->profiler->shouldReceive('saveTimestamp')->with(\Mockery::any(), 'file')->once();
$logger->log(LogLevel::WARNING, 'test', ['a' => 'context']); $logger->log(LogLevel::WARNING, 'test', ['a' => 'context']);
} }

View File

@ -54,18 +54,6 @@ function frio_install()
Logger::log('installed theme frio'); Logger::log('installed theme frio');
} }
function frio_uninstall()
{
Hook::unregister('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
Hook::unregister('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
Hook::unregister('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
Hook::unregister('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
Hook::unregister('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
Hook::unregister('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
Logger::log('uninstalled theme frio');
}
/** /**
* Replace friendica photo links hook * Replace friendica photo links hook
* *