From 612ce1f86bbff51973376efa89056587e5e12a50 Mon Sep 17 00:00:00 2001 From: Dimitri Sitchet Tomkeu Date: Wed, 29 Jan 2025 12:33:31 +0100 Subject: [PATCH 1/3] feat: recuperation recursive des parametres --- spec/Command.spec.php | 2 ++ spec/FileHandler.spec.php | 40 +++++++++++++++++++++++++++++++++++++++ spec/Parametres.spec.php | 2 ++ spec/bootstrap.php | 1 + src/Parametres.php | 16 +++++++++++----- 5 files changed, 56 insertions(+), 5 deletions(-) diff --git a/spec/Command.spec.php b/spec/Command.spec.php index fa31e0b..5d3873e 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..70c4ac0 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..88084a2 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..87f658e 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,15 @@ 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 +78,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 +168,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)]; } } From 11122d11f57012407ade3d940c413c1f903668b3 Mon Sep 17 00:00:00 2001 From: Dimitri Sitchet Tomkeu Date: Wed, 29 Jan 2025 12:43:51 +0100 Subject: [PATCH 2/3] chore: exclure les migrations des classmap de composer --- composer.json | 3 +++ 1 file changed, 3 insertions(+) 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" ] From 72fd4be857e482cede4d08a6a0fe7cd26eaa4035 Mon Sep 17 00:00:00 2001 From: dimtrovich <37987162+dimtrovich@users.noreply.github.com> Date: Wed, 29 Jan 2025 11:48:34 +0000 Subject: [PATCH 3/3] Fix styling --- spec/Command.spec.php | 2 +- spec/FileHandler.spec.php | 76 +++++++++++++++++++-------------------- spec/Parametres.spec.php | 2 +- src/Parametres.php | 13 +++---- 4 files changed, 47 insertions(+), 46 deletions(-) diff --git a/spec/Command.spec.php b/spec/Command.spec.php index 5d3873e..0cc5f79 100644 --- a/spec/Command.spec.php +++ b/spec/Command.spec.php @@ -13,7 +13,7 @@ describe('Parametres / Command', function () { it('La commande `parametres:clear` fonctionne', function () { - config()->set('parametres.file.path', $path = storage_path('.parametres.json')); + config()->set('parametres.file.path', $path = storage_path('.parametres.json')); $parametres = service('parametres'); diff --git a/spec/FileHandler.spec.php b/spec/FileHandler.spec.php index 70c4ac0..9dd30e2 100644 --- a/spec/FileHandler.spec.php +++ b/spec/FileHandler.spec.php @@ -339,43 +339,43 @@ ]))->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', + 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(); - }); - }); + '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 88084a2..b825ce5 100644 --- a/spec/Parametres.spec.php +++ b/spec/Parametres.spec.php @@ -18,7 +18,7 @@ describe('Parametres / Parametres', function () { beforeEach(function () { config()->reset('parametres'); - config()->set('parametres.handlers', ['array']); + config()->set('parametres.handlers', ['array']); $this->parametres = new Parametres(config('parametres')); }); diff --git a/src/Parametres.php b/src/Parametres.php index 87f658e..4d3e30e 100644 --- a/src/Parametres.php +++ b/src/Parametres.php @@ -67,9 +67,10 @@ 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 $data; + return Arr::getRecursive($data, str_replace($property . '.', '', $dotProperty)); + } + + return $data; } } @@ -168,9 +169,9 @@ private function parseDotSyntax(string $key): array */ private function prepareFileAndProperty(string $key): array { - $parts = $this->parseDotSyntax($key); - $file = array_shift($parts); - $property = $parts[0]; + $parts = $this->parseDotSyntax($key); + $file = array_shift($parts); + $property = $parts[0]; $config = config($file);