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\Reflection; |
11
|
|
|
|
12
|
|
|
use Railt\Io\File; |
13
|
|
|
use Railt\Io\Readable; |
14
|
|
|
use Railt\Reflection\Common\Serializable; |
15
|
|
|
use Railt\Reflection\Contracts\Definition\TypeDefinition; |
16
|
|
|
use Railt\Reflection\Contracts\Dictionary; |
17
|
|
|
use Railt\Reflection\Contracts\Document as DocumentInterface; |
18
|
|
|
use Railt\Reflection\Contracts\Reflection as ReflectionInterface; |
19
|
|
|
use Railt\Reflection\Contracts\Type as TypeInterface; |
20
|
|
|
use Railt\Reflection\Exception\TypeNotFoundException; |
21
|
|
|
use Railt\Reflection\Invocation\Behaviour\HasDirectives; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Document |
25
|
|
|
*/ |
26
|
|
|
class Document extends AbstractDefinition implements DocumentInterface |
27
|
|
|
{ |
28
|
|
|
use Serializable; |
29
|
|
|
use HasDirectives; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Readable |
33
|
|
|
*/ |
34
|
|
|
protected $file; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Reflection |
38
|
|
|
*/ |
39
|
|
|
protected $store; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array|string[] |
43
|
|
|
*/ |
44
|
|
|
protected $types = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* DocumentDefinition constructor. |
48
|
|
|
* @param Reflection|ReflectionInterface $parent |
49
|
|
|
* @param Readable|null $file |
50
|
|
|
*/ |
51
|
8 |
|
public function __construct(Reflection $parent, Readable $file = null) |
52
|
|
|
{ |
53
|
8 |
|
$this->file = $file ?? File::fromSources(''); |
54
|
8 |
|
$this->store = $parent; |
55
|
|
|
|
56
|
8 |
|
parent::__construct($this); |
57
|
|
|
|
58
|
8 |
|
$parent->addDocument($this); |
59
|
8 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return TypeInterface |
63
|
|
|
*/ |
64
|
|
|
public static function getType(): TypeInterface |
65
|
|
|
{ |
66
|
|
|
return Type::of(Type::DOCUMENT); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param TypeDefinition $type |
71
|
|
|
* @return DocumentInterface |
72
|
|
|
*/ |
73
|
4 |
|
public function withDefinition(TypeDefinition $type): DocumentInterface |
74
|
|
|
{ |
75
|
4 |
|
$this->store->add($type); |
76
|
|
|
|
77
|
4 |
|
$this->types[] = $type->getName(); |
78
|
|
|
|
79
|
4 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return Readable |
84
|
|
|
*/ |
85
|
2 |
|
public function getFile(): Readable |
86
|
|
|
{ |
87
|
2 |
|
return $this->file; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return ReflectionInterface|Reflection|Dictionary |
92
|
|
|
*/ |
93
|
3 |
|
public function getDictionary(): Dictionary |
94
|
|
|
{ |
95
|
3 |
|
return $this->store; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
1 |
|
public function isDeprecated(): bool |
102
|
|
|
{ |
103
|
1 |
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
1 |
|
public function getDeprecationReason(): string |
110
|
|
|
{ |
111
|
1 |
|
return ''; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return iterable|TypeDefinition[] |
116
|
|
|
* @throws TypeNotFoundException |
117
|
|
|
*/ |
118
|
4 |
|
public function getDefinitions(): iterable |
119
|
|
|
{ |
120
|
4 |
|
foreach ($this->types as $type) { |
121
|
2 |
|
yield $this->store->get($type); |
122
|
|
|
} |
123
|
4 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $name |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
4 |
|
public function hasDefinition(string $name): bool |
130
|
|
|
{ |
131
|
4 |
|
return \in_array($name, $this->types, true); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $name |
136
|
|
|
* @return null|TypeDefinition |
137
|
|
|
*/ |
138
|
4 |
|
public function getDefinition(string $name): ?TypeDefinition |
139
|
|
|
{ |
140
|
4 |
|
if (! \in_array($name, $this->types, true)) { |
141
|
2 |
|
return null; |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
return $this->store->find($name); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
8 |
|
public function getName(): string |
151
|
|
|
{ |
152
|
8 |
|
return $this->file->isFile() ? $this->file->getPathname() : $this->file->getHash(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|