Repository navigation
Expand file tree
/
Copy pathObjectDefinition.php
More file actions
109 lines (86 loc) · 2.44 KB
/
Copy pathObjectDefinition.php
File metadata and controls
109 lines (86 loc) · 2.44 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Di\Definition;
use Hyperf\Di\ReflectionManager;
use Stringable;
class ObjectDefinition implements Stringable, DefinitionInterface
{
protected ?MethodInjection $constructorInjection = null;
private ?string $className;
private bool $classExists = false;
private bool $instantiable = false;
public function __construct(private string $name, ?string $className = null)
{
$this->setClassName($className ?? $name);
}
public function __toString(): string
{
return sprintf('Object[%s]', $this->getClassName());
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function setClassName(?string $className = null): void
{
$this->className = $className;
$this->updateStatusCache();
}
public function getClassName(): string
{
return $this->className ?? $this->name;
}
public function isClassExists(): bool
{
return $this->classExists;
}
public function isInstantiable(): bool
{
return $this->instantiable;
}
/**
* @return null|MethodInjection
*/
public function getConstructorInjection()
{
return $this->constructorInjection;
}
public function setConstructorInjection(MethodInjection $injection): self
{
$this->constructorInjection = $injection;
return $this;
}
public function completeConstructorInjection(MethodInjection $injection): void
{
if ($this->constructorInjection !== null) {
// Merge
$this->constructorInjection->merge($injection);
} else {
// Set
$this->constructorInjection = $injection;
}
}
private function updateStatusCache(): void
{
$className = $this->getClassName();
$this->classExists = class_exists($className) || interface_exists($className);
if (! $this->classExists) {
$this->instantiable = false;
return;
}
$this->instantiable = ReflectionManager::reflectClass($className)->isInstantiable();
}
}