1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
declare(strict_types=1); |
20
|
|
|
|
21
|
|
|
namespace GeneratedHydrator; |
22
|
|
|
|
23
|
|
|
use GeneratedHydrator\ClassGenerator\HydratorGenerator; |
24
|
|
|
use GeneratedHydrator\ClassGenerator\HydratorGeneratorInterface; |
25
|
|
|
use GeneratedHydrator\Factory\HydratorFactory; |
26
|
|
|
use GeneratedHydrator\ClassGenerator\HydratorGeneratorFactory; |
27
|
|
|
use GeneratedHydrator\ClassGenerator\HydratorGeneratorFactoryInterface; |
28
|
|
|
use CodeGenerationUtils\Autoloader\AutoloaderInterface; |
29
|
|
|
use CodeGenerationUtils\Autoloader\Autoloader; |
30
|
|
|
use CodeGenerationUtils\FileLocator\FileLocator; |
31
|
|
|
use CodeGenerationUtils\GeneratorStrategy\FileWriterGeneratorStrategy; |
32
|
|
|
use CodeGenerationUtils\GeneratorStrategy\GeneratorStrategyInterface; |
33
|
|
|
use CodeGenerationUtils\Inflector\ClassNameInflectorInterface; |
34
|
|
|
use CodeGenerationUtils\Inflector\ClassNameInflector; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Base configuration class for the generated hydrator - serves as micro disposable DIC/facade |
38
|
|
|
* |
39
|
|
|
* @author Marco Pivetta <[email protected]> |
40
|
|
|
* @license MIT |
41
|
|
|
*/ |
42
|
|
|
class Configuration |
43
|
|
|
{ |
44
|
|
|
const DEFAULT_GENERATED_CLASS_NAMESPACE = 'GeneratedHydratorGeneratedClass'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $hydratedClassName; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
protected $autoGenerateProxies = true; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string|null |
58
|
|
|
*/ |
59
|
|
|
protected $generatedClassesTargetDir; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $generatedClassesNamespace = self::DEFAULT_GENERATED_CLASS_NAMESPACE; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var \CodeGenerationUtils\GeneratorStrategy\GeneratorStrategyInterface|null |
68
|
|
|
*/ |
69
|
|
|
protected $generatorStrategy; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var callable|null |
73
|
|
|
*/ |
74
|
|
|
protected $generatedClassesAutoloader; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var \CodeGenerationUtils\Inflector\ClassNameInflectorInterface|null |
78
|
|
|
*/ |
79
|
|
|
protected $classNameInflector; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var \GeneratedHydrator\ClassGenerator\HydratorGeneratorInterface|null |
83
|
|
|
*/ |
84
|
|
|
protected $hydratorGenerator; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $hydratedClassName |
88
|
|
|
*/ |
89
|
|
|
public function __construct(string $hydratedClassName) |
90
|
|
|
{ |
91
|
|
|
$this->setHydratedClassName($hydratedClassName); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return \GeneratedHydrator\Factory\HydratorFactory |
96
|
|
|
*/ |
97
|
1 |
|
public function createFactory() : HydratorFactory |
98
|
|
|
{ |
99
|
1 |
|
return new HydratorFactory($this); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $hydratedClassName |
104
|
|
|
*/ |
105
|
1 |
|
public function setHydratedClassName(string $hydratedClassName) |
106
|
|
|
{ |
107
|
1 |
|
$this->hydratedClassName = $hydratedClassName; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
1 |
|
public function getHydratedClassName() : string |
114
|
|
|
{ |
115
|
1 |
|
return $this->hydratedClassName; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param bool $autoGenerateProxies |
120
|
|
|
*/ |
121
|
1 |
|
public function setAutoGenerateProxies(bool $autoGenerateProxies) |
122
|
|
|
{ |
123
|
1 |
|
$this->autoGenerateProxies = $autoGenerateProxies; |
124
|
1 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
1 |
|
public function doesAutoGenerateProxies() : bool |
130
|
|
|
{ |
131
|
1 |
|
return $this->autoGenerateProxies; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param \CodeGenerationUtils\Autoloader\AutoloaderInterface $generatedClassesAutoloader |
136
|
|
|
*/ |
137
|
1 |
|
public function setGeneratedClassAutoloader(AutoloaderInterface $generatedClassesAutoloader) |
138
|
|
|
{ |
139
|
1 |
|
$this->generatedClassesAutoloader = $generatedClassesAutoloader; |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return \CodeGenerationUtils\Autoloader\AutoloaderInterface |
144
|
|
|
* |
145
|
|
|
* @throws \CodeGenerationUtils\Exception\InvalidGeneratedClassesDirectoryException |
146
|
|
|
*/ |
147
|
1 |
|
public function getGeneratedClassAutoloader() : AutoloaderInterface |
148
|
|
|
{ |
149
|
1 |
|
if (null === $this->generatedClassesAutoloader) { |
150
|
1 |
|
$this->generatedClassesAutoloader = new Autoloader( |
151
|
1 |
|
new FileLocator($this->getGeneratedClassesTargetDir()), |
152
|
1 |
|
$this->getClassNameInflector() |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
return $this->generatedClassesAutoloader; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $generatedClassesNamespace |
161
|
|
|
*/ |
162
|
1 |
|
public function setGeneratedClassesNamespace(string $generatedClassesNamespace) |
163
|
|
|
{ |
164
|
1 |
|
$this->generatedClassesNamespace = $generatedClassesNamespace; |
165
|
1 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
1 |
|
public function getGeneratedClassesNamespace() : string |
171
|
|
|
{ |
172
|
1 |
|
return $this->generatedClassesNamespace; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $generatedClassesTargetDir |
177
|
|
|
*/ |
178
|
1 |
|
public function setGeneratedClassesTargetDir(string $generatedClassesTargetDir) |
179
|
|
|
{ |
180
|
1 |
|
$this->generatedClassesTargetDir = $generatedClassesTargetDir; |
181
|
1 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return null|string |
185
|
|
|
*/ |
186
|
1 |
|
public function getGeneratedClassesTargetDir() |
187
|
|
|
{ |
188
|
1 |
|
if (null === $this->generatedClassesTargetDir) { |
189
|
1 |
|
$this->generatedClassesTargetDir = sys_get_temp_dir(); |
190
|
|
|
} |
191
|
|
|
|
192
|
1 |
|
return $this->generatedClassesTargetDir; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param \CodeGenerationUtils\GeneratorStrategy\GeneratorStrategyInterface $generatorStrategy |
197
|
|
|
*/ |
198
|
1 |
|
public function setGeneratorStrategy(GeneratorStrategyInterface $generatorStrategy) |
199
|
|
|
{ |
200
|
1 |
|
$this->generatorStrategy = $generatorStrategy; |
201
|
1 |
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return \CodeGenerationUtils\GeneratorStrategy\GeneratorStrategyInterface |
205
|
|
|
* |
206
|
|
|
* @throws \CodeGenerationUtils\Exception\InvalidGeneratedClassesDirectoryException |
207
|
|
|
*/ |
208
|
1 |
|
public function getGeneratorStrategy() : GeneratorStrategyInterface |
209
|
|
|
{ |
210
|
1 |
|
if (null === $this->generatorStrategy) { |
211
|
1 |
|
$this->generatorStrategy = new FileWriterGeneratorStrategy( |
212
|
1 |
|
new FileLocator($this->getGeneratedClassesTargetDir()) |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
1 |
|
return $this->generatorStrategy; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param \CodeGenerationUtils\Inflector\ClassNameInflectorInterface $classNameInflector |
221
|
|
|
*/ |
222
|
1 |
|
public function setClassNameInflector(ClassNameInflectorInterface $classNameInflector) |
223
|
|
|
{ |
224
|
1 |
|
$this->classNameInflector = $classNameInflector; |
225
|
1 |
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return \CodeGenerationUtils\Inflector\ClassNameInflectorInterface |
229
|
|
|
*/ |
230
|
1 |
|
public function getClassNameInflector() : ClassNameInflectorInterface |
231
|
|
|
{ |
232
|
1 |
|
if (null === $this->classNameInflector) { |
233
|
1 |
|
$this->classNameInflector = new ClassNameInflector($this->getGeneratedClassesNamespace()); |
234
|
|
|
} |
235
|
|
|
|
236
|
1 |
|
return $this->classNameInflector; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param HydratorGeneratorInterface $hydratorGenerator |
241
|
|
|
*/ |
242
|
1 |
|
public function setHydratorGenerator(HydratorGeneratorInterface $hydratorGenerator) |
243
|
|
|
{ |
244
|
1 |
|
$this->hydratorGenerator = $hydratorGenerator; |
245
|
1 |
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return HydratorGeneratorInterface |
249
|
|
|
*/ |
250
|
1 |
|
public function getHydratorGenerator() |
251
|
|
|
{ |
252
|
1 |
|
if (null === $this->hydratorGenerator) { |
253
|
1 |
|
$this->hydratorGenerator = new HydratorGenerator(); |
254
|
|
|
} |
255
|
|
|
|
256
|
1 |
|
return $this->hydratorGenerator; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|