|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Reactor; |
|
13
|
|
|
|
|
14
|
|
|
use Spiral\Reactor\Partial\Comment; |
|
15
|
|
|
use Spiral\Reactor\Partial\Directives; |
|
16
|
|
|
use Spiral\Reactor\Partial\Source; |
|
17
|
|
|
use Spiral\Reactor\Traits\CommentTrait; |
|
18
|
|
|
use Spiral\Reactor\Traits\UsesTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Provides ability to render file content. |
|
22
|
|
|
*/ |
|
23
|
|
|
class FileDeclaration extends AbstractDeclaration implements ReplaceableInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use UsesTrait; |
|
26
|
|
|
use CommentTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* File namespace. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $namespace; |
|
34
|
|
|
|
|
35
|
|
|
/** @var Directives|null */ |
|
36
|
|
|
private $directives; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Aggregator |
|
40
|
|
|
*/ |
|
41
|
|
|
private $elements; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $namespace |
|
45
|
|
|
* @param string $comment |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(string $namespace = '', string $comment = '') |
|
48
|
|
|
{ |
|
49
|
|
|
$this->namespace = $namespace; |
|
50
|
|
|
|
|
51
|
|
|
$this->elements = new Aggregator([ |
|
52
|
|
|
ClassDeclaration::class, |
|
53
|
|
|
NamespaceDeclaration::class, |
|
54
|
|
|
Comment::class, |
|
55
|
|
|
Source::class |
|
56
|
|
|
]); |
|
57
|
|
|
|
|
58
|
|
|
$this->initComment($comment); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __toString(): string |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->render(0); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $namespace |
|
71
|
|
|
* @return self |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setNamespace(string $namespace): FileDeclaration |
|
74
|
|
|
{ |
|
75
|
|
|
$this->namespace = $namespace; |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getNamespace(): string |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->namespace; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string ...$directives |
|
90
|
|
|
* @return FileDeclaration |
|
91
|
|
|
*/ |
|
92
|
|
|
public function setDirectives(string ...$directives): FileDeclaration |
|
93
|
|
|
{ |
|
94
|
|
|
$this->directives = new Directives(...$directives); |
|
95
|
|
|
|
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Method will automatically mount requested uses is any. |
|
101
|
|
|
* |
|
102
|
|
|
* @param DeclarationInterface $element |
|
103
|
|
|
* @return self |
|
104
|
|
|
* @throws Exception\ReactorException |
|
105
|
|
|
*/ |
|
106
|
|
|
public function addElement(DeclarationInterface $element): FileDeclaration |
|
107
|
|
|
{ |
|
108
|
|
|
$this->elements->add($element); |
|
109
|
|
|
if ($element instanceof DependedInterface) { |
|
110
|
|
|
$this->addUses($element->getDependencies()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
* @return self |
|
119
|
|
|
*/ |
|
120
|
|
|
public function replace($search, $replace): FileDeclaration |
|
121
|
|
|
{ |
|
122
|
|
|
$this->docComment->replace($search, $replace); |
|
123
|
|
|
$this->elements->replace($search, $replace); |
|
124
|
|
|
|
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritdoc} |
|
130
|
|
|
*/ |
|
131
|
|
|
public function render(int $indentLevel = 0): string |
|
132
|
|
|
{ |
|
133
|
|
|
$result = "<?php\n"; |
|
134
|
|
|
|
|
135
|
|
|
if (!$this->docComment->isEmpty()) { |
|
136
|
|
|
$result .= $this->docComment->render($indentLevel) . "\n"; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if ($this->directives !== null && !empty($this->directives->render())) { |
|
140
|
|
|
$result .= $this->directives->render() . "\n\n"; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if (!empty($this->namespace)) { |
|
144
|
|
|
if ($this->docComment->isEmpty()) { |
|
145
|
|
|
$result .= "\n"; |
|
146
|
|
|
} |
|
147
|
|
|
$result .= "namespace {$this->namespace};\n\n"; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if (!empty($this->uses)) { |
|
151
|
|
|
$result .= $this->renderUses($indentLevel) . "\n\n"; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$result .= $this->elements->render($indentLevel); |
|
155
|
|
|
$result .= "\n"; |
|
156
|
|
|
|
|
157
|
|
|
return $result; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return Aggregator|ClassDeclaration[]|NamespaceDeclaration[]|Source[]|Comment[] |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getElements(): Aggregator |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->elements; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|