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: * Get the given container, optionally falling back to the global container
14: * or creating a standalone instance if no container is provided
15: */
16: protected static function requireContainer(
17: ?ContainerInterface $container = null,
18: bool $getGlobalContainer = true,
19: bool $createGlobalContainer = false
20: ): ContainerInterface {
21: return $container
22: ?? ($createGlobalContainer || ($getGlobalContainer && Container::hasGlobalContainer())
23: ? Container::getGlobalContainer()
24: : new Container());
25: }
26: }
27: