mirror of
https://github.com/nextcloud/server.git
synced 2026-11-14 06:32:09 +01:00
PHP 8.3 allows class constants to declare a type. Adopt it where doing so cannot break third-party apps. Typing an inheritable constant is a hard BC break: a subclass that redeclares it untyped fails to load with "Type of C::FOO must be compatible with P::FOO of type string". Changes are therefore limited to constants that cannot be redeclared by a subclass: - private const (not inherited) - final public/protected const - constants declared in a final class or an enum Interface constants, trait constants, and public/protected constants in non-final (including abstract) classes are left untyped, as is all of lib/public (OCP) and lib/unstable (NCU). One review-requested exception: the public OBJECT_PREFIX/OBJECT_SUFFIX constants of the app-internal CalDAV import helpers (TextImporter, XmlImporter) are typed as well; these classes are not public API and have no subclasses. Only string, int and array are used. float is avoided because it would silently coerce an int literal and change === comparisons. No constant name or value is modified: every changed line adds only the type token, so runtime behaviour is unchanged. Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
106 lines
2.6 KiB
PHP
106 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace OC\OpenMetrics;
|
|
|
|
use Generator;
|
|
use OC\OpenMetrics\Exporters\ActiveSessions;
|
|
use OC\OpenMetrics\Exporters\ActiveUsers;
|
|
use OC\OpenMetrics\Exporters\AppEnabled;
|
|
use OC\OpenMetrics\Exporters\FilesByType;
|
|
use OC\OpenMetrics\Exporters\InstanceInfo;
|
|
use OC\OpenMetrics\Exporters\LogLevel;
|
|
use OC\OpenMetrics\Exporters\Maintenance;
|
|
use OC\OpenMetrics\Exporters\RunningJobs;
|
|
use OC\OpenMetrics\Exporters\UsersByBackend;
|
|
use OCP\App\IAppManager;
|
|
use OCP\IConfig;
|
|
use OCP\OpenMetrics\IMetricFamily;
|
|
use OCP\Server;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class ExporterManager {
|
|
private array $skippedClasses;
|
|
private const string XML_ENTRY = 'openmetrics';
|
|
|
|
public function __construct(
|
|
private IAppManager $appManager,
|
|
private LoggerInterface $logger,
|
|
IConfig $config,
|
|
) {
|
|
// Use values as keys for faster lookups
|
|
$this->skippedClasses = array_fill_keys($config->getSystemValue('openmetrics_skipped_classes', []), true);
|
|
}
|
|
|
|
public function export(): Generator {
|
|
// Core exporters
|
|
$exporters = [
|
|
// Basic exporters
|
|
InstanceInfo::class,
|
|
AppEnabled::class,
|
|
Maintenance::class,
|
|
LogLevel::class,
|
|
|
|
// File exporters
|
|
FilesByType::class,
|
|
|
|
// Users exporters
|
|
ActiveUsers::class,
|
|
ActiveSessions::class,
|
|
UsersByBackend::class,
|
|
|
|
// Jobs
|
|
RunningJobs::class,
|
|
];
|
|
$exporters = array_filter($exporters, fn ($classname) => !isset($this->skippedClasses[$classname]));
|
|
foreach ($exporters as $classname) {
|
|
$exporter = $this->loadExporter($classname);
|
|
if ($exporter !== null) {
|
|
yield $exporter;
|
|
}
|
|
}
|
|
|
|
// Apps exporters
|
|
foreach ($this->appManager->getEnabledApps() as $appId) {
|
|
$appInfo = $this->appManager->getAppInfo($appId);
|
|
if (!isset($appInfo[self::XML_ENTRY]) || !is_array($appInfo[self::XML_ENTRY])) {
|
|
continue;
|
|
}
|
|
foreach ($appInfo[self::XML_ENTRY] as $classnames) {
|
|
foreach ($classnames as $classname) {
|
|
if (isset($this->skippedClasses[$classname])) {
|
|
continue;
|
|
}
|
|
$exporter = $this->loadExporter($classname, $appId);
|
|
if ($exporter !== null) {
|
|
yield $exporter;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private function loadExporter(string $classname, string $appId = 'core'): ?IMetricFamily {
|
|
try {
|
|
return Server::get($classname);
|
|
} catch (\Exception $e) {
|
|
$this->logger->error(
|
|
'Unable to build exporter {exporter}',
|
|
[
|
|
'app' => $appId,
|
|
'exception' => $e,
|
|
'exporter' => $classname,
|
|
],
|
|
);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|