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 { ...@@ -13,4 +13,7 @@ interface Definition {
/** @return T */ /** @return T */
public function value(); public function value();
/** @return mixed */
public function option( string $name );
} }
...@@ -15,12 +15,17 @@ class CallableDefinition implements Definition { ...@@ -15,12 +15,17 @@ class CallableDefinition implements Definition {
/** @var callable */ /** @var callable */
private $callable; private $callable;
/** @var array<string, mixed> */
private array $options;
public function __construct( public function __construct(
callable $callable, callable $callable,
?string $hook = null, ?string $hook = null,
array $options = []
) { ) {
$this->callable = $callable; $this->callable = $callable;
$this->hook = $hook; $this->hook = $hook;
$this->options = $options;
} }
public function hook(): ?string { public function hook(): ?string {
...@@ -30,4 +35,8 @@ class CallableDefinition implements Definition { ...@@ -30,4 +35,8 @@ class CallableDefinition implements Definition {
public function value() { public function value() {
return $this->callable; return $this->callable;
} }
public function option( string $name ) {
return $this->options[ $name ] ?? null;
}
} }
...@@ -9,18 +9,22 @@ use WPDesk\Init\Binding\Definition; ...@@ -9,18 +9,22 @@ use WPDesk\Init\Binding\Definition;
/** @implements Definition<class-string<Hookable>> */ /** @implements Definition<class-string<Hookable>> */
class HookableDefinition implements Definition { class HookableDefinition implements Definition {
/** @var ?string */ private ?string $hook;
private $hook;
/** @var class-string<Hookable> */ /** @var class-string<Hookable> */
private $hookable; private string $hookable;
/** @var array<string, mixed> */
private array $options;
public function __construct( public function __construct(
string $hookable, string $hookable,
?string $hook = null, ?string $hook = null,
array $options = []
) { ) {
$this->hook = $hook; $this->hook = $hook;
$this->hookable = $hookable; $this->hookable = $hookable;
$this->options = $options;
} }
public function hook(): ?string { public function hook(): ?string {
...@@ -30,4 +34,12 @@ class HookableDefinition implements Definition { ...@@ -30,4 +34,12 @@ class HookableDefinition implements Definition {
public function value() { public function value() {
return $this->hookable; 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 { ...@@ -15,12 +15,17 @@ class UnknownDefinition implements Definition {
/** @var mixed */ /** @var mixed */
private $value; private $value;
/** @var array<string, mixed> */
private array $options;
public function __construct( public function __construct(
$value, $value,
?string $hook = null ?string $hook = null,
array $options = []
) { ) {
$this->value = $value; $this->value = $value;
$this->hook = $hook; $this->hook = $hook;
$this->options = $options;
} }
public function hook(): ?string { public function hook(): ?string {
...@@ -30,4 +35,8 @@ class UnknownDefinition implements Definition { ...@@ -30,4 +35,8 @@ class UnknownDefinition implements Definition {
public function value() { public function value() {
return $this->value; return $this->value;
} }
public function option( string $name ) {
return $this->options[ $name ] ?? null;
}
} }
...@@ -10,15 +10,15 @@ use WPDesk\Init\Binding\Definition\UnknownDefinition; ...@@ -10,15 +10,15 @@ use WPDesk\Init\Binding\Definition\UnknownDefinition;
class DefinitionFactory { 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 ) ) { 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 ) ) { 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 { ...@@ -28,9 +28,19 @@ class ArrayDefinitions implements BindingDefinitions {
private function normalize( iterable $bindings ): iterable { private function normalize( iterable $bindings ): iterable {
foreach ( $bindings as $key => $value ) { foreach ( $bindings as $key => $value ) {
if ( is_array( $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 ) { foreach ( $value as $unit ) {
if ( is_array( $unit ) && isset( $unit['handler'] ) ) {
yield $this->create( $unit['handler'], $key, $unit );
} else {
yield $this->create( $unit, $key ); yield $this->create( $unit, $key );
} }
}
}
} else { } else {
yield $this->create( $value, $key ); yield $this->create( $value, $key );
} }
...@@ -41,7 +51,7 @@ class ArrayDefinitions implements BindingDefinitions { ...@@ -41,7 +51,7 @@ class ArrayDefinitions implements BindingDefinitions {
* @param mixed $value * @param mixed $value
* @param int|string $hook * @param int|string $hook
*/ */
private function create( $value, $hook ): Definition { private function create( $value, $hook, array $options = [] ): Definition {
return $this->factory->create( $value, is_int( $hook ) ? null : $hook ); 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