diff --git a/composer.json b/composer.json index 46397f2..56a7bc6 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,9 @@ "psr-4": { "BlitzPHP\\Parametres\\": "src" }, + "exclude-from-classmap": [ + "**/Database/Migrations/**" + ], "files": [ "src/Config/helpers.php" ] diff --git a/spec/Command.spec.php b/spec/Command.spec.php index fa31e0b..0cc5f79 100644 --- a/spec/Command.spec.php +++ b/spec/Command.spec.php @@ -13,6 +13,8 @@ describe('Parametres / Command', function () { it('La commande `parametres:clear` fonctionne', function () { + config()->set('parametres.file.path', $path = storage_path('.parametres.json')); + $parametres = service('parametres'); $parametres->set('foo.site_name', 'Humpty'); diff --git a/spec/FileHandler.spec.php b/spec/FileHandler.spec.php index f99c7bb..9dd30e2 100644 --- a/spec/FileHandler.spec.php +++ b/spec/FileHandler.spec.php @@ -338,4 +338,44 @@ 'context' => 'context:male', ]))->toBeTruthy(); }); + + describe('Recuperation recursive', function () { + it('Recuperation recursive des configurations', function () { + config()->ghost('auth')->set('auth', [ + 'session' => $expected = [ + 'field' => 'user', + 'allow_remembering' => true, + 'depth' => [ + 'field' => 'id', + 'allow_remembering' => false, + 'depth' => null, // Cas particulier pour le dernier niveau + ], + ], + ]); + + expect(parametre('auth.session'))->toBe($expected); + expect(parametre('auth.session.field'))->toBe($expected['field']); + expect(parametre('auth.session.allow_remembering'))->toBeTruthy(); + expect(parametre('auth.session.depth.field'))->toBe('id'); + expect(parametre('auth.session.depth.allow_remembering'))->toBeFalsy(); + }); + + it('Recuperation recursive des parametres du fichier json', function () { + parametre()->set('auth.session', $expected = [ + 'field' => 'user', + 'allow_remembering' => true, + 'depth' => [ + 'field' => 'id', + 'allow_remembering' => false, + 'depth' => null, // Cas particulier pour le dernier niveau + ], + ]); + + expect(parametre('auth.session'))->toBe($expected); + expect(parametre('auth.session.field'))->toBe($expected['field']); + expect(parametre('auth.session.allow_remembering'))->toBeTruthy(); + expect(parametre('auth.session.depth.field'))->toBe('id'); + expect(parametre('auth.session.depth.allow_remembering'))->toBeFalsy(); + }); + }); }); diff --git a/spec/Parametres.spec.php b/spec/Parametres.spec.php index 49d9828..b825ce5 100644 --- a/spec/Parametres.spec.php +++ b/spec/Parametres.spec.php @@ -18,6 +18,8 @@ describe('Parametres / Parametres', function () { beforeEach(function () { config()->reset('parametres'); + config()->set('parametres.handlers', ['array']); + $this->parametres = new Parametres(config('parametres')); }); diff --git a/spec/bootstrap.php b/spec/bootstrap.php index fd3218c..6a2be91 100644 --- a/spec/bootstrap.php +++ b/spec/bootstrap.php @@ -28,6 +28,7 @@ Services::container()->initialize(); config()->load('parametres', __DIR__ . '/../src/Config/parametres.php'); +config()->set('parametres.handlers', ['array']); // Fakes configurations config()->ghost('test')->set('test', [ diff --git a/src/Parametres.php b/src/Parametres.php index f8ae560..4d3e30e 100644 --- a/src/Parametres.php +++ b/src/Parametres.php @@ -12,6 +12,7 @@ namespace BlitzPHP\Parametres; use BlitzPHP\Parametres\Handlers\BaseHandler; +use BlitzPHP\Utilities\Iterable\Arr; use InvalidArgumentException; use RuntimeException; @@ -60,12 +61,16 @@ public function __construct(array $config) */ public function get(string $key, ?string $context = null): mixed { - [$file, $property, $config] = $this->prepareFileAndProperty($key); + [$file, $property, $config, $dotProperty] = $this->prepareFileAndProperty($key); // Vérifier chacun de nos gestionnaires foreach ($this->handlers as $handler) { if ($handler->has($file, $property, $context)) { - return $handler->get($file, $property, $context); + if (is_array($data = $handler->get($file, $property, $context)) && $property !== $dotProperty) { + return Arr::getRecursive($data, str_replace($property . '.', '', $dotProperty)); + } + + return $data; } } @@ -74,7 +79,7 @@ public function get(string $key, ?string $context = null): mixed return $this->get($key); } - return $config[$property] ?? null; + return Arr::getRecursive($config, $dotProperty); } /** @@ -164,10 +169,12 @@ private function parseDotSyntax(string $key): array */ private function prepareFileAndProperty(string $key): array { - [$file, $property] = $this->parseDotSyntax($key); + $parts = $this->parseDotSyntax($key); + $file = array_shift($parts); + $property = $parts[0]; $config = config($file); - return [$file, $property, $config]; + return [$file, $property, $config, implode('.', $parts)]; } }