forked from FeastFramework/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyInjector.php
More file actions
30 lines (27 loc) · 891 Bytes
/
DependencyInjector.php
File metadata and controls
30 lines (27 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
declare(strict_types=1);
use Feast\ServiceContainer\NotFoundException;
use Feast\ServiceContainer\ServiceContainer;
/**
* Returns either the ServiceContainer (if no arguments) or the requested class' object
* from the ServiceContainer.
*
* @template returned
* @param returned $className
* @psalm-param returned::class $className
* @param mixed ...$arguments
* @return returned
* @throws NotFoundException
*/
function di(?string $className = null, mixed ...$arguments): object
{
static $serviceContainer = null;
$serviceContainer ??= new ServiceContainer();
if ($className === null) {
if (isset($arguments[0]) && $arguments[0] === \Feast\Enums\ServiceContainer::CLEAR_CONTAINER) {
$serviceContainer = new ServiceContainer();
}
return $serviceContainer;
}
return $serviceContainer->get($className, ...$arguments);
}