Re-use fetched dependencies in lib/base.php

Reduces calls to DI container by reusing already fetched dependencies.

For status.php it went from 355 to 344.

Signed-off-by: Morris Jobke <hey@morrisjobke.de>
This commit is contained in:
Morris Jobke 2021-01-20 21:55:16 +01:00
parent 4c81f5c4ad
commit d84e1f1951
No known key found for this signature in database
GPG Key ID: FE03C3A163FEDE68
8 changed files with 45 additions and 47 deletions

View File

@ -92,7 +92,7 @@ class DeleteOrphanedSharesJobTest extends \Test\TestCase {
$userManager->createUser($this->user1, 'pass'); $userManager->createUser($this->user1, 'pass');
$userManager->createUser($this->user2, 'pass'); $userManager->createUser($this->user2, 'pass');
\OC::registerShareHooks(); \OC::registerShareHooks(\OC::$server->getSystemConfig());
$this->job = new DeleteOrphanedSharesJob(); $this->job = new DeleteOrphanedSharesJob();
} }

View File

@ -65,7 +65,7 @@ class ExpireSharesJobTest extends \Test\TestCase {
$userManager->createUser($this->user1, 'longrandompassword'); $userManager->createUser($this->user1, 'longrandompassword');
$userManager->createUser($this->user2, 'longrandompassword'); $userManager->createUser($this->user2, 'longrandompassword');
\OC::registerShareHooks(); \OC::registerShareHooks(\OC::$server->getSystemConfig());
$this->job = new ExpireSharesJob(\OC::$server->get(ITimeFactory::class), \OC::$server->get(IManager::class), $this->connection); $this->job = new ExpireSharesJob(\OC::$server->get(ITimeFactory::class), \OC::$server->get(IManager::class), $this->connection);
} }

View File

