From 495d715f2a919c4a2935a222b03a32ee99b1fd75 Mon Sep 17 00:00:00 2001 From: Bart Jaskulski <bjaskulski@protonmail.com> Date: Sat, 9 Mar 2024 04:09:59 +0100 Subject: [PATCH] refactor: remove "binder", follow the spirit of wp-builder and "hookable" Signed-off-by: Bart Jaskulski <bjaskulski@protonmail.com> --- src/Binding/Binder/HookBinderBinder.php | 31 ----------------- .../Definition/HookBinderDefinition.php | 34 ------------------- src/Binding/DefinitionFactory.php | 12 ++----- src/Binding/HookBinder.php | 8 ----- src/Binding/Hookable.php | 10 ++++++ src/Binding/ObservableBinder.php | 25 -------------- src/Binding/StoppableBinder.php | 2 +- src/CommonBinding/I18n.php | 6 ++-- src/CommonBinding/RequirementsCheck.php | 2 +- src/Kernel.php | 7 +--- 10 files changed, 18 insertions(+), 119 deletions(-) delete mode 100644 src/Binding/Binder/HookBinderBinder.php delete mode 100644 src/Binding/Definition/HookBinderDefinition.php delete mode 100644 src/Binding/HookBinder.php create mode 100644 src/Binding/Hookable.php delete mode 100644 src/Binding/ObservableBinder.php diff --git a/src/Binding/Binder/HookBinderBinder.php b/src/Binding/Binder/HookBinderBinder.php deleted file mode 100644 index 888781c..0000000 --- a/src/Binding/Binder/HookBinderBinder.php +++ /dev/null @@ -1,31 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace WPDesk\Init\Binding\Binder; - -use Psr\Container\ContainerInterface; -use WPDesk\Init\Binding\Binder; -use WPDesk\Init\Binding\Definition; -use WPDesk\Init\Binding\Definition\HookBinderDefinition; -use WPDesk\Init\Binding\Definition\HookableDefinition; - -class HookBinderBinder implements Binder { - - /** @var ContainerInterface */ - private $container; - - public function __construct( ContainerInterface $c ) { - $this->container = $c; - } - - public function can_bind( Definition $def ): bool { - return $def instanceof HookBinderDefinition; - } - - public function bind( Definition $def ): void { - if ( $this->can_bind( $def ) ) { - $this->container->get( $def->value() )->bind(); - } - } -} diff --git a/src/Binding/Definition/HookBinderDefinition.php b/src/Binding/Definition/HookBinderDefinition.php deleted file mode 100644 index 5212ef3..0000000 --- a/src/Binding/Definition/HookBinderDefinition.php +++ /dev/null @@ -1,34 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace WPDesk\Init\Binding\Definition; - -use WPDesk\Init\Binding\Definition; -use WPDesk\Init\Binding\HookBinder; - -/** @implements Definition<class-string<HookBinder>> */ -class HookBinderDefinition implements Definition { - - /** @var ?string */ - private $hook; - - /** @var class-string<HookBinder> */ - private $hookable; - - public function __construct( - string $hookable, - ?string $hook = null, - ) { - $this->hook = $hook; - $this->hookable = $hookable; - } - - public function hook(): ?string { - return $this->hook; - } - - public function value() { - return $this->hookable; - } -} diff --git a/src/Binding/DefinitionFactory.php b/src/Binding/DefinitionFactory.php index 5827b89..5b66072 100644 --- a/src/Binding/DefinitionFactory.php +++ b/src/Binding/DefinitionFactory.php @@ -5,22 +5,14 @@ declare(strict_types=1); namespace WPDesk\Init\Binding; use WPDesk\Init\Binding\Definition\CallableDefinition; -use WPDesk\Init\Binding\Definition\HookBinderDefinition; use WPDesk\Init\Binding\Definition\HookableDefinition; use WPDesk\Init\Binding\Definition\UnknownDefinition; -use WPDesk\PluginBuilder\Plugin\Hookable; class DefinitionFactory { public function create( $value, ?string $hook ): Definition { - if ( is_string( $value ) && class_exists( $value ) ) { - if ( is_subclass_of( $value, Hookable::class, true ) ) { - return new HookableDefinition( $value, $hook ); - } - - if ( is_subclass_of( $value, HookBinder::class, true ) ) { - return new HookBinderDefinition( $value, $hook ); - } + if ( is_string( $value ) && class_exists( $value ) && is_subclass_of( $value, Hookable::class, true ) ) { + return new HookableDefinition( $value, $hook ); } if ( is_callable( $value ) ) { diff --git a/src/Binding/HookBinder.php b/src/Binding/HookBinder.php deleted file mode 100644 index 5b6c184..0000000 --- a/src/Binding/HookBinder.php +++ /dev/null @@ -1,8 +0,0 @@ -<?php - -namespace WPDesk\Init\Binding; - -interface HookBinder { - - public function bind(): void; -} diff --git a/src/Binding/Hookable.php b/src/Binding/Hookable.php new file mode 100644 index 0000000..3fb1e51 --- /dev/null +++ b/src/Binding/Hookable.php @@ -0,0 +1,10 @@ +<?php + +declare(strict_types=1); + +namespace WPDesk\Init\Binding; + +interface Hookable extends \WPDesk\PluginBuilder\Plugin\Hookable { + + public function hooks(): void; +} diff --git a/src/Binding/ObservableBinder.php b/src/Binding/ObservableBinder.php deleted file mode 100644 index 942ee2c..0000000 --- a/src/Binding/ObservableBinder.php +++ /dev/null @@ -1,25 +0,0 @@ -<?php -declare(strict_types=1); - -namespace WPDesk\Init\Binding; - -final class ObservableBinder implements HookBinder { - - /** @var HookBinder */ - private $binder; - - private $is_bound = false; - - public function __construct( HookBinder $binder ) { - $this->binder = $binder; - } - - public function bind(): void { - $this->binder->bind(); - $this->is_bound = true; - } - - public function is_bound(): bool { - return $this->is_bound; - } -} diff --git a/src/Binding/StoppableBinder.php b/src/Binding/StoppableBinder.php index 99957b9..daf5852 100644 --- a/src/Binding/StoppableBinder.php +++ b/src/Binding/StoppableBinder.php @@ -3,7 +3,7 @@ declare(strict_types=1); namespace WPDesk\Init\Binding; -interface StoppableBinder extends HookBinder { +interface StoppableBinder extends Hookable { public function should_stop(): bool; } diff --git a/src/CommonBinding/I18n.php b/src/CommonBinding/I18n.php index a192df0..2ab91db 100644 --- a/src/CommonBinding/I18n.php +++ b/src/CommonBinding/I18n.php @@ -2,10 +2,10 @@ namespace WPDesk\Init\CommonBinding; -use WPDesk\Init\Binding\HookBinder; +use WPDesk\Init\Binding\Hookable; use WPDesk\Init\Plugin\Plugin; -class I18n implements HookBinder { +class I18n implements Hookable { /** @var Plugin */ private $plugin; @@ -14,7 +14,7 @@ class I18n implements HookBinder { $this->plugin = $plugin; } - public function bind(): void { + public function hooks(): void { if ( did_action( 'plugins_loaded' ) ) { $this->load_textdomain(); } else { diff --git a/src/CommonBinding/RequirementsCheck.php b/src/CommonBinding/RequirementsCheck.php index a081459..d50d77b 100644 --- a/src/CommonBinding/RequirementsCheck.php +++ b/src/CommonBinding/RequirementsCheck.php @@ -20,7 +20,7 @@ class RequirementsCheck implements StoppableBinder { ); } - public function bind(): void { + public function hooks(): void { if ( $this->should_stop() ) { $this->checker->render_notices(); } diff --git a/src/Kernel.php b/src/Kernel.php index ac2495c..dd6097d 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -8,13 +8,9 @@ use DI\ContainerBuilder as DiBuilder; use Psr\Container\ContainerInterface; use WPDesk\Init\Binding\Binder\CallableBinder; use WPDesk\Init\Binding\Binder\CompositeBinder; -use WPDesk\Init\Binding\Binder\HookBinderBinder; use WPDesk\Init\Binding\Binder\HookableBinder; use WPDesk\Init\Binding\Binder\StoppableBinder; use WPDesk\Init\Binding\Loader\CompositeBindingLoader; -use WPDesk\Init\Binding\DefinitionFactory; -use WPDesk\Init\Binding\Loader\ConfigurationBindingLoader; -use WPDesk\Init\Binding\Loader\DirectoryBasedLoader; use WPDesk\Init\Configuration\Configuration; use WPDesk\Init\DependencyInjection\ContainerBuilder; use WPDesk\Init\Extension\ExtensionsSet; @@ -111,7 +107,7 @@ final class Kernel { private function prepare_driver( ContainerInterface $container ): HookDriver { $loader = new CompositeBindingLoader(); - foreach ( $this->extensions->bindings($container) as $bindings ) { + foreach ( $this->extensions->bindings( $container ) as $bindings ) { $loader->add( $bindings ); } @@ -120,7 +116,6 @@ final class Kernel { new StoppableBinder( new CompositeBinder( new HookableBinder( $container ), - new HookBinderBinder( $container ), new CallableBinder( $container ) ), $container -- GitLab