Skip to content
Snippets Groups Projects
Select Git revision
  • 973da1abe1280710439feda64280263f10c7064c
  • main default protected
  • v0.10
  • 0.10.6
  • 0.10.5
  • 0.10.4
  • 0.10.3
  • 0.10.2
  • 0.10.1
  • 0.10.0
  • 0.9.1
  • 0.9.0
12 results

di-functions.php

Blame
  • di-functions.php 4.46 KiB
    <?php
    // phpcs:disable WordPress.NamingConventions.ValidVariableName
    /**
     * This is a vendored set of php-di/php-di helper functions. We place it directly in our library to improve experience when using DI along with code scoping, which defy composer's file autoloading.
     *
     * @see https://github.com/PHP-DI/PHP-DI/blob/master/src/functions.php
     */
    
    declare(strict_types=1);
    
    namespace WPDesk\Init\DI;
    
    use DI\Definition\ArrayDefinitionExtension;
    use DI\Definition\EnvironmentVariableDefinition;
    use DI\Definition\Helper\AutowireDefinitionHelper;
    use DI\Definition\Helper\CreateDefinitionHelper;
    use DI\Definition\Helper\FactoryDefinitionHelper;
    use DI\Definition\Reference;
    use DI\Definition\StringDefinition;
    use DI\Definition\ValueDefinition;
    
    if ( ! function_exists( 'WPDesk\Init\DI\value' ) ) {
    	/**
    	 * Helper for defining a value.
    	 */
    	function value( mixed $value ): ValueDefinition {
    		return new ValueDefinition( $value );
    	}
    }
    
    if ( ! function_exists( 'WPDesk\Init\DI\create' ) ) {
    	/**
    	 * Helper for defining an object.
    	 *
    	 * @param string|null $className Class name of the object.
    	 *                               If null, the name of the entry (in the container) will be used as class name.
    	 */
    	function create( ?string $className = null ): CreateDefinitionHelper {
    		return new CreateDefinitionHelper( $className );
    	}
    }
    
    if ( ! function_exists( 'WPDesk\Init\DI\autowire' ) ) {
    	/**
    	 * Helper for autowiring an object.
    	 *
    	 * @param string|null $className Class name of the object.
    	 *                               If null, the name of the entry (in the container) will be used as class name.
    	 */
    	function autowire( ?string $className = null ): AutowireDefinitionHelper {
    		return new AutowireDefinitionHelper( $className );
    	}
    }
    
    if ( ! function_exists( 'WPDesk\Init\DI\factory' ) ) {
    	/**
    	 * Helper for defining a container entry using a factory function/callable.
    	 *
    	 * @param callable|array|string $factory The factory is a callable that takes the container as parameter
    	 *        and returns the value to register in the container.
    	 */
    	function factory( $factory ): FactoryDefinitionHelper {
    		return new FactoryDefinitionHelper( $factory );
    	}
    }
    
    if ( ! function_exists( 'WPDesk\Init\DI\decorate' ) ) {
    	/**
    	 * Decorate the previous definition using a callable.
    	 *