Select Git revision
StoppableBinder.php
-
Bartek Jaskulski authored
Previously stoppable binder was working for all binders, although it only supported to signal stop execution with class-based binders. This changes the behavior, as having all binders wrapped in stoppable binder resulted in execution of all collection binders, without regard for the stop signal. This is convoluted and gets even worse, so this part of the API should be rewritten. The side effect of this change is that any callable based binder (e.g. database migrations) will not be stopped, no matter what happens. This is a serious quirk, and may be better to remove other kinds of binders than hookable or rethink the stoppable binder. Signed-off-by:
Bart Jaskulski <bjaskulski@protonmail.com>
Bartek Jaskulski authoredPreviously stoppable binder was working for all binders, although it only supported to signal stop execution with class-based binders. This changes the behavior, as having all binders wrapped in stoppable binder resulted in execution of all collection binders, without regard for the stop signal. This is convoluted and gets even worse, so this part of the API should be rewritten. The side effect of this change is that any callable based binder (e.g. database migrations) will not be stopped, no matter what happens. This is a serious quirk, and may be better to remove other kinds of binders than hookable or rethink the stoppable binder. Signed-off-by:
Bart Jaskulski <bjaskulski@protonmail.com>
StoppableBinder.php 1.14 KiB
<?php
declare(strict_types=1);
namespace WPDesk\Init\Binding\Binder;
use Psr\Container\ContainerInterface;
use WPDesk\Init\Binding\ComposableBinder;
use WPDesk\Init\Binding\StoppableBinder as Stop;
use WPDesk\Init\Binding\Definition;
use WPDesk\Init\Binding\Binder as BinderInstance;
class StoppableBinder implements ComposableBinder {
private ContainerInterface $container;
/** @var Binder */
private $binder;
private bool $should_stop = false;
public function __construct( BinderInstance $b, ContainerInterface $c ) {
$this->binder = $b;
$this->container = $c;
}
public function can_bind( Definition $def ): bool {
return $this->binder->can_bind( $def );
}
public function bind( Definition $def ): void {
if ( $this->should_stop === true ) {
return;
}
$this->binder->bind( $def );
if ( $this->can_be_stoppable( $def ) ) {
$binding = $this->container->get( $def->value() );
if ( $binding instanceof Stop && $binding->should_stop() ) {
$this->should_stop = true;
}
}
}
private function can_be_stoppable( Definition $def ): bool {
return is_string( $def->value() ) && class_exists( $def->value() );
}
}