1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jellyfish\Transfer; |
4
|
|
|
|
5
|
|
|
use ArrayObject; |
6
|
|
|
use Jellyfish\Transfer\Generator\ClassGenerator; |
7
|
|
|
use Jellyfish\Transfer\Generator\FactoryClassGenerator; |
8
|
|
|
use Jellyfish\Transfer\Command\TransferGenerateCommand; |
9
|
|
|
use Jellyfish\Transfer\Definition\ClassDefinitionMapLoader; |
10
|
|
|
use Jellyfish\Transfer\Definition\ClassDefinitionMapLoaderInterface; |
11
|
|
|
use Jellyfish\Transfer\Definition\ClassDefinitionMapMapper; |
12
|
|
|
use Jellyfish\Transfer\Definition\ClassDefinitionMapMapperInterface; |
13
|
|
|
use Jellyfish\Transfer\Definition\ClassDefinitionMapMerger; |
14
|
|
|
use Jellyfish\Transfer\Definition\ClassDefinitionMapMergerInterface; |
15
|
|
|
use Jellyfish\Transfer\Definition\DefinitionFinder; |
16
|
|
|
use Jellyfish\Transfer\Definition\DefinitionFinderInterface; |
17
|
|
|
use Jellyfish\Transfer\Generator\FactoryRegistryGenerator; |
18
|
|
|
use Jellyfish\Transfer\Generator\FactoryRegistryGeneratorInterface; |
19
|
|
|
use Pimple\Container; |
20
|
|
|
use Pimple\ServiceProviderInterface; |
21
|
|
|
use Twig\Environment; |
22
|
|
|
use Twig\Loader\FilesystemLoader; |
23
|
|
|
|
24
|
|
|
class TransferServiceProvider implements ServiceProviderInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var \Twig\Environment|null |
28
|
|
|
*/ |
29
|
|
|
protected $twigEnvironment; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \Pimple\Container $pimple |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function register(Container $pimple): void |
37
|
|
|
{ |
38
|
|
|
$this->registerCommands($pimple); |
39
|
|
|
$this->registerFactories($pimple); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param \Pimple\Container $container |
44
|
|
|
* |
45
|
|
|
* @return \Pimple\ServiceProviderInterface |
46
|
|
|
*/ |
47
|
|
|
protected function registerCommands(Container $container): ServiceProviderInterface |
48
|
|
|
{ |
49
|
|
|
$self = $this; |
50
|
|
|
|
51
|
|
|
$container->extend('commands', function (array $commands, Container $container) use ($self) { |
52
|
|
|
$commands[] = new TransferGenerateCommand( |
53
|
|
|
$self->createTransferGenerator($container), |
54
|
|
|
$self->createTransferCleaner($container), |
55
|
|
|
$container->offsetGet('logger') |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
return $commands; |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param \Pimple\Container $container |
66
|
|
|
* |
67
|
|
|
* @return \Jellyfish\Transfer\TransferGeneratorInterface |
68
|
|
|
*/ |
69
|
|
|
protected function createTransferGenerator(Container $container): TransferGeneratorInterface |
70
|
|
|
{ |
71
|
|
|
return new TransferGenerator( |
72
|
|
|
$this->createClassDefinitionMapLoader($container), |
73
|
|
|
$this->createFactoryRegistryGenerator($container), |
74
|
|
|
$this->createClassGenerators($container) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param \Pimple\Container $container |
80
|
|
|
* |
81
|
|
|
* @return \Jellyfish\Transfer\Definition\ClassDefinitionMapLoaderInterface |
82
|
|
|
*/ |
83
|
|
|
protected function createClassDefinitionMapLoader(Container $container): ClassDefinitionMapLoaderInterface |
84
|
|
|
{ |
85
|
|
|
return new ClassDefinitionMapLoader( |
86
|
|
|
$this->createDefinitionFinder($container), |
87
|
|
|
$this->createClassDefinitionMapMapper($container), |
88
|
|
|
$this->createClassDefinitionMapMerger() |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param \Pimple\Container $container |
94
|
|
|
* |
95
|
|
|
* @return \Jellyfish\Transfer\Definition\DefinitionFinderInterface |
96
|
|
|
*/ |
97
|
|
|
protected function createDefinitionFinder(Container $container): DefinitionFinderInterface |
98
|
|
|
{ |
99
|
|
|
return new DefinitionFinder( |
100
|
|
|
$container->offsetGet('finder_factory'), |
101
|
|
|
$container->offsetGet('root_dir') |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param \Pimple\Container $container |
107
|
|
|
* |
108
|
|
|
* @return \Jellyfish\Transfer\Definition\ClassDefinitionMapMapperInterface |
109
|
|
|
*/ |
110
|
|
|
protected function createClassDefinitionMapMapper(Container $container): ClassDefinitionMapMapperInterface |
111
|
|
|
{ |
112
|
|
|
return new ClassDefinitionMapMapper($container->offsetGet('serializer')); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return \Jellyfish\Transfer\Definition\ClassDefinitionMapMergerInterface |
117
|
|
|
*/ |
118
|
|
|
protected function createClassDefinitionMapMerger(): ClassDefinitionMapMergerInterface |
119
|
|
|
{ |
120
|
|
|
return new ClassDefinitionMapMerger(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param \Pimple\Container $container |
125
|
|
|
* |
126
|
|
|
* @return \Jellyfish\Transfer\Generator\FactoryRegistryGeneratorInterface |
127
|
|
|
*/ |
128
|
|
|
protected function createFactoryRegistryGenerator(Container $container): FactoryRegistryGeneratorInterface |
129
|
|
|
{ |
130
|
|
|
$targetDirectory = $this->getTargetDirectory($container); |
131
|
|
|
$twigEnvironment = $this->getTwigEnvironment(); |
132
|
|
|
|
133
|
|
|
return new FactoryRegistryGenerator($container->offsetGet('filesystem'), $twigEnvironment, $targetDirectory); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param \Pimple\Container $container |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
protected function createClassGenerators(Container $container): array |
142
|
|
|
{ |
143
|
|
|
$targetDirectory = $this->getTargetDirectory($container); |
144
|
|
|
$twigEnvironment = $this->getTwigEnvironment(); |
145
|
|
|
|
146
|
|
|
return [ |
147
|
|
|
new ClassGenerator( |
148
|
|
|
$container->offsetGet('filesystem'), |
149
|
|
|
$twigEnvironment, |
150
|
|
|
$targetDirectory |
151
|
|
|
), |
152
|
|
|
new FactoryClassGenerator( |
153
|
|
|
$container->offsetGet('filesystem'), |
154
|
|
|
$twigEnvironment, |
155
|
|
|
$targetDirectory |
156
|
|
|
), |
157
|
|
|
]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return \Twig\Environment |
162
|
|
|
*/ |
163
|
|
|
protected function getTwigEnvironment(): Environment |
164
|
|
|
{ |
165
|
|
|
if ($this->twigEnvironment === null) { |
166
|
|
|
$pathToTemplates = __DIR__ . DIRECTORY_SEPARATOR . 'Templates' . DIRECTORY_SEPARATOR; |
167
|
|
|
$loader = new FilesystemLoader($pathToTemplates); |
168
|
|
|
$this->twigEnvironment = new Environment($loader, []); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->twigEnvironment; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param \Pimple\Container $container |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
protected function getTargetDirectory(Container $container): string |
180
|
|
|
{ |
181
|
|
|
return $container->offsetGet('root_dir') . 'src/Generated/Transfer/'; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param \Pimple\Container $container |
186
|
|
|
* |
187
|
|
|
* @return \Jellyfish\Transfer\TransferCleanerInterface |
188
|
|
|
*/ |
189
|
|
|
protected function createTransferCleaner(Container $container): TransferCleanerInterface |
190
|
|
|
{ |
191
|
|
|
$targetDirectory = $this->getTargetDirectory($container); |
192
|
|
|
|
193
|
|
|
return new TransferCleaner( |
194
|
|
|
$container->offsetGet('finder_factory'), |
195
|
|
|
$container->offsetGet('filesystem'), |
196
|
|
|
$targetDirectory |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param \Pimple\Container $container |
202
|
|
|
* |
203
|
|
|
* @return \Pimple\ServiceProviderInterface |
204
|
|
|
*/ |
205
|
|
|
protected function registerFactories(Container $container): ServiceProviderInterface |
206
|
|
|
{ |
207
|
|
|
$pathToFactoryRegistry = $this->getTargetDirectory($container) . 'factory-registry.php'; |
208
|
|
|
$factoryRegistry = new ArrayObject(); |
209
|
|
|
|
210
|
|
|
if (\file_exists($pathToFactoryRegistry)) { |
211
|
|
|
include $pathToFactoryRegistry; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
foreach ($factoryRegistry as $factoryId => $factory) { |
215
|
|
|
$container->offsetSet($factoryId, function () use ($factory) { |
216
|
|
|
return $factory; |
217
|
|
|
}); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|