diff --git a/src/Binding/Definition/DefinitionCollection.php b/src/Binding/Definition/DefinitionCollection.php new file mode 100644 index 0000000000000000000000000000000000000000..ed67b92d4f5daf1c51d58687077d76c6c3703154 --- /dev/null +++ b/src/Binding/Definition/DefinitionCollection.php @@ -0,0 +1,44 @@ +<?php + +declare(strict_types=1); + +namespace WPDesk\Init\Binding\Definition; + +use WPDesk\Init\Binding\Definition; + +/** @implements Definition<mixed> */ +class DefinitionCollection implements Definition { + + /** @var ?string */ + private $hook; + + /** @var Definition[] */ + private $defs; + + /** @var array<string, mixed> */ + private array $options; + + public function __construct( + ?string $hook = null, + array $options = [] + ) { + $this->hook = $hook; + $this->options = $options; + } + + public function hook(): ?string { + return $this->hook; + } + + public function value() { + yield from $this->defs; + } + + public function add( Definition $def ) { + $this->defs[] = $def; + } + + public function option( string $name ) { + return $this->options[ $name ] ?? null; + } +} diff --git a/src/Binding/Loader/ClusteredLoader.php b/src/Binding/Loader/ClusteredLoader.php new file mode 100644 index 0000000000000000000000000000000000000000..e290fa4f367f60a62378651dce65d194716f31f0 --- /dev/null +++ b/src/Binding/Loader/ClusteredLoader.php @@ -0,0 +1,34 @@ +<?php +declare( strict_types=1 ); + +namespace WPDesk\Init\Binding\Loader; + +use WPDesk\Init\Binding\Definition\DefinitionCollection; + +final class ClusteredLoader implements BindingDefinitions { + + private BindingDefinitions $loader; + + public function __construct( BindingDefinitions $loader ) { + $this->loader = $loader; + } + + public function load(): iterable { + $definitions = []; + foreach ( $this->loader->load() as $def ) { + if ( $def->hook() === null ) { + yield $def; + continue; + } + + if ( ! isset( $definitions[ $def->hook() ] ) ) { + $collection = new DefinitionCollection( $def->hook() ); + $definitions[ $def->hook() ] = $collection; + } + + $definitions[ $def->hook() ]->add( $def ); + } + + yield from $definitions; + } +}