1: <?php declare(strict_types=1);
2:
3: namespace Salient\Container;
4:
5: use Salient\Contract\Container\ContainerInterface;
6:
7: /**
8: * @api
9: */
10: trait RequiresContainer
11: {
12: /**
13: * In order of preference, get the given container, the global container or
14: * a standalone container
15: *
16: * @param bool $getGlobalContainer If `true` and `$container` is `null`,
17: * return the global container if it is set
18: * @param bool $createGlobalContainer If `true` and `$container` is `null`,
19: * create the global container if it is not set
20: */
21: protected static function requireContainer(
22: ?ContainerInterface $container = null,
23: bool $getGlobalContainer = true,
24: bool $createGlobalContainer = false
25: ): ContainerInterface {
26: return $container
27: ?? ($createGlobalContainer || ($getGlobalContainer && Container::hasGlobalContainer())
28: ? Container::getGlobalContainer()
29: : new Container());
30: }
31: }
32: