| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP; |
| 4 |
|
| 5 |
class Container |
| 6 |
{ |
| 7 |
public $providers = []; |
| 8 |
public $classes = []; |
| 9 |
private $event_handler; |
| 10 |
|
| 11 |
private static $instance; |
| 12 |
|
| 13 |
protected function __construct() |
| 14 |
{ |
| 15 |
} |
| 16 |
|
| 17 |
protected function __clone() |
| 18 |
{ |
| 19 |
} |
| 20 |
|
| 21 |
public function __wakeup() |
| 22 |
{ |
| 23 |
} |
| 24 |
|
| 25 |
public static function getInstance() |
| 26 |
{ |
| 27 |
if (!(self::$instance instanceof self)) { |
| 28 |
self::$instance = new self; |
| 29 |
} |
| 30 |
|
| 31 |
return self::$instance; |
| 32 |
} |
| 33 |
|
| 34 |
public function setupServiceProviders($is_pro = false) |
| 35 |
{ |
| 36 |
$this->event_handler = new EventHandler(); |
| 37 |
$potential_classes = []; |
| 38 |
|
| 39 |
if (true === $is_pro) { |
| 40 |
$potential_classes = ['ImportWP\Pro\ServiceProvider']; |
| 41 |
} else { |
| 42 |
$potential_classes = ['ImportWP\Free\ServiceProvider']; |
| 43 |
} |
| 44 |
|
| 45 |
// Addons |
| 46 |
$potential_classes[] = 'ImportWPAddon\YoastSEO\ServiceProvider'; |
| 47 |
$potential_classes[] = 'ImportWPAddon\XLSX\ServiceProvider'; |
| 48 |
$potential_classes[] = 'ImportWPAddon\WooCommerce\ServiceProvider'; |
| 49 |
|
| 50 |
foreach ($potential_classes as $class) { |
| 51 |
$this->maybeAddProvider($class); |
| 52 |
} |
| 53 |
|
| 54 |
if (!empty($this->providers)) { |
| 55 |
foreach ($this->providers as $provider) { |
| 56 |
$vars = get_object_vars($provider); |
| 57 |
foreach ($vars as $prop => $var) { |
| 58 |
if (!isset($this->classes[$prop])) { |
| 59 |
$this->classes[$prop] = $var; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public function maybeAddProvider($class) |
| 67 |
{ |
| 68 |
if (class_exists($class)) { |
| 69 |
$this->providers[$class] = new $class($this->event_handler); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function get($id) |
| 74 |
{ |
| 75 |
if (empty($this->classes)) { |
| 76 |
$this->setupServiceProviders(); |
| 77 |
} |
| 78 |
|
| 79 |
if (array_key_exists($id, $this->classes)) { |
| 80 |
return $this->classes[$id]; |
| 81 |
} |
| 82 |
|
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
protected function _registerProvider($provider) |
| 87 |
{ |
| 88 |
} |
| 89 |
} |
| 90 |
|