bytic /
container
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Nip\Container\Utility; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use Nip\Container\Container as NipContainer; |
||
| 7 | use Psr\Container\ContainerInterface; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Class Container |
||
| 11 | * @package Nip\Utility |
||
| 12 | */ |
||
| 13 | class Container |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @return false|ContainerInterface|NipContainer |
||
| 17 | * @noinspection PhpDocMissingThrowsInspection |
||
| 18 | */ |
||
| 19 | public static function container($reset = false) |
||
| 20 | { |
||
| 21 | static $instance; |
||
| 22 | if ($reset || !($instance instanceof ContainerInterface)) { |
||
| 23 | /** @noinspection PhpUnhandledExceptionInspection */ |
||
| 24 | $instance = static::detect(); |
||
| 25 | } |
||
| 26 | return $instance; |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @return ContainerInterface |
||
| 31 | * @throws Exception |
||
| 32 | */ |
||
| 33 | public static function detect() |
||
| 34 | { |
||
| 35 | if (class_exists(NipContainer::class)) { |
||
| 36 | return NipContainer::getInstance(); |
||
| 37 | } |
||
| 38 | throw new Exception("No valid container found"); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param null $make |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 43 | * @return mixed |
||
| 44 | */ |
||
| 45 | public static function get($make = null) |
||
| 46 | { |
||
| 47 | if (is_null($make)) { |
||
|
0 ignored issues
–
show
|
|||
| 48 | return static::container(); |
||
| 49 | } |
||
| 50 | |||
| 51 | return static::container()->get($make); |
||
| 52 | } |
||
| 53 | } |
||
| 54 |