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.
misc: introduce functions container to wrap definition handling
- Loading branch information
Showing
11 changed files
with
257 additions
and
126 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Definition\Exception; | ||
|
||
use CuyZ\Valinor\Definition\FunctionDefinition; | ||
use RuntimeException; | ||
|
||
/** @internal */ | ||
final class CallbackNotFound extends RuntimeException | ||
{ | ||
public function __construct(FunctionDefinition $function) | ||
{ | ||
parent::__construct( | ||
"The callback associated to `{$function->signature()}` could not be found.", | ||
1647523495 | ||
); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Definition\Exception; | ||
|
||
use RuntimeException; | ||
|
||
/** @internal */ | ||
final class FunctionNotFound extends RuntimeException | ||
{ | ||
/** | ||
* @param string|int $key | ||
*/ | ||
public function __construct($key) | ||
{ | ||
parent::__construct( | ||
"The function `$key` was not found.", | ||
1647523444 | ||
); | ||
} | ||
} |
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,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Definition; | ||
|
||
use CuyZ\Valinor\Definition\Exception\CallbackNotFound; | ||
use CuyZ\Valinor\Definition\Exception\FunctionNotFound; | ||
use CuyZ\Valinor\Definition\Repository\FunctionDefinitionRepository; | ||
use IteratorAggregate; | ||
use Traversable; | ||
|
||
use function array_keys; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @implements IteratorAggregate<FunctionDefinition> | ||
*/ | ||
final class FunctionsContainer implements IteratorAggregate | ||
{ | ||
private FunctionDefinitionRepository $functionDefinitionRepository; | ||
|
||
/** @var array<callable> */ | ||
private array $callables; | ||
|
||
/** @var array<array{definition: FunctionDefinition, callback: callable}> */ | ||
private array $functions = []; | ||
|
||
/** | ||
* @param array<callable> $callables | ||
*/ | ||
public function __construct(FunctionDefinitionRepository $functionDefinitionRepository, array $callables) | ||
{ | ||
$this->functionDefinitionRepository = $functionDefinitionRepository; | ||
$this->callables = $callables; | ||
} | ||
|
||
/** | ||
* @param string|int $key | ||
*/ | ||
public function has($key): bool | ||
{ | ||
return isset($this->callables[$key]); | ||
} | ||
|
||
/** | ||
* @param string|int $key | ||
*/ | ||
public function get($key): FunctionDefinition | ||
{ | ||
if (! $this->has($key)) { | ||
throw new FunctionNotFound($key); | ||
} | ||
|
||
return $this->function($key)['definition']; | ||
} | ||
|
||
public function callback(FunctionDefinition $function): callable | ||
{ | ||
foreach ($this->functions as $data) { | ||
if ($function === $data['definition']) { | ||
return $data['callback']; | ||
} | ||
} | ||
|
||
throw new CallbackNotFound($function); | ||
} | ||
|
||
public function getIterator(): Traversable | ||
{ | ||
foreach (array_keys($this->callables) as $key) { | ||
yield $key => $this->function($key)['definition']; | ||
} | ||
} | ||
|
||
/** | ||
* @param string|int $key | ||
* @return array{definition: FunctionDefinition, callback: callable} | ||
*/ | ||
private function function($key): array | ||
{ | ||
/** @infection-ignore-all */ | ||
return $this->functions[$key] ??= [ | ||
'callback' => $this->callables[$key], | ||
'definition' => $this->functionDefinitionRepository->for($this->callables[$key]), | ||
]; | ||
} | ||
} |
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
Oops, something went wrong.