Skip to content

Commit

Permalink
Added Example::config() helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Jan 1, 2024
1 parent 8aa9a66 commit 88272b0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
31 changes: 29 additions & 2 deletions dev/App/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace LastDragon_ru\LaraASP\Dev\App;

use Illuminate\Console\Command;
use Illuminate\Container\Container;
use Illuminate\Contracts\Config\Repository;
use LastDragon_ru\LaraASP\Core\Utils\Cast;
use LastDragon_ru\LaraASP\Core\Utils\ConfigMerger;
use LogicException;
use PhpParser\ErrorHandler\Collecting;
use PhpParser\Node;
Expand All @@ -18,6 +21,7 @@

use function array_map;
use function array_slice;
use function config;
use function debug_backtrace;
use function end;
use function file;
Expand Down Expand Up @@ -58,8 +62,12 @@ protected static function getDumper(): Dumper {
}

public function __invoke(Dumper $dumper): void {
$file = Cast::toString($this->argument('file'));
self::$dumper = $dumper;
$container = Container::getInstance();
$config = $container->make(Repository::class);
$file = Cast::toString($this->argument('file'));

$container[Repository::class] = clone $config;

try {
// Run
Expand All @@ -75,7 +83,10 @@ public function __invoke(Dumper $dumper): void {
$this->output->writeln("<markdown>{$output}</markdown>");
}
} finally {
self::$dumper = null;
Container::setInstance($container);

$container[Repository::class] = $config;
self::$dumper = null;
}
}

Expand All @@ -87,6 +98,22 @@ public static function raw(Stringable|string $value, string $type, string $expre
self::getDumper()->raw($value, $expression ?? self::getExpression(__FUNCTION__), $type);
}

/**
* @param array<string, mixed> $settings
*/
public static function config(string $root, array $settings): void {
// Example?
self::getDumper();

// Update
$config = (array) config($root, []);
$config = (new ConfigMerger())->merge([ConfigMerger::Strict => false], $config, $settings);

config([
$root => $config,
]);
}

private static function getExpression(string $method): ?string {
// File?
$context = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
Expand Down
4 changes: 3 additions & 1 deletion dev/App/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Illuminate\Support\ServiceProvider;
use LastDragon_ru\LaraASP\Dev\App\Provider;
use LastDragon_ru\LaraASP\Documentator\Provider as DocumentatorProvider;
use LastDragon_ru\LaraASP\Formatter\Provider as FormatterProvider;
use LastDragon_ru\LaraASP\GraphQL\Provider as GraphQLProvider;
use LastDragon_ru\LaraASP\Migrator\Provider as MigratorProvider;
use LastDragon_ru\LaraASP\Serializer\Provider as SerializerProvider;
Expand Down Expand Up @@ -32,7 +33,7 @@
| services the application utilizes. Set this in your ".env" file.
|
*/
'env' => env('APP_ENV', 'production'),
'env' => env('APP_ENV', 'local'),

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -154,6 +155,7 @@
DocumentatorProvider::class,
MigratorProvider::class,
GraphQLProvider::class,
FormatterProvider::class,
])->toArray(),

/*
Expand Down

0 comments on commit 88272b0

Please sign in to comment.