-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core,graphql,serializer,spa): Component Config injection (#197)
Issue: #152
- Loading branch information
Showing
65 changed files
with
1,739 additions
and
669 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
packages/core/src/Application/Configuration/Configuration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Core\Application\Configuration; | ||
|
||
use ArrayAccess; | ||
use Illuminate\Support\Str; | ||
use LogicException; | ||
use Override; | ||
use ReflectionNamedType; | ||
use ReflectionProperty; | ||
|
||
use function array_is_list; | ||
use function is_a; | ||
use function is_array; | ||
use function is_string; | ||
use function sprintf; | ||
use function str_contains; | ||
|
||
/** | ||
* @implements ArrayAccess<string, mixed> | ||
*/ | ||
abstract class Configuration implements ArrayAccess { | ||
protected function __construct() { | ||
// empty | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $array | ||
*/ | ||
public static function __set_state(array $array): static { | ||
return new static(...$array); // @phpstan-ignore new.static (this is developer responsibility) | ||
} | ||
|
||
#[Override] | ||
public function offsetExists(mixed $offset): bool { | ||
throw new LogicException('Not supported.'); | ||
} | ||
|
||
#[Override] | ||
public function offsetGet(mixed $offset): mixed { | ||
throw new LogicException('Not supported.'); | ||
} | ||
|
||
#[Override] | ||
public function offsetSet(mixed $offset, mixed $value): void { | ||
throw new LogicException('Not supported.'); | ||
} | ||
|
||
#[Override] | ||
public function offsetUnset(mixed $offset): void { | ||
throw new LogicException('Not supported.'); | ||
} | ||
|
||
/** | ||
* @deprecated %{VERSION} Array-based config is deprecated. Please migrate to object-based config. | ||
* | ||
* @param array<array-key, mixed> $array | ||
*/ | ||
public static function fromArray(array $array): static { | ||
$properties = []; | ||
|
||
foreach ($array as $key => $value) { | ||
$property = static::fromArrayGetPropertyName($key); | ||
$properties[$property] = static::fromArrayGetPropertyValue($property, $value); | ||
} | ||
|
||
return static::__set_state($properties); | ||
} | ||
|
||
/** | ||
* @deprecated %{VERSION} | ||
*/ | ||
protected static function fromArrayGetPropertyName(int|string $property): string { | ||
if (!is_string($property)) { | ||
throw new LogicException( | ||
sprintf( | ||
'The `%s::$%s` is not a valid property name.', | ||
static::class, | ||
$property, | ||
), | ||
); | ||
} | ||
|
||
if (str_contains($property, '_')) { | ||
$property = Str::camel($property); | ||
} | ||
|
||
return $property; | ||
} | ||
|
||
/** | ||
* @deprecated %{VERSION} | ||
*/ | ||
protected static function fromArrayGetPropertyValue(string $property, mixed $value): mixed { | ||
if (is_array($value) && (!array_is_list($value) || $value === [])) { | ||
$property = new ReflectionProperty(static::class, $property); | ||
$type = $property->getType(); | ||
|
||
if ($type instanceof ReflectionNamedType && !$type->isBuiltin()) { | ||
$name = $type->getName(); | ||
|
||
if (is_a($name, self::class, true)) { | ||
$value = $name::fromArray($value); | ||
} | ||
} | ||
} | ||
|
||
return $value; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/core/src/Application/Configuration/ConfigurationResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Core\Application\Configuration; | ||
|
||
use LastDragon_ru\LaraASP\Core\Application\ConfigResolver; | ||
use LastDragon_ru\LaraASP\Core\Application\Resolver; | ||
|
||
/** | ||
* @template TConfiguration of Configuration | ||
* | ||
* @extends Resolver<TConfiguration> | ||
*/ | ||
abstract class ConfigurationResolver extends Resolver { | ||
public function __construct(ConfigResolver $config) { | ||
parent::__construct( | ||
static function () use ($config): Configuration { | ||
/** @var TConfiguration $configuration */ | ||
$configuration = $config->getInstance()->get(static::getName()); | ||
|
||
return $configuration; | ||
}, | ||
); | ||
} | ||
|
||
abstract protected static function getName(): string; | ||
|
||
/** | ||
* @return TConfiguration | ||
*/ | ||
abstract public static function getDefaultConfig(): Configuration; | ||
} |
103 changes: 103 additions & 0 deletions
103
packages/core/src/Application/Configuration/ConfigurationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Core\Application\Configuration; | ||
|
||
use LastDragon_ru\LaraASP\Core\Testing\Package\TestCase; | ||
use LogicException; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[CoversClass(Configuration::class)] | ||
final class ConfigurationTest extends TestCase { | ||
public function testSetState(): void { | ||
$expected = new ConfigurationTest_ConfigurationA(); | ||
$expected->a = 321; | ||
$expected->b = new ConfigurationTest_ConfigurationB(); | ||
$actual = ConfigurationTest_ConfigurationA::__set_state([ | ||
'a' => 321, | ||
'b' => new ConfigurationTest_ConfigurationB(), | ||
]); | ||
|
||
self::assertEquals($expected, $actual); | ||
} | ||
|
||
public function testOffsetGet(): void { | ||
self::expectException(LogicException::class); | ||
|
||
$config = new ConfigurationTest_ConfigurationA(); | ||
|
||
$config['a']; // @phpstan-ignore expr.resultUnused (for test) | ||
} | ||
|
||
public function testOffsetExists(): void { | ||
self::expectException(LogicException::class); | ||
|
||
$config = new ConfigurationTest_ConfigurationA(); | ||
|
||
isset($config['a']); // @phpstan-ignore expr.resultUnused (for test) | ||
} | ||
|
||
public function testOffsetUnset(): void { | ||
self::expectException(LogicException::class); | ||
|
||
$config = new ConfigurationTest_ConfigurationA(); | ||
|
||
unset($config['a']); | ||
} | ||
|
||
public function testOffsetSet(): void { | ||
self::expectException(LogicException::class); | ||
|
||
$config = new ConfigurationTest_ConfigurationA(); | ||
|
||
$config['a'] = 123; | ||
} | ||
|
||
public function testFromArray(): void { | ||
$expected = new ConfigurationTest_ConfigurationA(); | ||
$expected->a = 321; | ||
$expected->b = new ConfigurationTest_ConfigurationB(); | ||
$expected->b->b = true; | ||
$expected->b->bA = 'cba'; | ||
$actual = ConfigurationTest_ConfigurationA::fromArray([ | ||
'a' => 321, | ||
'b' => [ | ||
'b' => true, | ||
'b_a' => 'cba', | ||
], | ||
]); | ||
|
||
self::assertEquals($expected, $actual); | ||
} | ||
} | ||
|
||
// @phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses | ||
// @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps | ||
|
||
/** | ||
* @internal | ||
* @noinspection PhpMultipleClassesDeclarationsInOneFile | ||
*/ | ||
class ConfigurationTest_ConfigurationA extends Configuration { | ||
public function __construct( | ||
public int $a = 123, | ||
public ?ConfigurationTest_ConfigurationB $b = null, | ||
) { | ||
parent::__construct(); | ||
} | ||
} | ||
|
||
/** | ||
* @internal | ||
* @noinspection PhpMultipleClassesDeclarationsInOneFile | ||
*/ | ||
class ConfigurationTest_ConfigurationB extends Configuration { | ||
public function __construct( | ||
public bool $b = false, | ||
public string $bA = 'abc', | ||
) { | ||
parent::__construct(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.