Skip to content

Commit

Permalink
Merge pull request #11 from Laravel-Lang/1.x
Browse files Browse the repository at this point in the history
Static class replaced with Laravel facade
  • Loading branch information
andrey-helldar authored Jun 10, 2024
2 parents db3a713 + b934114 commit 41f0bf7
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 77 deletions.
86 changes: 9 additions & 77 deletions src/Facades/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,19 @@

namespace LaravelLang\Config\Facades;

use Illuminate\Config\Repository;
use Illuminate\Support\Facades\Facade;
use LaravelLang\Config\Data\HiddenData;
use LaravelLang\Config\Data\NonPushableData;
use LaravelLang\Config\Data\PushableData;
use LaravelLang\Config\Data\RouteNameData;
use LaravelLang\Config\Data\RoutesData;
use LaravelLang\Config\Data\SharedData;
use LaravelLang\Config\Data\SmartPunctuationData;
use LaravelLang\Config\Enums\Name;
use LaravelLang\Config\Services\Config as ConfigService;

class Config
/**
* @method static HiddenData hidden()
* @method static SharedData shared()
*/
class Config extends Facade
{
public static function shared(): SharedData
protected static function getFacadeAccessor(): string
{
return new SharedData(
inline : (bool) static::value(Name::Shared, 'inline'),
align : (bool) static::value(Name::Shared, 'align'),
aliases : static::value(Name::Shared, 'aliases', object: NonPushableData::class),
punctuation: static::smartPunctuation(),
routes : static::routes(),
);
}

public static function hidden(): HiddenData
{
return new HiddenData(
plugins : static::value(Name::Hidden, 'plugins', object: PushableData::class),
packages: static::value(Name::Hidden, 'packages', object: PushableData::class),
map : static::value(Name::Hidden, 'map', object: NonPushableData::class),
);
}

protected static function smartPunctuation(): SmartPunctuationData
{
return new SmartPunctuationData(
enabled: static::value(Name::Shared, 'smart_punctuation.enable'),
common : static::value(Name::Shared, 'smart_punctuation.common'),

locales: static::value(
Name::Shared,
'smart_punctuation.locales',
'smart_punctuation.common',
NonPushableData::class
),
);
}

protected static function routes(): RoutesData
{
return new RoutesData(
names: new RouteNameData(
parameter: static::value(Name::Shared, 'routes.names.parameter'),
header : static::value(Name::Shared, 'routes.names.header'),
cookie : static::value(Name::Shared, 'routes.names.cookie'),
session : static::value(Name::Shared, 'routes.names.session'),
)
);
}

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

if (is_null($object)) {
return static::repository($main, static::repository($default));
}

return new $object($main, $default);
}

protected static function repository(?string $key, ?string $default = null): mixed
{
if (is_null($key)) {
return null;
}

if (! is_null($default)) {
$default = static::repository($default);
}

return app(Repository::class)->get($key, $default);
return ConfigService::class;
}
}
94 changes: 94 additions & 0 deletions src/Services/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Config\Services;

use Illuminate\Config\Repository;
use LaravelLang\Config\Data\HiddenData;
use LaravelLang\Config\Data\NonPushableData;
use LaravelLang\Config\Data\PushableData;
use LaravelLang\Config\Data\RouteNameData;
use LaravelLang\Config\Data\RoutesData;
use LaravelLang\Config\Data\SharedData;
use LaravelLang\Config\Data\SmartPunctuationData;
use LaravelLang\Config\Enums\Name;

class Config
{
public function __construct(
protected Repository $appConfig
) {}

public function shared(): SharedData
{
return new SharedData(
inline : (bool) $this->value(Name::Shared, 'inline'),
align : (bool) $this->value(Name::Shared, 'align'),
aliases : $this->value(Name::Shared, 'aliases', object: NonPushableData::class),
punctuation: $this->smartPunctuation(),
routes : $this->routes(),
);
}

public function hidden(): HiddenData
{
return new HiddenData(
plugins : $this->value(Name::Hidden, 'plugins', object: PushableData::class),
packages: $this->value(Name::Hidden, 'packages', object: PushableData::class),
map : $this->value(Name::Hidden, 'map', object: NonPushableData::class),
);
}

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

locales: $this->value(
Name::Shared,
'smart_punctuation.locales',
'smart_punctuation.common',
NonPushableData::class
),
);
}

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'),
)
);
}

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

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

return new $object($main, $default);
}

protected function repository(?string $key, ?string $default = null): mixed
{
if (is_null($key)) {
return null;
}

if (! is_null($default)) {
$default = $this->repository($default);
}

return $this->appConfig->get($key, $default);
}
}

0 comments on commit 41f0bf7

Please sign in to comment.