1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Murtukov\PHPCodeGenerator; |
6
|
|
|
|
7
|
|
|
abstract class DependencyAwareGenerator extends AbstractGenerator |
8
|
|
|
{ |
9
|
|
|
protected array $usePaths = []; |
10
|
|
|
protected array $useGroups = []; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* List of all generator children, which maintain their own use dependencies. |
14
|
|
|
* The list should be defined in the constructor. |
15
|
|
|
* |
16
|
|
|
* @var mixed[] |
17
|
|
|
*/ |
18
|
|
|
protected array $dependencyAwareChildren = []; |
19
|
|
|
|
20
|
35 |
|
public function resolveQualifier(string $path, $alias = ''): string |
21
|
|
|
{ |
22
|
35 |
|
if (empty($path) || false === Config::$shortenQualifiers || '\\' === $path[0]) { |
23
|
17 |
|
return $path; |
24
|
|
|
} |
25
|
|
|
|
26
|
27 |
|
if ($path[0] === Config::$suppressSymbol) { |
27
|
2 |
|
return substr($path, 1); |
28
|
|
|
} |
29
|
|
|
|
30
|
25 |
|
if ($qualifier = Utils::resolveQualifier($path)) { |
31
|
4 |
|
$this->usePaths[$path] = $alias; |
32
|
4 |
|
$path = $qualifier; |
33
|
|
|
} |
34
|
|
|
|
35
|
25 |
|
return $path; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function addUse(string $fqcn, string ...$aliases): self |
39
|
|
|
{ |
40
|
1 |
|
$this->usePaths[$fqcn] = implode(', ', $aliases); |
41
|
|
|
|
42
|
1 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function addUseGroup(string $fqcn, string ...$classNames) |
46
|
|
|
{ |
47
|
1 |
|
foreach ($classNames as $name) { |
48
|
1 |
|
if ($qualifier = Utils::resolveQualifier($name)) { |
49
|
1 |
|
$name = $qualifier; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
if (empty($this->useGroups[$fqcn]) || !in_array($name, $this->useGroups[$fqcn])) { |
53
|
1 |
|
$this->useGroups[$fqcn][] = $name; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function removeUse(string $fqcn): self |
61
|
|
|
{ |
62
|
1 |
|
unset($this->usePaths[$fqcn]); |
63
|
1 |
|
unset($this->useGroups[$fqcn]); |
64
|
|
|
|
65
|
1 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
public function useGroupsToArray() |
69
|
|
|
{ |
70
|
3 |
|
$result = []; |
71
|
|
|
|
72
|
3 |
|
foreach ($this->useGroups as $path => $classNames) { |
73
|
1 |
|
$result[rtrim($path, '\\').'\{'.implode(', ', $classNames).'}'] = ''; |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
return $result; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns all use-qualifiers used in this object and all it's children. |
81
|
|
|
* |
82
|
|
|
* @return string[] |
83
|
|
|
*/ |
84
|
3 |
|
public function getUsePaths(): array |
85
|
|
|
{ |
86
|
|
|
// Merge self use paths and use groups |
87
|
3 |
|
$mergedPaths = $this->usePaths + $this->useGroupsToArray(); |
88
|
|
|
|
89
|
3 |
|
foreach ($this->dependencyAwareChildren as $child) { |
90
|
3 |
|
if (is_array($child)) { |
91
|
3 |
|
foreach ($child as $subchild) { |
92
|
3 |
|
if ($subchild instanceof self) { |
93
|
3 |
|
$mergedPaths = $mergedPaths + $subchild->getUsePaths(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} else { |
97
|
2 |
|
$mergedPaths = $mergedPaths + $child->getUsePaths(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
3 |
|
return $mergedPaths; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|