|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\SDL\Reflection; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\SDL\Contracts\Definitions\Definition; |
|
13
|
|
|
use Railt\SDL\Contracts\Definitions\TypeDefinition; |
|
14
|
|
|
use Railt\SDL\Exceptions\TypeConflictException; |
|
15
|
|
|
use Railt\SDL\Exceptions\TypeNotFoundException; |
|
16
|
|
|
use Railt\SDL\Reflection\Builder\Process\Compilable; |
|
17
|
|
|
use Railt\SDL\Runtime\CallStackInterface; |
|
18
|
|
|
use Railt\SDL\Support; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Repository |
|
22
|
|
|
*/ |
|
23
|
|
|
class Repository implements Dictionary, \Countable, \IteratorAggregate |
|
24
|
|
|
{ |
|
25
|
|
|
use Support; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array|TypeDefinition[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $definitions = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var CallStackInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $stack; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Repository constructor. |
|
39
|
|
|
* @param CallStackInterface $stack |
|
40
|
|
|
*/ |
|
41
|
283 |
|
public function __construct(CallStackInterface $stack) |
|
42
|
|
|
{ |
|
43
|
283 |
|
$this->stack = $stack; |
|
44
|
283 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param TypeDefinition $type |
|
48
|
|
|
* @param bool $force |
|
49
|
|
|
* @return Dictionary |
|
50
|
|
|
* @throws TypeConflictException |
|
51
|
|
|
*/ |
|
52
|
6577 |
|
public function register(TypeDefinition $type, bool $force = false): Dictionary |
|
53
|
|
|
{ |
|
54
|
6577 |
|
if (! $force && $this->has($type->getName())) { |
|
55
|
|
|
$error = \sprintf( |
|
56
|
|
|
'Can not declare %s, because the name %s already in use', |
|
57
|
|
|
$this->typeToString($type), |
|
58
|
|
|
$type->getName() |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
throw new TypeConflictException($error, $this->stack); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
6577 |
|
$this->definitions[$type->getName()] = $type; |
|
65
|
|
|
|
|
66
|
6577 |
|
return $this; |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $name |
|
71
|
|
|
* @param Definition|null $from |
|
72
|
|
|
* @return TypeDefinition |
|
73
|
|
|
*/ |
|
74
|
6552 |
|
public function get(string $name, Definition $from = null): TypeDefinition |
|
75
|
|
|
{ |
|
76
|
6552 |
|
if ($this->has($name)) { |
|
77
|
6552 |
|
$result = $this->definitions[$name]; |
|
78
|
|
|
|
|
79
|
6552 |
|
if ($result instanceof Compilable) { |
|
80
|
2323 |
|
$result->compile(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
6552 |
|
return $result; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$error = \sprintf('Type "%s" not found', $name); |
|
87
|
|
|
throw new TypeNotFoundException($error, $this->stack); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $type |
|
92
|
|
|
* @return iterable|TypeDefinition[] |
|
93
|
|
|
*/ |
|
94
|
|
|
public function only(string $type): iterable |
|
95
|
|
|
{ |
|
96
|
|
|
foreach ($this->definitions as $definition) { |
|
97
|
|
|
if ($definition instanceof $type) { |
|
98
|
|
|
yield $definition; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return iterable|TypeDefinition[] |
|
105
|
|
|
*/ |
|
106
|
|
|
public function all(): iterable |
|
107
|
|
|
{ |
|
108
|
|
|
yield from $this->getIterator(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $name |
|
113
|
|
|
* @return bool |
|
114
|
|
|
*/ |
|
115
|
6583 |
|
public function has(string $name): bool |
|
116
|
|
|
{ |
|
117
|
6583 |
|
return \array_key_exists($name, $this->definitions); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return \Traversable|TypeDefinition[] |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getIterator(): \Traversable |
|
124
|
|
|
{ |
|
125
|
|
|
return new \ArrayIterator(\array_values($this->definitions)); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return int |
|
130
|
|
|
*/ |
|
131
|
|
|
public function count(): int |
|
132
|
|
|
{ |
|
133
|
|
|
return \count($this->definitions); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.