Skip to content

Commit

Permalink
Improved work with the static data register
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jun 22, 2024
1 parent 4f61357 commit ef9a0d3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use LaravelLang\Models\Services\Registry;
use LaravelLang\Models\Services\Relation;

use function app;
use function filled;
use function in_array;
use function is_iterable;
Expand Down Expand Up @@ -167,7 +168,7 @@ public function newInstance($attributes = [], $exists = false): static

public function translatable(): array
{
return Registry::get(__METHOD__, function () {
return app(Registry::class)->get(__METHOD__, function () {
return (new (static::translationModelName())())->translatable();
});
}
Expand Down
11 changes: 11 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use LaravelLang\Models\Console\ModelMakeCommand;
use LaravelLang\Models\Console\ModelsHelperCommand;
use LaravelLang\Models\Services\Registry;

class ServiceProvider extends BaseServiceProvider
{
public function register(): void
{
$this->registry();
}

public function boot(): void
{
if ($this->app->runningInConsole() || $this->app->runningUnitTests()) {
Expand All @@ -32,4 +38,9 @@ protected function bootMigrations(): void
__DIR__ . '/../database/migrations'
);
}

protected function registry(): void
{
$this->app->singleton(Registry::class, fn () => new Registry());
}
}
6 changes: 3 additions & 3 deletions src/Services/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

class Registry
{
public static array $values = [];
protected array $values = [];

public static function get(string $key, Closure $callback): mixed
public function get(string $key, Closure $callback): mixed
{
return static::$values[$key] ?? $callback();
return $this->values[$key] ?? $callback();
}
}

0 comments on commit ef9a0d3

Please sign in to comment.