Skip to content
Snippets Groups Projects
Select Git revision
  • 309d1742e94de48ea6510b5b835d2386153f4743
  • 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

PluginInit.php

Blame
  • PluginInit.php 3.52 KiB
    <?php
    declare( strict_types=1 );
    
    namespace WPDesk\Init;
    
    use DI\Container;
    use DI\ContainerBuilder as DiBuilder;
    use WPDesk\Init\Bundle\Bundle;
    use WPDesk\Init\Configuration\Configuration;
    use WPDesk\Init\DependencyInjection\ContainerBuilder;
    use WPDesk\Init\Dumper\PhpFileDumper;
    use WPDesk\Init\HookDriver\HookDriver;
    use WPDesk\Init\HookDriver\CallbackDriver;
    use WPDesk\Init\Loader\PhpFileLoader;
    
    /**
     * Plugin builder class responsible for our initialization system.
     *
     * @api
     */
    final class PluginInit {
    	/** @var Bundle[] */
    	private $bundles = [];
    
    	/** @var string|null Plugin filename. */
    	private $filename;
    
    	/** @var Configuration */
    	private $config;
    
    	/** @var PhpFileLoader */
    	private $loader;
    
    	/** @var HookDriver */
    	private $driver;
    
    	/** @var HeaderParser */
    	private $parser;
    
    	/**
    	 * @param string|array|Configuration $config
    	 */
    	public function __construct(
    		$config,
    		?HookDriver $driver = null,
    		?HeaderParser $parser = null
    	) {
    		$this->loader = new PhpFileLoader();
    		if ( $config instanceof Configuration ) {
    			$this->config = $config;
    		} elseif ( \is_array( $config ) ) {
    			$this->config = new Configuration( $config );
    		} elseif ( \is_string( $config ) ) {
    			$this->config = new Configuration( $this->loader->load( $config ) );
    		} else {
    			throw new \InvalidArgumentException( sprintf( 'Configuration must be either path to configuration file, array of configuration data or %s instance', Configuration::class ) );
    		}
    
    		$this->driver = $driver ?? new CallbackDriver();
    		$this->parser = $parser ?? new DefaultHeaderParser();
    	}
    
    	/**
    	 * Build and return a plugin.
    	 *
    	 * @return Plugin|null If plugin failed to build (e.g. requirements are not fulfilled),
    	 * initialization process returns null. There are no exceptions thrown on foreseeable issues
    	 * as those cases should be handled gracefully, by displaying admin notice if possible and
    	 * preventing to initialize plugin functions without disrupting a website.
    	 */