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
23 changes: 23 additions & 0 deletions src/Config/Registrar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* This file is part of BlitzPHP Parametres.
*
* (c) 2025 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
*
* 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'];
}
}
36 changes: 25 additions & 11 deletions src/Handlers/ArrayHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace BlitzPHP\Parametres\Handlers;

use BlitzPHP\Utilities\Iterable\Arr;

/**
* Gestionnaire de paramètres via des tableaux
*
Expand Down Expand Up @@ -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);
}

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

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

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

Expand Down
16 changes: 8 additions & 8 deletions src/Parametres.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ 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);

// Vérifier chacun de nos gestionnaires
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;
Expand All @@ -76,21 +76,21 @@ 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);
}

/**
* Sauvegarde d'une valeur dans le gestionnaire d'écriture pour récupération ultérieure.
*/
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);
}
}

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

Expand Down
35 changes: 0 additions & 35 deletions src/Publishers/ParametresPublisher.php

This file was deleted.