Skip to content

Commit

Permalink
Hotfix has been turned into a good solution
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jun 12, 2024
1 parent 1be4bbd commit b7260d4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
3 changes: 2 additions & 1 deletion config/private.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use LaravelLang\Config\Helpers\Path;
use LaravelLang\LocaleList\Direction;
use LaravelLang\LocaleList\Locale;

Expand All @@ -10,7 +11,7 @@
'packages' => [],

'models' => [
'directory' => base_path('app'),
'directory' => Path::app(),
],

'map' => [
Expand Down
27 changes: 27 additions & 0 deletions src/Helpers/Path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Config\Helpers;

use Illuminate\Support\Env;

use function base_path;

class Path
{
public static function app(): string
{
return base_path('app');
}

public static function helpers(): string
{
return static::vendor() . '/_laravel_lang';
}

public static function vendor(): string
{
return Env::get('COMPOSER_VENDOR_DIR') ?: base_path('vendor');
}
}
36 changes: 21 additions & 15 deletions src/Services/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use LaravelLang\Config\Data\Shared\SmartPunctuationData;
use LaravelLang\Config\Data\SharedData;
use LaravelLang\Config\Enums\Name;
use LaravelLang\Config\Helpers\Path;

use function is_null;

Expand All @@ -27,8 +28,8 @@ public function __construct(
public function shared(): SharedData
{
return new SharedData(
inline : (bool) $this->value(Name::Shared, 'inline'),
align : (bool) $this->value(Name::Shared, 'align'),
inline : (bool) $this->value(Name::Shared, 'inline', fallback: false),
align : (bool) $this->value(Name::Shared, 'align', fallback: true),
aliases : $this->value(Name::Shared, 'aliases', object: NonPushableData::class),
punctuation: $this->smartPunctuation(),
routes : $this->routes(),
Expand All @@ -49,15 +50,15 @@ public function hidden(): HiddenData
protected function hiddenModels(): HiddenModelsData
{
return new HiddenModelsData(
directory: $this->value(Name::Hidden, 'models.directory') ?? base_path('app'),
directory: $this->value(Name::Hidden, 'models.directory', fallback: Path::app()),
);
}

protected function smartPunctuation(): SmartPunctuationData
{
return new SmartPunctuationData(
enabled: $this->value(Name::Shared, 'smart_punctuation.enable'),
common : $this->value(Name::Shared, 'smart_punctuation.common'),
enabled: $this->value(Name::Shared, 'smart_punctuation.enable', fallback: false),
common : $this->value(Name::Shared, 'smart_punctuation.common', fallback: []),

locales: $this->value(
Name::Shared,
Expand All @@ -72,10 +73,10 @@ protected function routes(): RoutesData
{
return new RoutesData(
names: new RouteNameData(
parameter: $this->value(Name::Shared, 'routes.names.parameter'),
header : $this->value(Name::Shared, 'routes.names.header'),
cookie : $this->value(Name::Shared, 'routes.names.cookie'),
session : $this->value(Name::Shared, 'routes.names.session'),
parameter: $this->value(Name::Shared, 'routes.names.parameter', fallback: 'locale'),
header : $this->value(Name::Shared, 'routes.names.header', fallback: 'X-Localization'),
cookie : $this->value(Name::Shared, 'routes.names.cookie', fallback: 'X-Localization'),
session : $this->value(Name::Shared, 'routes.names.session', fallback: 'X-Localization'),
)
);
}
Expand All @@ -84,19 +85,24 @@ protected function models(): ModelsData
{
return new ModelsData(
connection: $this->value(Name::Shared, 'models.connection'),
table : $this->value(Name::Shared, 'models.table'),
flags : $this->value(Name::Shared, 'models.flags'),
helpers : $this->value(Name::Shared, 'models.helpers'),
table : $this->value(Name::Shared, 'models.table', fallback: 'translations'),
flags : $this->value(Name::Shared, 'models.flags', fallback: 0),
helpers : $this->value(Name::Shared, 'models.helpers', fallback: Path::helpers()),
);
}

protected function value(Name $name, string $key, ?string $default = null, ?string $object = null): mixed
{
protected function value(
Name $name,
string $key,
?string $default = null,
?string $object = null,
mixed $fallback = null
): mixed {
$main = $name->value . '.' . $key;
$default = $default ? $name->value . '.' . $default : null;

if (is_null($object)) {
return $this->repository($main, $this->repository($default));
return $this->repository($main, $this->repository($default)) ?? $fallback;
}

return new $object($main, $default);
Expand Down

0 comments on commit b7260d4

Please sign in to comment.