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

feat: add support for passing additional options to handlers

parent aa354bff
No related branches found
No related tags found
2 merge requests!5feat: remove compilation command,!4feat: remove compilation command
......@@ -13,4 +13,7 @@ interface Definition {
/** @return T */
public function value();
/** @return mixed */
public function option( string $name );
}
......@@ -15,12 +15,17 @@ class CallableDefinition implements Definition {
/** @var callable */
private $callable;
/** @var array<string, mixed> */
private array $options;
public function __construct(
callable $callable,
?string $hook = null,
array $options = []
) {
$this->callable = $callable;
$this->hook = $hook;
$this->options = $options;
}
public function hook(): ?string {
......@@ -30,4 +35,8 @@ class CallableDefinition implements Definition {
public function value() {
return $this->callable;
}
public function option( string $name ) {
return $this->options[ $name ] ?? null;
}
}
......@@ -9,18 +9,22 @@ use WPDesk\Init\Binding\Definition;
/** @implements Definition<class-string<Hookable>> */
class HookableDefinition implements Definition {
/** @var ?string */
private $hook;
private ?string $hook;
/** @var class-string<Hookable> */
private $hookable;
private string $hookable;
/** @var array<string, mixed> */
private array $options;
public function __construct(
string $hookable,
?string $hook = null,
array $options = []
) {
$this->hook = $hook;
$this->hookable = $hookable;
$this->options = $options;
}
public function hook(): ?string {
......@@ -30,4 +34,12 @@ class HookableDefinition implements Definition {
public function value() {
return $this->hookable;
}
/**
* @param string $name
* @return mixed
*/
public function option( string $name ) {
return $this->options[ $name ] ?? null;
}
}
......@@ -15,12 +15,17 @@ class UnknownDefinition implements Definition {
/** @var mixed */
private $value;
/** @var array<string, mixed> */
private array $options;
public function __construct(
$value,
?string $hook = null
?string $hook = null,
array $options = []
) {
$this->value = $value;
$this->hook = $hook;
$this->options = $options;
}
public function hook(): ?string {
......@@ -30,4 +35,8 @@ class UnknownDefinition implements Definition {
public function value() {
return $this->value;
}
public function option( string $name ) {
return $this->options[ $name ] ?? null;
}
}
......@@ -10,15 +10,15 @@ use WPDesk\Init\Binding\Definition\UnknownDefinition;
class DefinitionFactory {
public function create( $value, ?string $hook ): Definition {
public function create( $value, ?string $hook, array $options = [] ): Definition {
if ( is_string( $value ) && class_exists( $value ) && is_subclass_of( $value, Hookable::class, true ) ) {
return new HookableDefinition( $value, $hook );
return new HookableDefinition( $value, $hook, $options );
}
if ( is_callable( $value ) ) {
return new CallableDefinition( $value, $hook );
return new CallableDefinition( $value, $hook, $options );
}
return new UnknownDefinition( $value, $hook );
return new UnknownDefinition( $value, $hook, $options );
}
}
......@@ -28,9 +28,19 @@ class ArrayDefinitions implements BindingDefinitions {
private function normalize( iterable $bindings ): iterable {
foreach ( $bindings as $key => $value ) {
if ( is_array( $value ) ) {
if ( isset( $value['handler'] ) ) {
// Single item with handler
yield $this->create( $value['handler'], $key, $value );
} else {
// Multiple items
foreach ( $value as $unit ) {
if ( is_array( $unit ) && isset( $unit['handler'] ) ) {
yield $this->create( $unit['handler'], $key, $unit );
} else {
yield $this->create( $unit, $key );
}
}
}
} else {
yield $this->create( $value, $key );
}
......@@ -41,7 +51,7 @@ class ArrayDefinitions implements BindingDefinitions {
* @param mixed $value
* @param int|string $hook
*/
private function create( $value, $hook ): Definition {
return $this->factory->create( $value, is_int( $hook ) ? null : $hook );
private function create( $value, $hook, array $options = [] ): Definition {
return $this->factory->create( $value, is_int( $hook ) ? null : $hook, $options );
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment