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