Skip to content

Instantly share code, notes, and snippets.

@Preciousomonze
Last active May 22, 2023 17:48
Show Gist options
  • Save Preciousomonze/fe8ff6022e35a6abcea4bd6bc30842a7 to your computer and use it in GitHub Desktop.
Save Preciousomonze/fe8ff6022e35a6abcea4bd6bc30842a7 to your computer and use it in GitHub Desktop.
Trying to use Containers in a WP way
<?php
/**
* Main Plugin
* @version 1.0.0
*/
namespace Backcourt;
use Backcourt\Vendor\League\Container\Container as Container;
defined( 'ABSPATH' ) || exit;
/**
* Main plugin class.
*/
final class Plugin {
/**
* Plugin version.
*
* @var string
*/
const VERSION = '1.0.0';
/**
* Holds an instance of the class.
*
* @var Plugin
*/
private static $instance;
/**
* Holds an instance of the Container.
*
* @var Container
*/
private $container;
/**
* The list of Admin classes to register.
*
* @var string[]
*/
private $service_providers = array(
'Admin\Products\Metabox',
'Admin\Products\Input'
);
/**
* Constructor.
*/
private function __construct() {
// Initialize dependency injection.
$this->container = new Container();
foreach ( $this->service_providers as $service_provider_class ) {
// There may be occasions where you wish the service to be
// the same instance each time you retrieve it. There are two ways to achieve this, declare it as shared
$this->container->share( $service_provider_class );
}
}
/**
* init.
*/
public function init() {
$this->container->get('Admin\Products\Metabox')->init();
}
/**
* Shortcut to get a class from the container.
*/
public function get( $class ) {
return $this->container->get( $class );
}
/**
* Gets the main instance.
*
* Ensures only one instance can be loaded.
*
* @return \Backcourt\Plugin
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
<?php
/**
* Main Plugin
* @version 1.0.0
*/
namespace Backcourt;
use Backcourt\Vendor\League\Container\Container as Container;
use Backcourt\Vendor\League\Container\Definition\Definition as BaseDefinition;
defined( 'ABSPATH' ) || exit;
/**
* Main plugin class.
*/
final class Plugin extends BaseDefinition {
/**
* Plugin version.
*
* @var string
*/
const VERSION = '1.0.0';
/**
* Holds an instance of the class.
*
* @var Plugin
*/
private static $instance;
/**
* Holds an instance of the Container.
*
* @var Container
*/
private $container;
/**
* The standard method that we use for dependency injection.
*/
public const ATTACH_HOOKS = 'init';
/**
* The list of Admin classes to register.
*
* @var string['class_name'=>'method']
*/
private $service_providers = array(
'Admin\Products\Metabox'=>'init',
'Admin\Products\Input'=>'init',
);
/**
* Constructor.
*/
private function __construct() {
// Initialize dependency injection.
$this->container = new Container();
foreach ( $this->service_providers as $service_provider_class => $method ) {
// There may be occasions where you wish the service to be
// the same instance each time you retrieve it. There are two ways to achieve this, declare it as shared
// $this->container->share( $service_provider_class );
$this->resolveClass( $service_provider_class );
}
}
/**
* init.
*/
public function init() {
//$this->container->get('Admin\Products\Metabox')->init();
}
/**
* Shortcut to get a class from the container.
*/
public function get( $class ) {
return $this->container->get( $class );
}
/**
* Resolve a class using method injection instead of constructor injection.
*
* @param string $concrete The concrete to instantiate.
*
* @return object
*/
protected function resolveClass( string $concrete ) {
$resolved = $this->resolveArguments( $this->arguments );
$concrete = new $concrete();
// Constructor injection causes backwards compatibility problems
// so we will rely on method injection via an internal method.
if ( method_exists( $concrete, static::ATTACH_HOOKS ) ) {
call_user_func_array( array( $concrete, static::ATTACH_HOOKS ), $resolved );
}
return $concrete;
}
/**
* Gets the main instance.
*
* Ensures only one instance can be loaded.
*
* @return \Backcourt\Plugin
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment