diff options
| author | 2023-04-04 10:23:26 +0200 | |
|---|---|---|
| committer | 2023-04-04 10:23:26 +0200 | |
| commit | 36aa0122e15b6c5a4bf923467b63a577cac5a539 (patch) | |
| tree | 3dc7d2c5143157165f0248fab7470f86f76b0898 /lib/Minz/Extension.php | |
| parent | 2340f7a1bac38647f0267c1d7143c0cf04d68fcc (diff) | |
Fix extensions in actualize_script (#5243)
* Fix extension freshrss_user_maintenance in actualize_script
Follow-up of https://github.com/FreshRSS/FreshRSS/pull/3440
The hook was called before registering all the extensions for the current user
* PHPStan Level 6 for extensions
And remove 5-year old legacy format of enabled extensions < FreshRSS 1.11.1
* Fix multiple bugs in extensions
* Minor typing
* Don't change signature of methods supposed to be overridden
* PHPStan Level 9 and compatibility Intelliphense
* Set as final the methods not supposed to be overriden
Diffstat (limited to 'lib/Minz/Extension.php')
| -rw-r--r-- | lib/Minz/Extension.php | 135 |
1 files changed, 81 insertions, 54 deletions
diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index d1dabe8d5..e95fe7160 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -4,22 +4,34 @@ * The extension base class. */ abstract class Minz_Extension { + /** @var string */ private $name; + /** @var string */ private $entrypoint; + /** @var string */ private $path; + /** @var string */ private $author; + /** @var string */ private $description; + /** @var string */ private $version; + /** @var 'system'|'user' */ private $type; + /** @var string */ private $config_key = 'extensions'; + /** @var array<string,mixed>|null */ private $user_configuration; + /** @var array<string,mixed>|null */ private $system_configuration; + /** @var array{0:'system',1:'user'} */ public static $authorized_types = array( 'system', 'user', ); + /** @var bool */ private $is_enabled; /** @@ -34,9 +46,10 @@ abstract class Minz_Extension { * - version: a version for the current extension. * - type: "system" or "user" (default). * - * @param array<string> $meta_info contains information about the extension. + * @param array{'name':string,'entrypoint':string,'path':string,'author'?:string,'description'?:string,'version'?:string,'type'?:'system'|'user'} $meta_info + * contains information about the extension. */ - final public function __construct($meta_info) { + final public function __construct(array $meta_info) { $this->name = $meta_info['name']; $this->entrypoint = $meta_info['entrypoint']; $this->path = $meta_info['path']; @@ -70,22 +83,23 @@ abstract class Minz_Extension { /** * Call at the initialization of the extension (i.e. when the extension is * enabled by the extension manager). + * @return void */ abstract public function init(); /** * Set the current extension to enable. */ - public function enable() { + public final function enable(): void { $this->is_enabled = true; } /** * Return if the extension is currently enabled. * - * @return string|true true if extension is enabled, false otherwise. + * @return bool true if extension is enabled, false otherwise. */ - public function isEnabled() { + public final function isEnabled(): bool { return $this->is_enabled; } @@ -94,7 +108,7 @@ abstract class Minz_Extension { * * @return string|false html content from ext_dir/configure.phtml, false if it does not exist. */ - public function getConfigureView() { + public final function getConfigureView() { $filename = $this->path . '/configure.phtml'; if (!file_exists($filename)) { return false; @@ -107,35 +121,39 @@ abstract class Minz_Extension { /** * Handle the configure action. + * @return void */ public function handleConfigureAction() {} /** * Getters and setters. */ - public function getName() { + public final function getName(): string { return $this->name; } - public function getEntrypoint() { + public final function getEntrypoint(): string { return $this->entrypoint; } - public function getPath() { + public final function getPath(): string { return $this->path; } - public function getAuthor() { + public final function getAuthor(): string { return $this->author; } - public function getDescription() { + public final function getDescription(): string { return $this->description; } - public function getVersion() { + public final function getVersion(): string { return $this->version; } - public function getType() { + /** @return 'system'|'user' */ + public final function getType() { return $this->type; } - private function setType($type) { - if (!in_array($type, self::$authorized_types)) { + + /** @param 'user'|'system' $type */ + private function setType(string $type): void { + if (!in_array($type, ['user', 'system'])) { throw new Minz_ExtensionException('invalid `type` info', $this->name); } $this->type = $type; @@ -145,11 +163,11 @@ abstract class Minz_Extension { * Return the url for a given file. * * @param string $filename name of the file to serve. - * @param string $type the type (js or css) of the file to serve. + * @param 'css'|'js' $type the type (js or css) of the file to serve. * @param bool $isStatic indicates if the file is a static file or a user file. Default is static. * @return string url corresponding to the file. */ - public function getFileUrl($filename, $type, $isStatic = true) { + public final function getFileUrl(string $filename, string $type, bool $isStatic = true): string { if ($isStatic) { $dir = basename($this->path); $file_name_url = urlencode("{$dir}/static/{$filename}"); @@ -169,21 +187,21 @@ abstract class Minz_Extension { * * @param string $base_name the base name of the controller. Final name will be FreshExtension_<base_name>_Controller. */ - public function registerController($base_name) { + public final function registerController(string $base_name): void { Minz_Dispatcher::registerController($base_name, $this->path); } /** * Register the views in order to be accessible by the application. */ - public function registerViews() { + public final function registerViews(): void { Minz_View::addBasePathname($this->path); } /** * Register i18n files from ext_dir/i18n/ */ - public function registerTranslates() { + public final function registerTranslates(): void { $i18n_dir = $this->path . '/i18n'; Minz_Translate::registerPath($i18n_dir); } @@ -192,46 +210,48 @@ abstract class Minz_Extension { * Register a new hook. * * @param string $hook_name the hook name (must exist). - * @param callable-string|array<string> $hook_function the function name to call (must be callable). + * @param callable $hook_function the function name to call (must be callable). */ - public function registerHook($hook_name, $hook_function) { + public final function registerHook(string $hook_name, $hook_function): void { Minz_ExtensionManager::addHook($hook_name, $hook_function); } - /** - * @return bool - */ - private function isConfigurationEnabled(string $type) { + /** @param 'system'|'user' $type */ + private function isConfigurationEnabled($type): bool { if (!class_exists('FreshRSS_Context', false)) { return false; } - $conf = "{$type}_conf"; - if (null === FreshRSS_Context::$$conf) { - return false; + switch ($type) { + case 'system': return FreshRSS_Context::$system_conf !== null; + case 'user': return FreshRSS_Context::$user_conf !== null; } - - return true; } - /** - * @return bool - */ - private function isExtensionConfigured(string $type) { - $conf = "{$type}_conf"; + /** @param 'system'|'user' $type */ + private function isExtensionConfigured($type): bool { + switch ($type) { + case 'system': + $conf = FreshRSS_Context::$user_conf; + break; + case 'user': + $conf = FreshRSS_Context::$system_conf; + break; + } - if (!FreshRSS_Context::$$conf->hasParam($this->config_key)) { + if ($conf === null || !$conf->hasParam($this->config_key)) { return false; } - $extensions = FreshRSS_Context::$$conf->{$this->config_key}; + $extensions = $conf->{$this->config_key}; return array_key_exists($this->getName(), $extensions); } /** - * @return array + * @param 'system'|'user' $type + * @return array<string,mixed> */ - private function getConfiguration(string $type) { + private function getConfiguration(string $type): array { if (!$this->isConfigurationEnabled($type)) { return []; } @@ -245,16 +265,16 @@ abstract class Minz_Extension { } /** - * @return array + * @return array<string,mixed> */ - public function getSystemConfiguration() { + public final function getSystemConfiguration(): array { return $this->getConfiguration('system'); } /** - * @return array + * @return array<string,mixed> */ - public function getUserConfiguration() { + public final function getUserConfiguration(): array { return $this->getConfiguration('user'); } @@ -262,7 +282,7 @@ abstract class Minz_Extension { * @param mixed $default * @return mixed */ - public function getSystemConfigurationValue(string $key, $default = null) { + public final function getSystemConfigurationValue(string $key, $default = null) { if (!is_array($this->system_configuration)) { $this->system_configuration = $this->getSystemConfiguration(); } @@ -277,7 +297,7 @@ abstract class Minz_Extension { * @param mixed $default * @return mixed */ - public function getUserConfigurationValue(string $key, $default = null) { + public final function getUserConfigurationValue(string $key, $default = null) { if (!is_array($this->user_configuration)) { $this->user_configuration = $this->getUserConfiguration(); } @@ -288,7 +308,11 @@ abstract class Minz_Extension { return $default; } - private function setConfiguration(string $type, array $configuration) { + /** + * @param 'system'|'user' $type + * @param array<string,mixed> $configuration + */ + private function setConfiguration(string $type, array $configuration): void { $conf = "{$type}_conf"; if (FreshRSS_Context::$$conf->hasParam($this->config_key)) { @@ -302,17 +326,20 @@ abstract class Minz_Extension { FreshRSS_Context::$$conf->save(); } - public function setSystemConfiguration(array $configuration) { + /** @param array<string,mixed> $configuration */ + public final function setSystemConfiguration(array $configuration): void { $this->setConfiguration('system', $configuration); $this->system_configuration = $configuration; } - public function setUserConfiguration(array $configuration) { + /** @param array<string,mixed> $configuration */ + public final function setUserConfiguration(array $configuration): void { $this->setConfiguration('user', $configuration); $this->user_configuration = $configuration; } - private function removeConfiguration(string $type) { + /** @param 'system'|'user' $type */ + private function removeConfiguration(string $type): void { if (!$this->isConfigurationEnabled($type)) { return; } @@ -332,17 +359,17 @@ abstract class Minz_Extension { FreshRSS_Context::$$conf->save(); } - public function removeSystemConfiguration() { + public final function removeSystemConfiguration(): void { $this->removeConfiguration('system'); $this->system_configuration = null; } - public function removeUserConfiguration() { + public final function removeUserConfiguration(): void { $this->removeConfiguration('user'); $this->user_configuration = null; } - public function saveFile(string $filename, string $content) { + public final function saveFile(string $filename, string $content): void { $username = Minz_User::name(); $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}"; @@ -353,7 +380,7 @@ abstract class Minz_Extension { file_put_contents("{$path}/{$filename}", $content); } - public function removeFile(string $filename) { + public final function removeFile(string $filename): void { $username = Minz_User::name(); $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}/{$filename}"; |
