forked from CuyZ/Valinor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
843 additions
and
71 deletions.
There are no files selected for viewing
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
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
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
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Library; | ||
|
||
use CuyZ\Valinor\Definition\FunctionsContainer; | ||
use CuyZ\Valinor\Definition\Repository\FunctionDefinitionRepository; | ||
use CuyZ\Valinor\Normalizer\FunctionsCheckerNormalizer; | ||
use CuyZ\Valinor\Normalizer\Normalizer; | ||
use CuyZ\Valinor\Normalizer\RecursiveNormalizer; | ||
|
||
/** @internal */ | ||
final class NormalizerContainer | ||
{ | ||
private SharedContainer $shared; | ||
|
||
/** @var array<class-string, object> */ | ||
private array $services = []; | ||
|
||
/** @var array<class-string, callable(): object> */ | ||
private array $factories; | ||
|
||
public function __construct(NormalizerSettings $settings) | ||
{ | ||
$this->shared = SharedContainer::new($settings->cache); | ||
$this->factories = [ | ||
Normalizer::class => function () use ($settings) { | ||
$functions = new FunctionsContainer( | ||
$this->shared->get(FunctionDefinitionRepository::class), | ||
$settings->sortedHandlers() | ||
); | ||
|
||
$normalizer = new RecursiveNormalizer($functions); | ||
|
||
return new FunctionsCheckerNormalizer($normalizer, $functions); | ||
}, | ||
]; | ||
} | ||
|
||
public function normalizer(): Normalizer | ||
{ | ||
return $this->get(Normalizer::class); | ||
} | ||
|
||
/** | ||
* @template T of object | ||
* @param class-string<T> $name | ||
* @return T | ||
*/ | ||
private function get(string $name): object | ||
{ | ||
return $this->services[$name] ??= call_user_func($this->factories[$name]); // @phpstan-ignore-line | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Library; | ||
|
||
use Psr\SimpleCache\CacheInterface; | ||
|
||
use function krsort; | ||
|
||
/** @internal */ | ||
final class NormalizerSettings | ||
{ | ||
/** @var CacheInterface<mixed>|null */ | ||
public ?CacheInterface $cache = null; | ||
|
||
/** @var array<int, list<callable>> */ | ||
public array $handlers = []; | ||
|
||
/** | ||
* @return array<callable> | ||
*/ | ||
public function sortedHandlers(): array | ||
{ | ||
krsort($this->handlers); | ||
|
||
$callables = []; | ||
|
||
foreach ($this->handlers as $list) { | ||
$callables = [...$callables, ...$list]; | ||
} | ||
|
||
return $callables; | ||
} | ||
} |
Oops, something went wrong.