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

refactor: remove "binder", follow the spirit of wp-builder and "hookable"

parent 04d3bf0b
No related branches found
No related tags found
2 merge requests!3improve into wp-hook and some additional unfinished things,!21.x
<?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();
}
}
}
<?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;
}
}
......@@ -5,24 +5,16 @@ 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 ) ) {
if ( is_string( $value ) && class_exists( $value ) && 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_callable( $value ) ) {
return new CallableDefinition( $value, $hook );
}
......
<?php
namespace WPDesk\Init\Binding;
interface HookBinder {
public function bind(): void;
}
<?php
declare(strict_types=1);
namespace WPDesk\Init\Binding;
interface Hookable extends \WPDesk\PluginBuilder\Plugin\Hookable {
public function hooks(): void;
}
<?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;
}
}
......@@ -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;
}
......@@ -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 {
......
......@@ -20,7 +20,7 @@ class RequirementsCheck implements StoppableBinder {
);
}
public function bind(): void {
public function hooks(): void {
if ( $this->should_stop() ) {
$this->checker->render_notices();
}
......
......@@ -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;
......@@ -120,7 +116,6 @@ final class Kernel {
new StoppableBinder(
new CompositeBinder(
new HookableBinder( $container ),
new HookBinderBinder( $container ),
new CallableBinder( $container )
),
$container
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment