Skip to content
Snippets Groups Projects
Select Git revision
  • 98d51c9d03256fb1bb5459eb05e513efb73effac
  • master default protected
  • bugfix/wordpress-review
  • bugfix/prevent-error-notice
  • remove-arrow
  • feature/update-message
  • feature/minimum-plugin-version-check-demo1
  • feature/plugin-name
  • 3.7.1
  • 3.7.0
  • 3.6.3
  • 3.6.2
  • 3.6.1
  • 3.6.0
  • 3.6.0-beta3
  • 3.6.0-beta2
  • 3.6.0-beta1
  • 3.5.2
  • 3.5.1
  • 3.5.0
  • 3.4.0
  • 3.3.0
  • 3.2.8
  • 3.2.7
  • 3.2.6
  • 3.2.5
  • 3.2.4
  • 3.2.3
28 results

Basic_Requirement_Checker.php

Blame
  • ArrayDefinitions.php 1.51 KiB
    <?php
    declare( strict_types=1 );
    
    namespace WPDesk\Init\Binding\Loader;
    
    use WPDesk\Init\Binding\Definition;
    use WPDesk\Init\Binding\DefinitionFactory;
    use WPDesk\Init\Configuration\ReadableConfig;
    use WPDesk\Init\Plugin\Plugin;
    
    class ArrayDefinitions implements BindingDefinitions {
    
    	private array $bindings;
    
    	private DefinitionFactory $factory;
    
    	public function __construct( array $bindings, ?DefinitionFactory $factory = null ) {
    		$this->bindings = $bindings;
    		$this->factory  = $factory ?? new DefinitionFactory();
    	}
    
    	public function load(): iterable {
    		yield from $this->normalize( $this->bindings );
    	}
    
    	/**
    	 * @param iterable<string,array> $bindings
    	 *
    	 * @return iterable<Definition>
    	 */
    	private function normalize( iterable $bindings ): iterable {
    		foreach ( $bindings as $key => $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 ) {
    						if ( is_array( $unit ) && isset( $unit['handler'] ) ) {
    							yield $this->create( $unit['handler'], $key, $unit );
    						} else {
    							yield $this->create( $unit, $key );
    						}
    					}
    				}
    			} else {
    				yield $this->create( $value, $key );
    			}
    		}
    	}
    
    	/**
    	 * @param mixed $value
    	 * @param int|string $hook
    	 */
    	private function create( $value, $hook, array $options = [] ): Definition {
    		return $this->factory->create( $value, is_int( $hook ) ? null : $hook, $options );
    	}
    }