-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerEntry.php
More file actions
82 lines (72 loc) · 2.41 KB
/
ContainerEntry.php
File metadata and controls
82 lines (72 loc) · 2.41 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
ContainerEntry.php - Part of the container project.
© - Jitesoft
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
namespace Jitesoft\Container;
use Jitesoft\Exceptions\Psr\Container\ContainerException;
use Jitesoft\Exceptions\Psr\Container\NotFoundException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
/**
* Class ContainerEntry
*
* @internal
*/
class ContainerEntry {
protected mixed $concrete;
protected bool $isSingleton;
protected bool $isCreated;
protected string $abstract;
/**
* ContainerEntry constructor.
*
* @param string $abstract Abstract to bind.
* @param mixed $concrete Concrete to bind to abstract.
* @param boolean $isSingleton If a singleton instance or not.
*
* @internal
*/
public function __construct(string $abstract,
mixed $concrete,
bool $isSingleton = false) {
$this->abstract = $abstract;
$this->concrete = $concrete;
$this->isCreated = (
(!is_string($concrete) || !class_exists($concrete))
);
$this->isSingleton = $isSingleton;
}
/**
* @param Injector $injector Injector to use for resolving.
*
* @return mixed
*
* @throws ContainerException|ContainerExceptionInterface Thrown in case the container fails in injection.
* @throws NotFoundException|NotFoundExceptionInterface Thrown in case the value is not found.
* @throws ReflectionException Thrown if instantiation failed.
* @internal
*/
public function resolve(Injector $injector): mixed {
if ($this->isCreated) {
return $this->concrete;
}
if (class_exists($this->concrete)) {
$object = $injector->create($this->concrete);
if ($this->isSingleton) {
$this->isCreated = true;
$this->concrete = $object;
}
}
if (is_callable($this->concrete)) {
$object = $injector->invoke($this->concrete);
if ($this->isSingleton) {
$this->isCreated = true;
$this->concrete = $object;
}
return $object;
}
return $object ?? null;
}
}