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\Source; |
16
|
|
|
use Spiral\Reactor\Traits\CommentTrait; |
17
|
|
|
use Spiral\Reactor\Traits\NamedTrait; |
18
|
|
|
use Spiral\Reactor\Traits\UsesTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Represent namespace declaration. Attention, namespace renders in a form of namespace name { ... } |
22
|
|
|
*/ |
23
|
|
|
class NamespaceDeclaration extends AbstractDeclaration implements ReplaceableInterface |
24
|
|
|
{ |
25
|
|
|
use NamedTrait; |
26
|
|
|
use UsesTrait; |
27
|
|
|
use CommentTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Aggregator |
31
|
|
|
*/ |
32
|
|
|
private $elements; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $name |
36
|
|
|
* @param string $comment |
37
|
|
|
*/ |
38
|
|
|
public function __construct(string $name = '', string $comment = '') |
39
|
|
|
{ |
40
|
|
|
$this->setName($name); |
41
|
|
|
|
42
|
|
|
$this->elements = new Aggregator([ |
43
|
|
|
ClassDeclaration::class, |
44
|
|
|
Comment::class, |
45
|
|
|
Source::class |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
$this->initComment($comment); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Method will automatically mount requested uses is any. |
53
|
|
|
* |
54
|
|
|
* @param DeclarationInterface $element |
55
|
|
|
* @return self |
56
|
|
|
* @throws Exception\ReactorException |
57
|
|
|
*/ |
58
|
|
|
public function addElement(DeclarationInterface $element): NamespaceDeclaration |
59
|
|
|
{ |
60
|
|
|
$this->elements->add($element); |
61
|
|
|
if ($element instanceof DependedInterface) { |
62
|
|
|
$this->addUses($element->getDependencies()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
* @return self |
71
|
|
|
*/ |
72
|
|
|
public function replace($search, $replace): NamespaceDeclaration |
73
|
|
|
{ |
74
|
|
|
$this->docComment->replace($search, $replace); |
75
|
|
|
$this->elements->replace($search, $replace); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function render(int $indentLevel = 0): string |
84
|
|
|
{ |
85
|
|
|
$result = ''; |
86
|
|
|
$indentShift = 0; |
87
|
|
|
|
88
|
|
|
if (!$this->docComment->isEmpty()) { |
89
|
|
|
$result .= $this->docComment->render($indentLevel) . "\n"; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (!empty($this->getName())) { |
93
|
|
|
$result .= $this->addIndent("namespace {$this->getName()} {", $indentLevel) . "\n"; |
94
|
|
|
$indentShift = 1; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (!empty($this->uses)) { |
98
|
|
|
$result .= $this->renderUses($indentLevel + $indentShift) . "\n\n"; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$result .= $this->elements->render($indentLevel + $indentShift); |
102
|
|
|
|
103
|
|
|
if (!empty($this->getName())) { |
104
|
|
|
$result .= "\n" . $this->addIndent('}', $indentLevel); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $result; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return Aggregator|ClassDeclaration[]|Source[]|Comment[] |
112
|
|
|
*/ |
113
|
|
|
public function getElements(): Aggregator |
114
|
|
|
{ |
115
|
|
|
return $this->elements; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|