Skip to content

Commit 20c6d1a

Browse files
committed
feat: Improve init a bit, and add more profiling steps
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 56897b6 commit 20c6d1a

8 files changed

Lines changed: 52 additions & 36 deletions

File tree

build/rector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
5252
$config = RectorConfig::configure()
5353
->withPaths([
5454
$nextcloudDir . '/apps',
55+
$nextcloudDir . '/status.php',
5556
// $nextcloudDir . '/config',
5657
// $nextcloudDir . '/core',
5758
// $nextcloudDir . '/lib',

lib/autoloader.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ class Autoloader {
2222

2323
/**
2424
* Optional low-latency memory cache for class to path mapping.
25-
*
26-
* @var \OC\Memcache\Cache
2725
*/
28-
protected $memoryCache;
26+
protected ?ICache $memoryCache = null;
2927

3028
/**
3129
* Autoloader constructor.
@@ -127,15 +125,15 @@ protected function isValidPath(string $fullPath): bool {
127125
* @throws AutoloadNotAllowedException
128126
*/
129127
public function load(string $class): bool {
128+
if (class_exists($class, false)) {
129+
return false;
130+
}
131+
130132
$pathsToRequire = null;
131133
if ($this->memoryCache) {
132134
$pathsToRequire = $this->memoryCache->get($class);
133135
}
134136

135-
if (class_exists($class, false)) {
136-
return false;
137-
}
138-
139137
if (!is_array($pathsToRequire)) {
140138
// No cache or cache miss
141139
$pathsToRequire = [];

lib/base.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,6 @@ public static function initPaths(): void {
188188
}
189189

190190
public static function checkConfig(): void {
191-
$l = Server::get(\OCP\L10N\IFactory::class)->get('lib');
192-
193191
// Create config if it does not already exist
194192
$configFilePath = self::$configDir . '/config.php';
195193
if (!file_exists($configFilePath)) {
@@ -201,6 +199,7 @@ public static function checkConfig(): void {
201199
if (!$configFileWritable && !OC_Helper::isReadOnlyConfigEnabled()
202200
|| !$configFileWritable && \OCP\Util::needUpgrade()) {
203201
$urlGenerator = Server::get(IURLGenerator::class);
202+
$l = Server::get(\OCP\L10N\IFactory::class)->get('lib');
204203

205204
if (self::$CLI) {
206205
echo $l->t('Cannot write into "config" directory!') . "\n";
@@ -711,6 +710,7 @@ public static function init(): void {
711710
self::performSameSiteCookieProtection($config);
712711

713712
if (!defined('OC_CONSOLE')) {
713+
$eventLogger->start('check_server', 'Run a few configuration checks');
714714
$errors = OC_Util::checkServer($systemConfig);
715715
if (count($errors) > 0) {
716716
if (!self::$CLI) {
@@ -745,13 +745,15 @@ public static function init(): void {
745745
} elseif (self::$CLI && $config->getSystemValueBool('installed', false)) {
746746
$config->deleteAppValue('core', 'cronErrors');
747747
}
748+
$eventLogger->end('check_server');
748749
}
749750

750751
// User and Groups
751752
if (!$systemConfig->getValue('installed', false)) {
752753
self::$server->getSession()->set('user_id', '');
753754
}
754755

756+
$eventLogger->start('setup_backends', 'Setup group and user backends');
755757
Server::get(\OCP\IUserManager::class)->registerBackend(new \OC\User\Database());
756758
Server::get(\OCP\IGroupManager::class)->addBackend(new \OC\Group\Database());
757759

@@ -770,6 +772,7 @@ public static function init(): void {
770772
// Run upgrades in incognito mode
771773
OC_User::setIncognitoMode(true);
772774
}
775+
$eventLogger->end('setup_backends');
773776

774777
self::registerCleanupHooks($systemConfig);
775778
self::registerShareHooks($systemConfig);

lib/private/AppFramework/Bootstrap/Coordinator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ private function registerApps(array $appIds): void {
8383
$appNameSpace = App::buildAppNamespace($appId);
8484
$applicationClassName = $appNameSpace . '\\AppInfo\\Application';
8585
try {
86-
if (class_exists($applicationClassName) && in_array(IBootstrap::class, class_implements($applicationClassName), true)) {
86+
if (class_exists($applicationClassName) && is_a($applicationClassName, IBootstrap::class, true)) {
8787
$this->eventLogger->start("bootstrap:register_app:$appId:application", "Load `Application` instance for $appId");
8888
try {
89-
/** @var IBootstrap|App $application */
89+
/** @var IBootstrap&App $application */
9090
$apps[$appId] = $application = $this->serverContainer->query($applicationClassName);
9191
} catch (QueryException $e) {
9292
// Weird, but ok

lib/private/AppFramework/Utility/SimpleContainer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ public function registerService($name, Closure $closure, $shared = true) {
153153
return $closure($this);
154154
};
155155
$name = $this->sanitizeName($name);
156-
if (isset($this[$name])) {
157-
unset($this[$name]);
156+
if (isset($this->container[$name])) {
157+
unset($this->container[$name]);
158158
}
159159
if ($shared) {
160-
$this[$name] = $wrapped;
160+
$this->container[$name] = $wrapped;
161161
} else {
162-
$this[$name] = $this->container->factory($wrapped);
162+
$this->container[$name] = $this->container->factory($wrapped);
163163
}
164164
}
165165

lib/private/Route/Router.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public function __construct(
5454
protected LoggerInterface $logger,
5555
IRequest $request,
5656
private IConfig $config,
57-
private IEventLogger $eventLogger,
57+
protected IEventLogger $eventLogger,
5858
private ContainerInterface $container,
59-
private IAppManager $appManager,
59+
protected IAppManager $appManager,
6060
) {
6161
$baseUrl = \OC::$WEBROOT;
6262
if (!($config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) {
@@ -116,9 +116,11 @@ public function loadRoutes($app = null) {
116116
$this->loaded = true;
117117
$routingFiles = $this->getRoutingFiles();
118118

119+
$this->eventLogger->start('route:load:attributes', 'Loading Routes from attributes');
119120
foreach (\OC_App::getEnabledApps() as $enabledApp) {
120121
$this->loadAttributeRoutes($enabledApp);
121122
}
123+
$this->eventLogger->end('route:load:attributes');
122124
} else {
123125
if (isset($this->loadedApps[$app])) {
124126
return;
@@ -140,6 +142,7 @@ public function loadRoutes($app = null) {
140142
}
141143
}
142144

145+
$this->eventLogger->start('route:load:files', 'Loading Routes from files');
143146
foreach ($routingFiles as $app => $file) {
144147
if (!isset($this->loadedApps[$app])) {
145148
if (!$this->appManager->isAppLoaded($app)) {
@@ -160,6 +163,7 @@ public function loadRoutes($app = null) {
160163
$this->root->addCollection($collection);
161164
}
162165
}
166+
$this->eventLogger->end('route:load:files');
163167

164168
if (!isset($this->loadedApps['core'])) {
165169
$this->loadedApps['core'] = true;
@@ -265,6 +269,7 @@ public function findMatchingRoute(string $url): array {
265269
$this->loadRoutes();
266270
}
267271

272+
$this->eventLogger->start('route:url:match', 'Symfony url matcher call');
268273
$matcher = new UrlMatcher($this->root, $this->context);
269274
try {
270275
$parameters = $matcher->match($url);
@@ -283,6 +288,7 @@ public function findMatchingRoute(string $url): array {
283288
throw $e;
284289
}
285290
}
291+
$this->eventLogger->end('route:url:match');
286292

287293
$this->eventLogger->end('route:match');
288294
return $parameters;

lib/private/ServerContainer.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,17 @@ public function query(string $name, bool $autoload = true) {
128128
} catch (QueryException $e) {
129129
// Continue with general autoloading then
130130
}
131-
}
132-
133-
// In case the service starts with OCA\ we try to find the service in
134-
// the apps container first.
135-
if (($appContainer = $this->getAppContainerForService($name)) !== null) {
136-
try {
137-
return $appContainer->queryNoFallback($name);
138-
} catch (QueryException $e) {
139-
// Didn't find the service or the respective app container
140-
// In this case the service won't be part of the core container,
141-
// so we can throw directly
142-
throw $e;
131+
// In case the service starts with OCA\ we try to find the service in
132+
// the apps container first.
133+
if (($appContainer = $this->getAppContainerForService($name)) !== null) {
134+
try {
135+
return $appContainer->queryNoFallback($name);
136+
} catch (QueryException $e) {
137+
// Didn't find the service or the respective app container
138+
// In this case the service won't be part of the core container,
139+
// so we can throw directly
140+
throw $e;
141+
}
143142
}
144143
} elseif (str_starts_with($name, 'OC\\Settings\\') && substr_count($name, '\\') >= 3) {
145144
$segments = explode('\\', $name);

status.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,42 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
68
* SPDX-License-Identifier: AGPL-3.0-only
79
*/
10+
811
require_once __DIR__ . '/lib/versioncheck.php';
912

13+
use OC\SystemConfig;
14+
use OCP\Defaults;
15+
use OCP\Server;
16+
use OCP\ServerVersion;
17+
use OCP\Util;
1018
use Psr\Log\LoggerInterface;
1119

1220
try {
1321
require_once __DIR__ . '/lib/base.php';
1422

15-
$systemConfig = \OC::$server->getSystemConfig();
23+
$systemConfig = Server::get(SystemConfig::class);
1624

1725
$installed = (bool)$systemConfig->getValue('installed', false);
1826
$maintenance = (bool)$systemConfig->getValue('maintenance', false);
1927
# see core/lib/private/legacy/defaults.php and core/themes/example/defaults.php
2028
# for description and defaults
21-
$defaults = new \OCP\Defaults();
29+
$defaults = new Defaults();
30+
$serverVersion = Server::get(ServerVersion::class);
2231
$values = [
2332
'installed' => $installed,
2433
'maintenance' => $maintenance,
25-
'needsDbUpgrade' => \OCP\Util::needUpgrade(),
26-
'version' => implode('.', \OCP\Util::getVersion()),
27-
'versionstring' => \OCP\Server::get(\OCP\ServerVersion::class)->getVersionString(),
34+
'needsDbUpgrade' => Util::needUpgrade(),
35+
'version' => implode('.', $serverVersion->getVersion()),
36+
'versionstring' => $serverVersion->getVersionString(),
2837
'edition' => '',
2938
'productname' => $defaults->getProductName(),
30-
'extendedSupport' => \OCP\Util::hasExtendedSupport()
39+
'extendedSupport' => Util::hasExtendedSupport()
3140
];
3241
if (OC::$CLI) {
3342
print_r($values);
@@ -38,5 +47,5 @@
3847
}
3948
} catch (Exception $ex) {
4049
http_response_code(500);
41-
\OCP\Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'remote','exception' => $ex]);
50+
Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'remote','exception' => $ex]);
4251
}

0 commit comments

Comments
 (0)