Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"psr-4": {
"BlitzPHP\\Parametres\\": "src"
},
"exclude-from-classmap": [
"**/Database/Migrations/**"
],
"files": [
"src/Config/helpers.php"
]
Expand Down
2 changes: 2 additions & 0 deletions spec/Command.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
40 changes: 40 additions & 0 deletions spec/FileHandler.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
2 changes: 2 additions & 0 deletions spec/Parametres.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
describe('Parametres / Parametres', function () {
beforeEach(function () {
config()->reset('parametres');
config()->set('parametres.handlers', ['array']);

$this->parametres = new Parametres(config('parametres'));
});

Expand Down
1 change: 1 addition & 0 deletions spec/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down
17 changes: 12 additions & 5 deletions src/Parametres.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace BlitzPHP\Parametres;

use BlitzPHP\Parametres\Handlers\BaseHandler;
use BlitzPHP\Utilities\Iterable\Arr;
use InvalidArgumentException;
use RuntimeException;

Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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)];
}
}