Skip to content
Snippets Groups Projects
Select Git revision
  • 3af777915d3e224dc6855fdb226d577b0e442328
  • master default protected
  • devel
  • feature/add-escaping-to-templates
  • feature/add-priority-sorting
  • 3.3.3
  • 3.3.2
  • 3.3.1
  • 3.3.0
  • 3.2.1
  • 3.2.0
  • 3.1.0
  • 3.0.0
  • 2.4.12
  • 2.4.11
  • 2.4.10
  • 2.4.9
  • 2.4.8
  • 2.4.7
  • 2.4.6
  • 2.4.5
  • 2.4.4
  • 2.4.2
  • 2.4.1
  • 2.4.0
25 results

changelog.txt

Blame
  • To find the state of this project's repository at the time of any of these versions, check out the tags.
    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() );
    	}
    }