diff --git a/src/CmsPluginPanel.php b/src/CmsPluginPanel.php index 942bb1e..f8d20ff 100644 --- a/src/CmsPluginPanel.php +++ b/src/CmsPluginPanel.php @@ -98,11 +98,11 @@ public function getPanel(): string $components = ''; $componentList = $this->pluginManager->getComponentsInfo(); usort($componentList, static function (array $a, array $b): int { - if (($a['position'] ?? 50) === ($b['position'] ?? 50)) { + if ($a['position'] === $b['position']) { return $a['implements'] === $b['implements'] ? 1 : -1; } - return ($a['position'] ?? 50) < ($b['position'] ?? 50) ? 1 : -1; + return $a['position'] < $b['position'] ? 1 : -1; }); foreach ($componentList as $component) { $matchPlugin = $pluginServiceType === $component['implements']; @@ -126,7 +126,7 @@ public function getPanel(): string ? '' . htmlspecialchars($component['view']) . '' : '' . htmlspecialchars($component['view']) . '' ) - . '' . htmlspecialchars((string) ($component['position'] ?? '???'), ENT_QUOTES) . '' + . '' . htmlspecialchars((string) $component['position'], ENT_QUOTES) . '' . '' . (function (array $params): string { $return = ''; @@ -150,7 +150,7 @@ public function getPanel(): string } return $return; - })($component['params'] ?? []) + })($component['params']) . '' . ''; } diff --git a/src/Component/VueComponent.php b/src/Component/VueComponent.php index 19aa5bd..2d842d8 100644 --- a/src/Component/VueComponent.php +++ b/src/Component/VueComponent.php @@ -25,15 +25,15 @@ class VueComponent implements PluginComponent /** - * @param mixed[] $config + * @param array{key: string, name: string, implements: class-string, componentClass: class-string, view: string, source: string, position?: int, tab?: string, params?: array} $config */ public function __construct(array $config) { - $this->key = (string) $config['key']; - $this->name = (string) $config['name']; - $this->tab = (string) ($config['tab'] ?? $config['key']); - $this->source = (string) $config['source']; - $this->position = (int) ($config['position'] ?? 50); + $this->key = $config['key']; + $this->name = $config['name']; + $this->tab = $config['tab'] ?? $config['key']; + $this->source = $config['source']; + $this->position = $config['position'] ?? 50; $parameters = []; foreach ($config['params'] ?? [] as $parameterName => $parameterValue) { @@ -42,16 +42,20 @@ public function __construct(array $config) $parameterValue = null; } if (\is_string($parameterName) === false) { - throw new \InvalidArgumentException( - 'Component "' . $this->key . '": Parameter "' . $parameterName . '" must be ' - . 'a string, but "' . get_debug_type($parameterName) . '" given.', - ); + throw new \InvalidArgumentException(sprintf( + 'Component "%s": Parameter "%s" must be a string, but "%s" given.', + $this->key, + $parameterName, + get_debug_type($parameterName), + )); } if ($parameterValue !== null && is_scalar($parameterValue) === false) { - throw new \InvalidArgumentException( - 'Component "' . $this->key . '": Parameter "' . $parameterName . '" value ' - . 'must be scalar, but type "' . get_debug_type($parameterValue) . '" given.', - ); + throw new \InvalidArgumentException(sprintf( + 'Component "%s": Parameter "%s" value must be scalar, but type "%s" given.', + $this->key, + $parameterName, + get_debug_type($parameterValue), + )); } $parameters[$parameterName] = $parameterValue; } diff --git a/src/PluginComponentExtension.php b/src/PluginComponentExtension.php index d829325..27a2f49 100644 --- a/src/PluginComponentExtension.php +++ b/src/PluginComponentExtension.php @@ -55,13 +55,13 @@ public function beforeCompile(): void return; } - /** @var mixed[] $config */ + /** @var array> $config */ $config = $this->getConfig(); $components = []; foreach ($config as $key => $component) { if (\is_string($key) === false) { - throw new \RuntimeException('Component name must be string, but "' . $key . '" given.'); + throw new \RuntimeException('Component name must be string, but "' . $key . '" (' . get_debug_type($key) . ') given.'); } if (isset($component['name'], $component['implements'], $component['view'], $component['source']) === false) { throw new \RuntimeException( @@ -112,8 +112,13 @@ public function beforeCompile(): void } } catch (\ReflectionException $e) { throw new \RuntimeException( - 'Component "' . $key . '": Component class "' . $componentClass . '" is broken: ' . $e->getMessage(), - $e->getCode(), + sprintf( + 'Component "%s": Component class "%s" is broken: %s', + $key, + $componentClass, + $e->getMessage(), + ), + 500, $e, ); } @@ -138,8 +143,10 @@ public function beforeCompile(): void . 'path "' . $source . '" given.', ); } + $componentParameters = $component['params'] ?? []; + assert(is_array($componentParameters)); $params = []; - foreach ($component['params'] ?? [] as $parameter) { + foreach ($componentParameters as $parameter) { if (is_array($parameter) && count($parameter) === 1) { $parameterName = array_keys($parameter)[0] ?? throw new \RuntimeException('Broken parameter.'); $parameterValue = array_values($parameter)[0] ?? null; @@ -170,15 +177,21 @@ public function beforeCompile(): void } $params[Strings::firstLower((string) $parameterName)] = $parameterValue; } + $tab = $component['tab'] ?? $key; + $position = $component['position'] ?? 1; + $componentClass = $component['componentClass'] ?? VueComponent::class; + assert(is_string($tab)); + assert(is_int($position)); + assert(is_string($componentClass)); $components[] = (new ComponentDIDefinition( key: $key, name: trim(trim($name) === '' ? $key : $name), implements: $implements, - componentClass: $component['componentClass'] ?? VueComponent::class, + componentClass: $componentClass, view: $view, source: $source, - position: (int) ($component['position'] ?? 1), - tab: (string) ($component['tab'] ?? $key), + position: $position, + tab: $tab, params: $params, ))->toArray(); } diff --git a/src/PluginManager.php b/src/PluginManager.php index 586b1a2..bebf7c3 100644 --- a/src/PluginManager.php +++ b/src/PluginManager.php @@ -14,13 +14,13 @@ final class PluginManager { - /** @var array */ + /** @var array}> */ private array $components = []; - /** @var array|null */ + /** @var array, array{service: string, type: class-string, name: string, realName: string, baseEntity: string|null, label: string, basePath: string, priority: int, icon: string|null, roles: array, privileges: array, menuItem: array|null}>|null */ private ?array $pluginInfo = null; - /** @var string[] */ + /** @var array */ private array $baseEntityToPlugin; /** @var string[] */ @@ -43,13 +43,14 @@ public function __construct( /** * Effective all Plugin services setter. * - * @param string[] $pluginServices + * @param array $pluginServices * @internal use in DIC */ public function setPluginServices(array $pluginServices): void { + /** @var array{hash: string, plugins: array, array{service: string, type: class-string, name: string, realName: string, baseEntity: string|null, label: string, basePath: string, priority: int, icon: string|null, roles: array, privileges: array, menuItem: array|null}>, baseEntityToPlugin: array>, baseEntitySimpleToPlugin: array>, nameToType: array}|null $cache */ $cache = $this->cache->load('plugin-info'); - if ($cache === null || ((string) ($cache['hash'] ?? '')) !== $this->getPluginServicesHash($pluginServices)) { + if ($cache === null || $cache['hash'] !== $this->getPluginServicesHash($pluginServices)) { [$plugins, $baseEntityToPlugin, $baseEntitySimpleToPlugin] = $this->processPluginInfo($pluginServices); $pluginNameToType = []; @@ -57,13 +58,14 @@ public function setPluginServices(array $pluginServices): void $pluginNameToType[$plugin['name']] = $plugin['type']; } - $this->cache->save('plugin-info', $cache = [ + $cache = [ 'hash' => $this->getPluginServicesHash($pluginServices), 'plugins' => $plugins, 'baseEntityToPlugin' => $baseEntityToPlugin, 'baseEntitySimpleToPlugin' => $baseEntitySimpleToPlugin, 'nameToType' => $pluginNameToType, - ]); + ]; + $this->cache->save('plugin-info', $cache); } $this->pluginInfo = $cache['plugins']; @@ -95,7 +97,11 @@ public function getComponents(Plugin|PluginInfoEntity|string $plugin, ?string $v try { $baseEntityExist = class_exists($baseEntity); } catch (\Throwable $e) { - throw new \RuntimeException('Base entity "' . $baseEntity . '" is broken: ' . $e->getMessage(), $e->getCode(), $e); + throw new \RuntimeException( + sprintf('Base entity "%s" is broken: %s', $baseEntity, $e->getMessage()), + 500, + $e, + ); } if ($baseEntityExist === false) { throw new \InvalidArgumentException('Entity class "' . $baseEntity . '" does not exist or is not autoloadable.'); @@ -137,7 +143,7 @@ public function getComponents(Plugin|PluginInfoEntity|string $plugin, ?string $v /** - * @param mixed[] $components + * @param array}> $components * @internal use in DIC */ public function addComponents(array $components): void @@ -147,7 +153,7 @@ public function addComponents(array $components): void /** - * @param mixed[] $component + * @param array{key: string, name: string, implements: class-string, componentClass: class-string, view: string, source: string, position: int, tab: string, params: array} $component * @internal use in DIC */ public function addComponent(array $component): void @@ -157,7 +163,7 @@ public function addComponent(array $component): void /** - * @return mixed[] + * @return array}> */ public function getComponentsInfo(): array { @@ -227,7 +233,7 @@ public function getPluginByType(string $type): Plugin /** - * @return mixed[] + * @return array, array{service: string, type: class-string, name: string, realName: string, baseEntity: string|null, label: string, basePath: string, priority: int, icon: string|null, roles: array, privileges: array, menuItem: array|null}> */ public function getPluginInfo(): array {