@ -73,14 +73,14 @@ abstract class TestCase extends \Test\TestCase {
parent::setUpBeforeClass(); parent::setUpBeforeClass();
new Application(); new Application();
// reset backend // reset backend
\OC_User::clearBackends(); \OC_User::clearBackends();
\OC::$server->getGroupManager()->clearBackends(); \OC::$server->getGroupManager()->clearBackends();
// clear share hooks // clear share hooks
\OC_Hook::clear('OCP\\Share'); \OC_Hook::clear('OCP\\Share');
\OC::registerShareHooks(); \OC::registerShareHooks(\OC::$server->getSystemConfig());
// create users // create users
$backend = new \Test\Util\User\Dummy(); $backend = new \Test\Util\User\Dummy();

View File

@ -72,7 +72,7 @@ class TrashbinTest extends \Test\TestCase {
// clear share hooks // clear share hooks
\OC_Hook::clear('OCP\\Share'); \OC_Hook::clear('OCP\\Share');
\OC::registerShareHooks(); \OC::registerShareHooks(\OC::$server->getSystemConfig());
// init files sharing // init files sharing
new Application(); new Application();

View File

@ -100,7 +100,7 @@ class VersioningTest extends \Test\TestCase {
// clear hooks // clear hooks
\OC_Hook::clear(); \OC_Hook::clear();
\OC::registerShareHooks(); \OC::registerShareHooks(\OC::$server->getSystemConfig());
\OCA\Files_Versions\Hooks::connectHooks(); \OCA\Files_Versions\Hooks::connectHooks();
self::loginHelper(self::TEST_VERSIONS_USER); self::loginHelper(self::TEST_VERSIONS_USER);

View File

@ -273,12 +273,12 @@ class OC {
} }
} }
public static function checkInstalled() { public static function checkInstalled(\OC\SystemConfig $systemConfig) {
if (defined('OC_CONSOLE')) { if (defined('OC_CONSOLE')) {
return; return;
} }
// Redirect to installer if not installed // Redirect to installer if not installed
if (!\OC::$server->getSystemConfig()->getValue('installed', false) && OC::$SUBURI !== '/index.php' && OC::$SUBURI !== '/status.php') { if (!$systemConfig->getValue('installed', false) && OC::$SUBURI !== '/index.php' && OC::$SUBURI !== '/status.php') {
if (OC::$CLI) { if (OC::$CLI) {
throw new Exception('Not installed'); throw new Exception('Not installed');
} else { } else {
@ -289,9 +289,9 @@ class OC {
} }
} }
public static function checkMaintenanceMode() { public static function checkMaintenanceMode(\OC\SystemConfig $systemConfig) {
// Allow ajax update script to execute without being stopped // Allow ajax update script to execute without being stopped
if (((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) && OC::$SUBURI != '/core/ajax/update.php') { if (((bool) $systemConfig->getValue('maintenance', false)) && OC::$SUBURI != '/core/ajax/update.php') {
// send http status 503 // send http status 503
http_response_code(503); http_response_code(503);
header('Retry-After: 120'); header('Retry-After: 120');
@ -503,14 +503,14 @@ class OC {
* We use an additional cookie since we want to protect logout CSRF and * We use an additional cookie since we want to protect logout CSRF and
* also we can't directly interfere with PHP's session mechanism. * also we can't directly interfere with PHP's session mechanism.
*/ */
private static function performSameSiteCookieProtection() { private static function performSameSiteCookieProtection(\OCP\IConfig $config) {
$request = \OC::$server->getRequest(); $request = \OC::$server->getRequest();
// Some user agents are notorious and don't really properly follow HTTP // Some user agents are notorious and don't really properly follow HTTP
// specifications. For those, have an automated opt-out. Since the protection // specifications. For those, have an automated opt-out. Since the protection
// for remote.php is applied in base.php as starting point we need to opt out // for remote.php is applied in base.php as starting point we need to opt out
// here. // here.
$incompatibleUserAgents = \OC::$server->getConfig()->getSystemValue('csrf.optout'); $incompatibleUserAgents = $config->getSystemValue('csrf.optout');
// Fallback, if csrf.optout is unset // Fallback, if csrf.optout is unset
if (!is_array($incompatibleUserAgents)) { if (!is_array($incompatibleUserAgents)) {
@ -541,7 +541,7 @@ class OC {
self::sendSameSiteCookies(); self::sendSameSiteCookies();
// Debug mode gets access to the resources without strict cookie // Debug mode gets access to the resources without strict cookie
// due to the fact that the SabreDAV browser also lives there. // due to the fact that the SabreDAV browser also lives there.
if (!\OC::$server->getConfig()->getSystemValue('debug', false)) { if (!$config->getSystemValue('debug', false)) {
http_response_code(\OCP\AppFramework\Http::STATUS_SERVICE_UNAVAILABLE); http_response_code(\OCP\AppFramework\Http::STATUS_SERVICE_UNAVAILABLE);
exit(); exit();
} }
@ -593,8 +593,9 @@ class OC {
// setup the basic server // setup the basic server
self::$server = new \OC\Server(\OC::$WEBROOT, self::$config); self::$server = new \OC\Server(\OC::$WEBROOT, self::$config);
self::$server->boot(); self::$server->boot();
\OC::$server->getEventLogger()->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd); $eventLogger = \OC::$server->getEventLogger();
\OC::$server->getEventLogger()->start('boot', 'Initialize'); $eventLogger->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd);
$eventLogger->start('boot', 'Initialize');
// Override php.ini and log everything if we're troubleshooting // Override php.ini and log everything if we're troubleshooting
if (self::$config->getValue('loglevel') === ILogger::DEBUG) { if (self::$config->getValue('loglevel') === ILogger::DEBUG) {
@ -622,14 +623,16 @@ class OC {
self::setRequiredIniValues(); self::setRequiredIniValues();
self::handleAuthHeaders(); self::handleAuthHeaders();
self::registerAutoloaderCache(); $systemConfig = \OC::$server->get(\OC\SystemConfig::class);
self::registerAutoloaderCache($systemConfig);
// initialize intl fallback if necessary // initialize intl fallback if necessary
OC_Util::isSetLocaleWorking(); OC_Util::isSetLocaleWorking();
$config = \OC::$server->get(\OCP\IConfig::class);
if (!defined('PHPUNIT_RUN')) { if (!defined('PHPUNIT_RUN')) {
OC\Log\ErrorHandler::setLogger(\OC::$server->getLogger()); OC\Log\ErrorHandler::setLogger(\OC::$server->getLogger());
$debug = \OC::$server->getConfig()->getSystemValue('debug', false); $debug = $config->getSystemValue('debug', false);
OC\Log\ErrorHandler::register($debug); OC\Log\ErrorHandler::register($debug);
} }
@ -637,21 +640,21 @@ class OC {
$bootstrapCoordinator = \OC::$server->query(\OC\AppFramework\Bootstrap\Coordinator::class); $bootstrapCoordinator = \OC::$server->query(\OC\AppFramework\Bootstrap\Coordinator::class);
$bootstrapCoordinator->runInitialRegistration(); $bootstrapCoordinator->runInitialRegistration();
\OC::$server->getEventLogger()->start('init_session', 'Initialize session'); $eventLogger->start('init_session', 'Initialize session');
OC_App::loadApps(['session']); OC_App::loadApps(['session']);
if (!self::$CLI) { if (!self::$CLI) {
self::initSession(); self::initSession();
} }
\OC::$server->getEventLogger()->end('init_session'); $eventLogger->end('init_session');
self::checkConfig(); self::checkConfig();
self::checkInstalled(); self::checkInstalled($systemConfig);
OC_Response::addSecurityHeaders(); OC_Response::addSecurityHeaders();
self::performSameSiteCookieProtection(); self::performSameSiteCookieProtection($config);
if (!defined('OC_CONSOLE')) { if (!defined('OC_CONSOLE')) {
$errors = OC_Util::checkServer(\OC::$server->getSystemConfig()); $errors = OC_Util::checkServer($systemConfig);
if (count($errors) > 0) { if (count($errors) > 0) {
if (!self::$CLI) { if (!self::$CLI) {
http_response_code(503); http_response_code(503);
@ -677,21 +680,19 @@ class OC {
} }
try { try {
\OC::$server->getConfig()->setAppValue('core', 'cronErrors', json_encode($staticErrors)); $config->setAppValue('core', 'cronErrors', json_encode($staticErrors));
} catch (\Exception $e) { } catch (\Exception $e) {
echo('Writing to database failed'); echo('Writing to database failed');
} }
exit(1); exit(1);
} elseif (self::$CLI && \OC::$server->getConfig()->getSystemValue('installed', false)) { } elseif (self::$CLI && $config->getSystemValue('installed', false)) {
\OC::$server->getConfig()->deleteAppValue('core', 'cronErrors'); $config->deleteAppValue('core', 'cronErrors');
} }
} }
//try to set the session lifetime //try to set the session lifetime
$sessionLifeTime = self::getSessionLifeTime(); $sessionLifeTime = self::getSessionLifeTime();
@ini_set('gc_maxlifetime', (string)$sessionLifeTime); @ini_set('gc_maxlifetime', (string)$sessionLifeTime);
$systemConfig = \OC::$server->getSystemConfig();
// User and Groups // User and Groups
if (!$systemConfig->getValue("installed", false)) { if (!$systemConfig->getValue("installed", false)) {
self::$server->getSession()->set('user_id', ''); self::$server->getSession()->set('user_id', '');
@ -716,11 +717,10 @@ class OC {
OC_User::setIncognitoMode(true); OC_User::setIncognitoMode(true);
} }
self::registerCleanupHooks(); self::registerCleanupHooks($systemConfig);
self::registerFilesystemHooks(); self::registerFilesystemHooks();
self::registerShareHooks(); self::registerShareHooks($systemConfig);
self::registerEncryptionWrapper(); self::registerEncryptionWrapperAndHooks();
self::registerEncryptionHooks();
self::registerAccountHooks(); self::registerAccountHooks();
self::registerResourceCollectionHooks(); self::registerResourceCollectionHooks();
self::registerAppRestrictionsHooks(); self::registerAppRestrictionsHooks();
@ -755,7 +755,7 @@ class OC {
*/ */
if (!OC::$CLI if (!OC::$CLI
&& !\OC::$server->getTrustedDomainHelper()->isTrustedDomain($host) && !\OC::$server->getTrustedDomainHelper()->isTrustedDomain($host)
&& self::$server->getConfig()->getSystemValue('installed', false) && $config->getSystemValue('installed', false)
) { ) {
// Allow access to CSS resources // Allow access to CSS resources
$isScssRequest = false; $isScssRequest = false;
@ -789,15 +789,15 @@ class OC {
exit(); exit();
} }
} }
\OC::$server->getEventLogger()->end('boot'); $eventLogger->end('boot');
} }
/** /**
* register hooks for the cleanup of cache and bruteforce protection * register hooks for the cleanup of cache and bruteforce protection
*/ */
public static function registerCleanupHooks() { public static function registerCleanupHooks(\OC\SystemConfig $systemConfig) {
//don't try to do this before we are properly setup //don't try to do this before we are properly setup
if (\OC::$server->getSystemConfig()->getValue('installed', false) && !\OCP\Util::needUpgrade()) { if ($systemConfig->getValue('installed', false) && !\OCP\Util::needUpgrade()) {
// NOTE: This will be replaced to use OCP // NOTE: This will be replaced to use OCP
$userSession = self::$server->getUserSession(); $userSession = self::$server->getUserSession();
@ -831,13 +831,11 @@ class OC {
} }
} }
private static function registerEncryptionWrapper() { private static function registerEncryptionWrapperAndHooks() {
$manager = self::$server->getEncryptionManager(); $manager = self::$server->getEncryptionManager();
\OCP\Util::connectHook('OC_Filesystem', 'preSetup', $manager, 'setupStorage'); \OCP\Util::connectHook('OC_Filesystem', 'preSetup', $manager, 'setupStorage');
}
private static function registerEncryptionHooks() { $enabled = $manager->isEnabled();
$enabled = self::$server->getEncryptionManager()->isEnabled();
if ($enabled) { if ($enabled) {
\OCP\Util::connectHook(Share::class, 'post_shared', HookManager::class, 'postShared'); \OCP\Util::connectHook(Share::class, 'post_shared', HookManager::class, 'postShared');
\OCP\Util::connectHook(Share::class, 'post_unshare', HookManager::class, 'postUnshared'); \OCP\Util::connectHook(Share::class, 'post_unshare', HookManager::class, 'postUnshared');
@ -890,8 +888,8 @@ class OC {
/** /**
* register hooks for sharing * register hooks for sharing
*/ */
public static function registerShareHooks() { public static function registerShareHooks(\OC\SystemConfig $systemConfig) {
if (\OC::$server->getSystemConfig()->getValue('installed')) { if ($systemConfig->getValue('installed')) {
OC_Hook::connect('OC_User', 'post_deleteUser', Hooks::class, 'post_deleteUser'); OC_Hook::connect('OC_User', 'post_deleteUser', Hooks::class, 'post_deleteUser');
OC_Hook::connect('OC_User', 'post_deleteGroup', Hooks::class, 'post_deleteGroup'); OC_Hook::connect('OC_User', 'post_deleteGroup', Hooks::class, 'post_deleteGroup');
@ -901,14 +899,14 @@ class OC {
} }
} }
protected static function registerAutoloaderCache() { protected static function registerAutoloaderCache(\OC\SystemConfig $systemConfig) {
// The class loader takes an optional low-latency cache, which MUST be // The class loader takes an optional low-latency cache, which MUST be
// namespaced. The instanceid is used for namespacing, but might be // namespaced. The instanceid is used for namespacing, but might be
// unavailable at this point. Furthermore, it might not be possible to // unavailable at this point. Furthermore, it might not be possible to
// generate an instanceid via \OC_Util::getInstanceId() because the // generate an instanceid via \OC_Util::getInstanceId() because the
// config file may not be writable. As such, we only register a class // config file may not be writable. As such, we only register a class
// loader cache if instanceid is available without trying to create one. // loader cache if instanceid is available without trying to create one.
$instanceId = \OC::$server->getSystemConfig()->getValue('instanceid', null); $instanceId = $systemConfig->getValue('instanceid', null);
if ($instanceId) { if ($instanceId) {
try { try {
$memcacheFactory = \OC::$server->getMemCacheFactory(); $memcacheFactory = \OC::$server->getMemCacheFactory();
@ -948,7 +946,7 @@ class OC {
return; return;
} }
if (substr($requestPath, -3) !== '.js') { // we need these files during the upgrade if (substr($requestPath, -3) !== '.js') { // we need these files during the upgrade
self::checkMaintenanceMode(); self::checkMaintenanceMode($systemConfig);
if (\OCP\Util::needUpgrade()) { if (\OCP\Util::needUpgrade()) {
if (function_exists('opcache_reset')) { if (function_exists('opcache_reset')) {
@ -999,7 +997,7 @@ class OC {
OC_App::loadApps(['filesystem', 'logging']); OC_App::loadApps(['filesystem', 'logging']);
OC_App::loadApps(); OC_App::loadApps();
} }
OC::$server->get(\OC\Route\Router::class)->match(\OC::$server->getRequest()->getRawPathInfo()); OC::$server->get(\OC\Route\Router::class)->match($request->getRawPathInfo());
return; return;
} catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { } catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
//header('HTTP/1.0 404 Not Found'); //header('HTTP/1.0 404 Not Found');

View File

@ -42,7 +42,7 @@ try {
exit; exit;
} }
OC::checkMaintenanceMode(); OC::checkMaintenanceMode(\OC::$server->get(\OC\SystemConfig::class));
$request = \OC::$server->getRequest(); $request = \OC::$server->getRequest();
$pathInfo = $request->getPathInfo(); $pathInfo = $request->getPathInfo();

View File

@ -101,7 +101,7 @@ class ShareTest extends \Test\TestCase {
\OC\Share\Share::registerBackend('test', 'Test\Share\Backend'); \OC\Share\Share::registerBackend('test', 'Test\Share\Backend');
\OC_Hook::clear('OCP\\Share'); \OC_Hook::clear('OCP\\Share');
\OC::registerShareHooks(); \OC::registerShareHooks(\OC::$server->getSystemConfig());
$this->resharing = \OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_resharing', 'yes'); $this->resharing = \OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_resharing', 'yes');
\OC::$server->getConfig()->setAppValue('core', 'shareapi_allow_resharing', 'yes'); \OC::$server->getConfig()->setAppValue('core', 'shareapi_allow_resharing', 'yes');