Skip to content

Commit e832c14

Browse files
committed
Improve PHP opcache setup check and reduce level in some cases
Signed-off-by: Côme Chilliet <[email protected]>
1 parent c92fbca commit e832c14

1 file changed

Lines changed: 35 additions & 21 deletions

File tree

apps/settings/lib/SetupChecks/PhpOpcacheSetup.php

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,47 +49,44 @@ public function getCategory(): string {
4949

5050
/**
5151
* Checks whether a PHP OPcache is properly set up
52-
* @return string[] The list of OPcache setup recommendations
52+
* @return array{'warning'|'error',list<string>} The level and the list of OPcache setup recommendations
5353
*/
5454
protected function getOpcacheSetupRecommendations(): array {
55+
$level = 'warning';
56+
5557
// If the module is not loaded, return directly to skip inapplicable checks
5658
if (!extension_loaded('Zend OPcache')) {
57-
return [$this->l10n->t('The PHP OPcache module is not loaded. For better performance it is recommended to load it into your PHP installation.')];
59+
return ['error',[$this->l10n->t('The PHP OPcache module is not loaded. For better performance it is recommended to load it into your PHP installation.')]];
5860
}
5961

6062
$recommendations = [];
6163

6264
// Check whether Nextcloud is allowed to use the OPcache API
6365
$isPermitted = true;
6466
$permittedPath = $this->iniGetWrapper->getString('opcache.restrict_api');
65-
if (isset($permittedPath) && $permittedPath !== '' && !str_starts_with(\OC::$SERVERROOT, rtrim($permittedPath, '/'))) {
67+
if ($permittedPath !== '' && !str_starts_with(\OC::$SERVERROOT, rtrim($permittedPath, '/'))) {
6668
$isPermitted = false;
6769
}
6870

6971
if (!$this->iniGetWrapper->getBool('opcache.enable')) {
7072
$recommendations[] = $this->l10n->t('OPcache is disabled. For better performance, it is recommended to apply "opcache.enable=1" to your PHP configuration.');
71-
72-
// Check for saved comments only when OPcache is currently disabled. If it was enabled, opcache.save_comments=0 would break Nextcloud in the first place.
73-
if (!$this->iniGetWrapper->getBool('opcache.save_comments')) {
74-
$recommendations[] = $this->l10n->t('OPcache is configured to remove code comments. With OPcache enabled, "opcache.save_comments=1" must be set for Nextcloud to function.');
75-
}
76-
77-
if (!$isPermitted) {
78-
$recommendations[] = $this->l10n->t('Nextcloud is not allowed to use the OPcache API. With OPcache enabled, it is highly recommended to include all Nextcloud directories with "opcache.restrict_api" or unset this setting to disable OPcache API restrictions, to prevent errors during Nextcloud core or app upgrades.');
79-
}
80-
} elseif (!$isPermitted) {
81-
$recommendations[] = $this->l10n->t('Nextcloud is not allowed to use the OPcache API. It is highly recommended to include all Nextcloud directories with "opcache.restrict_api" or unset this setting to disable OPcache API restrictions, to prevent errors during Nextcloud core or app upgrades.');
73+
$level = 'error';
8274
} elseif ($this->iniGetWrapper->getBool('opcache.file_cache_only')) {
8375
$recommendations[] = $this->l10n->t('The shared memory based OPcache is disabled. For better performance, it is recommended to apply "opcache.file_cache_only=0" to your PHP configuration and use the file cache as second level cache only.');
8476
} else {
8577
// Check whether opcache_get_status has been explicitly disabled an in case skip usage based checks
8678
$disabledFunctions = $this->iniGetWrapper->getString('disable_functions');
8779
if (isset($disabledFunctions) && str_contains($disabledFunctions, 'opcache_get_status')) {
88-
return [];
80+
return [$level, $recommendations];
8981
}
9082

9183
$status = opcache_get_status(false);
9284

85+
if ($status === false) {
86+
$recommendations[] = $this->l10n->t('OPcache is not working as it should, opcache_get_status() returns false, please check configuration.');
87+
$level = 'error';
88+
}
89+
9390
// Recommend to raise value, if more than 90% of max value is reached
9491
if (
9592
empty($status['opcache_statistics']['max_cached_keys']) ||
@@ -117,16 +114,33 @@ protected function getOpcacheSetupRecommendations(): array {
117114
}
118115
}
119116

120-
return $recommendations;
117+
// Check for saved comments only when OPcache is currently disabled. If it was enabled, opcache.save_comments=0 would break Nextcloud in the first place.
118+
if (!$this->iniGetWrapper->getBool('opcache.save_comments')) {
119+
$recommendations[] = $this->l10n->t('OPcache is configured to remove code comments. With OPcache enabled, "opcache.save_comments=1" must be set for Nextcloud to function.');
120+
$level = 'error';
121+
}
122+
123+
if (!$isPermitted) {
124+
$recommendations[] = $this->l10n->t('Nextcloud is not allowed to use the OPcache API. With OPcache enabled, it is highly recommended to include all Nextcloud directories with "opcache.restrict_api" or unset this setting to disable OPcache API restrictions, to prevent errors during Nextcloud core or app upgrades.');
125+
$level = 'error';
126+
}
127+
128+
return [$level, $recommendations];
121129
}
122130

123131
public function run(): SetupResult {
124-
$recommendations = $this->getOpcacheSetupRecommendations();
132+
[$level,$recommendations] = $this->getOpcacheSetupRecommendations();
125133
if (!empty($recommendations)) {
126-
return SetupResult::error(
127-
$this->l10n->t('The PHP OPcache module is not properly configured. %s.', implode("\n", $recommendations)),
128-
$this->urlGenerator->linkToDocs('admin-php-opcache')
129-
);
134+
return match($level) {
135+
'error' => SetupResult::error(
136+
$this->l10n->t('The PHP OPcache module is not properly configured. %s.', implode("\n", $recommendations)),
137+
$this->urlGenerator->linkToDocs('admin-php-opcache')
138+
),
139+
default => SetupResult::warning(
140+
$this->l10n->t('The PHP OPcache module is not properly configured. %s.', implode("\n", $recommendations)),
141+
$this->urlGenerator->linkToDocs('admin-php-opcache')
142+
),
143+
};
130144
} else {
131145
return SetupResult::success($this->l10n->t('Correctly configured'));
132146
}

0 commit comments

Comments
 (0)