| 1 |
<?php declare( strict_types=1 ); |
| 2 |
|
| 3 |
namespace lloc\Msls; |
| 4 |
|
| 5 |
use DI\Container as DiContainer; |
| 6 |
use DI\ContainerBuilder; |
| 7 |
|
| 8 |
/** |
| 9 |
* Accessor for the PHP-DI container. |
| 10 |
* |
| 11 |
* The container is built on first use and kept for the rest of the request; requests that |
| 12 |
* never ask for it never pay for it. |
| 13 |
* |
| 14 |
* @package Msls |
| 15 |
*/ |
| 16 |
final class Container { |
| 17 |
|
| 18 |
/** |
| 19 |
* The container built by self::get(), kept for the rest of the request. |
| 20 |
* |
| 21 |
* @var ?DiContainer |
| 22 |
*/ |
| 23 |
private static ?DiContainer $container = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* @throws \Exception If the container cannot be built. |
| 27 |
*/ |
| 28 |
public static function get(): DiContainer { |
| 29 |
if ( null === self::$container ) { |
| 30 |
$builder = new ContainerBuilder(); |
| 31 |
$builder->addDefinitions( Plugin::plugin_dir_path( 'config.php' ) ); |
| 32 |
|
| 33 |
self::$container = $builder->build(); |
| 34 |
} |
| 35 |
|
| 36 |
return self::$container; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Drops the built container, so the next call to self::get() builds a new one. |
| 41 |
*/ |
| 42 |
public static function reset(): void { |
| 43 |
self::$container = null; |
| 44 |
} |
| 45 |
} |
| 46 |
|