1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace generators; |
4
|
|
|
|
5
|
|
|
use filesystem\Filesystem; |
6
|
|
|
|
7
|
|
|
class MultipleFileGenerator implements IFileGenerator { |
8
|
|
|
private $fileGenerators = []; |
9
|
|
|
private $filesystem; |
10
|
|
|
|
11
|
|
|
function __construct(Filesystem $filesystem) |
12
|
|
|
{ |
13
|
|
|
$this->filesystem = $filesystem; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function addGenerator(AbstractGenerator $generator) |
17
|
|
|
{ |
18
|
|
|
$className = (new \ReflectionClass($generator))->getShortName(); |
19
|
|
|
$this->fileGenerators[] = [ |
20
|
|
|
'type' => $className, |
21
|
|
|
'generator' => new FileGenerator($generator, $this->filesystem) |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
return $this; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function removeGenerator(string $className) { |
28
|
|
|
$inputClassName = (new \ReflectionClass($className))->getShortName(); |
29
|
|
|
|
30
|
|
|
$this->fileGenerators = array_filter($this->fileGenerators, function($fileGenerator) use ($inputClassName) { |
31
|
|
|
$currentClassName = (new \ReflectionClass($fileGenerator['generator']->extract()))->getShortName(); |
32
|
|
|
|
33
|
|
|
return $inputClassName !== $currentClassName; |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function find(string $className): AbstractFileGenerator |
40
|
|
|
{ |
41
|
|
|
$inputClassName = (new \ReflectionClass($className))->getShortName(); |
42
|
|
|
$found = array_filter($this->fileGenerators, function($fileGenerator) use ($inputClassName) { |
43
|
|
|
$currentClassName = (new \ReflectionClass($fileGenerator['generator']->extract()))->getShortName(); |
44
|
|
|
return $inputClassName === $currentClassName; |
45
|
|
|
}); |
46
|
|
|
|
47
|
|
|
if (!empty($found)) { |
48
|
|
|
return (current($found))['generator']; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
throw new \InvalidArgumentException('There is no file generator for ' . $className); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function filter(string $className) |
55
|
|
|
{ |
56
|
|
|
$inputClassName = (new \ReflectionClass($className))->getShortName(); |
57
|
|
|
$filtered = new $this($this->filesystem); |
58
|
|
|
array_walk($this->fileGenerators, function($fileGenerator) use ($filtered, $inputClassName) { |
59
|
|
|
$currentClassName = (new \ReflectionClass($fileGenerator['generator']->extract()))->getShortName(); |
60
|
|
|
if ($inputClassName === $currentClassName) { |
61
|
|
|
$filtered->addGenerator($fileGenerator['generator']->extract()); |
62
|
|
|
} |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
if (!$filtered->isEmpty()) { |
66
|
|
|
return $filtered; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new \InvalidArgumentException('There is no file generators for ' . $className); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function excluding(string $className) { |
73
|
|
|
return (clone $this)->removeGenerator($className); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function including(AbstractGenerator $generator) { |
77
|
|
|
return (clone $this)->addGenerator($generator); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function each(callable $callback) |
81
|
|
|
{ |
82
|
|
|
$generatorslist = array_column($this->fileGenerators, 'generator'); |
83
|
|
|
array_walk($generatorslist, $callback); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function readFromTemplate() |
89
|
|
|
{ |
90
|
|
|
array_walk($this->fileGenerators, function($fileGenerator) { |
91
|
|
|
$fileGenerator['generator']->readFromTemplate(); |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function read() |
98
|
|
|
{ |
99
|
|
|
array_walk($this->fileGenerators, function($fileGenerator) { |
100
|
|
|
$fileGenerator['generator']->read(); |
101
|
|
|
}); |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function exists(): bool |
107
|
|
|
{ |
108
|
|
|
return array_reduce($this->fileGenerators, function($exists, $fileGenerator) { |
109
|
|
|
return $exists && $fileGenerator['generator']->exists(); |
110
|
|
|
}, true); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function remove() |
114
|
|
|
{ |
115
|
|
|
array_walk($this->fileGenerators, function($fileGenerator) { |
116
|
|
|
$fileGenerator['generator']->remove(); |
117
|
|
|
}); |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function throwIfExists(string $message = '') |
123
|
|
|
{ |
124
|
|
|
array_walk($this->fileGenerators, function($fileGenerator) use ($message) { |
125
|
|
|
$fileGenerator['generator']->throwIfExists($message); |
126
|
|
|
}); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function throwIfNotExists(string $message = '') |
132
|
|
|
{ |
133
|
|
|
array_walk($this->fileGenerators, function($fileGenerator) use ($message) { |
134
|
|
|
$fileGenerator['generator']->throwIfNotExists($message); |
135
|
|
|
}); |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function write() |
141
|
|
|
{ |
142
|
|
|
array_walk($this->fileGenerators, function($fileGenerator) { |
143
|
|
|
$fileGenerator['generator']->write(); |
144
|
|
|
}); |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function isEmpty(): bool |
150
|
|
|
{ |
151
|
|
|
return empty($this->fileGenerators); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|