Skip to content
Snippets Groups Projects
Verified Commit 77f64d18 authored by Bartek Jaskulski's avatar Bartek Jaskulski
Browse files

perf: group handlers by hook to improve performance

parent b7ed8b86
No related branches found
No related tags found
2 merge requests!5feat: remove compilation command,!4feat: remove compilation command
<?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;
}
}
<?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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment