diff --git a/src/Config/Registrar.php b/src/Config/Registrar.php new file mode 100644 index 0000000..9d23832 --- /dev/null +++ b/src/Config/Registrar.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace BlitzPHP\Parametres\Config; + +class Registrar +{ + /** + * Enregistre les fichiers de configurations publiable + */ + public static function config(): array + { + return ['parametres']; + } +} diff --git a/src/Handlers/ArrayHandler.php b/src/Handlers/ArrayHandler.php index 2ac526d..65b2cce 100644 --- a/src/Handlers/ArrayHandler.php +++ b/src/Handlers/ArrayHandler.php @@ -11,6 +11,8 @@ namespace BlitzPHP\Parametres\Handlers; +use BlitzPHP\Utilities\Iterable\Arr; + /** * Gestionnaire de paramètres via des tableaux * @@ -97,10 +99,10 @@ public function flush(): void protected function hasStored(string $file, string $property, ?string $context = null): bool { if ($context === null) { - return isset($this->general[$file]) && array_key_exists($property, $this->general[$file]); + return isset($this->general[$file]) && Arr::has($this->general[$file], $property); } - return isset($this->contexts[$context][$file]) && array_key_exists($property, $this->contexts[$context][$file]); + return isset($this->contexts[$context][$file]) && Arr::has($this->contexts[$context][$file], $property); } /** @@ -114,9 +116,15 @@ protected function getStored(string $file, string $property, ?string $context = return null; } - return $context === null - ? $this->parseValue(...$this->general[$file][$property]) - : $this->parseValue(...$this->contexts[$context][$file][$property]); + $value = $context === null + ? Arr::get($this->general[$file], $property) + : Arr::get($this->contexts[$context][$file], $property); + + if (Arr::dimensions($value) === 2) { + return array_map(fn ($v) => $this->parseValue(...$v), $value); + } + + return $this->parseValue(...$value); } /** @@ -128,15 +136,21 @@ protected function setStored(string $file, string $property, mixed $value, ?stri $value = $this->prepareValue($value); if ($context === null) { - $this->general[$file][$property] = [ + if (empty($this->general[$file])) { + $this->general[$file] = []; + } + Arr::set($this->general[$file], $property, [ $value, $type, - ]; + ]); } else { - $this->contexts[$context][$file][$property] = [ + if (empty($this->contexts[$context][$file])) { + $this->contexts[$context][$file] = []; + } + Arr::set($this->contexts[$context][$file], $property, [ $value, $type, - ]; + ]); } } @@ -146,9 +160,9 @@ protected function setStored(string $file, string $property, mixed $value, ?stri protected function forgetStored(string $file, string $property, ?string $context): void { if ($context === null) { - unset($this->general[$file][$property]); + Arr::forget($this->general[$file], $property); } else { - unset($this->contexts[$context][$file][$property]); + Arr::forget($this->contexts[$context][$file], $property); } } diff --git a/src/Parametres.php b/src/Parametres.php index 4d3e30e..8d2ce6e 100644 --- a/src/Parametres.php +++ b/src/Parametres.php @@ -59,7 +59,7 @@ public function __construct(array $config) /** * Récupère une valeur de n'importe quel gestionnaire ou d'un fichier de configuration correspondant au nom file.arg.optionalArg */ - public function get(string $key, ?string $context = null): mixed + public function get(string $key, ?string $context = null, mixed $default = null): mixed { [$file, $property, $config, $dotProperty] = $this->prepareFileAndProperty($key); @@ -67,7 +67,7 @@ public function get(string $key, ?string $context = null): mixed foreach ($this->handlers as $handler) { if ($handler->has($file, $property, $context)) { if (is_array($data = $handler->get($file, $property, $context)) && $property !== $dotProperty) { - return Arr::getRecursive($data, str_replace($property . '.', '', $dotProperty)); + return Arr::get($data, str_replace($property . '.', '', $dotProperty), $default); } return $data; @@ -76,10 +76,10 @@ public function get(string $key, ?string $context = null): mixed // Si aucune valeur contextuelle n'a été trouvée, on revient à la valeur générale. if ($context !== null) { - return $this->get($key); + return $this->get($key, null, $default); } - return Arr::getRecursive($config, $dotProperty); + return Arr::get($config, $dotProperty, $default); } /** @@ -87,10 +87,10 @@ public function get(string $key, ?string $context = null): mixed */ public function set(string $key, mixed $value = null, ?string $context = null): void { - [$file, $property] = $this->prepareFileAndProperty($key); + [$file, , , $dotProperty] = $this->prepareFileAndProperty($key); foreach ($this->getWriteHandlers() as $handler) { - $handler->set($file, $property, $value, $context); + $handler->set($file, $dotProperty, $value, $context); } } @@ -100,10 +100,10 @@ public function set(string $key, mixed $value = null, ?string $context = null): */ public function forget(string $key, ?string $context = null): void { - [$file, $property] = $this->prepareFileAndProperty($key); + [$file, , $dotProperty] = $this->prepareFileAndProperty($key); foreach ($this->getWriteHandlers() as $handler) { - $handler->forget($file, $property, $context); + $handler->forget($file, $dotProperty, $context); } } diff --git a/src/Publishers/ParametresPublisher.php b/src/Publishers/ParametresPublisher.php deleted file mode 100644 index 99f3cc2..0000000 --- a/src/Publishers/ParametresPublisher.php +++ /dev/null @@ -1,35 +0,0 @@ - - * - * For the full copyright and license information, please view - * the LICENSE file that was distributed with this source code. - */ - -namespace BlitzPHP\Parametres\Publishers; - -use BlitzPHP\Publisher\Publisher; - -class ParametresPublisher extends Publisher -{ - /** - * {@inheritDoc} - */ - protected string $source = __DIR__ . '/../Config/'; - - /** - * {@inheritDoc} - */ - protected string $destination = CONFIG_PATH; - - /** - * {@inheritDoc} - */ - public function publish(): bool - { - return $this->addPaths(['parametres.php'])->merge(false); - } -}