1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GeneratedHydrator; |
6
|
|
|
|
7
|
|
|
use CodeGenerationUtils\Autoloader\Autoloader; |
8
|
|
|
use CodeGenerationUtils\Autoloader\AutoloaderInterface; |
9
|
|
|
use CodeGenerationUtils\Exception\InvalidGeneratedClassesDirectoryException; |
10
|
|
|
use CodeGenerationUtils\FileLocator\FileLocator; |
11
|
|
|
use CodeGenerationUtils\GeneratorStrategy\FileWriterGeneratorStrategy; |
12
|
|
|
use CodeGenerationUtils\GeneratorStrategy\GeneratorStrategyInterface; |
13
|
|
|
use CodeGenerationUtils\Inflector\ClassNameInflector; |
14
|
|
|
use CodeGenerationUtils\Inflector\ClassNameInflectorInterface; |
15
|
|
|
use GeneratedHydrator\ClassGenerator\DefaultHydratorGenerator; |
16
|
|
|
use GeneratedHydrator\ClassGenerator\HydratorGenerator; |
17
|
|
|
use GeneratedHydrator\Factory\HydratorFactory; |
18
|
|
|
use function sys_get_temp_dir; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Base configuration class for the generated hydrator - serves as micro disposable DIC/facade |
22
|
|
|
*/ |
23
|
|
|
class Configuration |
24
|
|
|
{ |
25
|
|
|
public const DEFAULT_GENERATED_CLASS_NAMESPACE = 'GeneratedHydratorGeneratedClass'; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
protected $hydratedClassName; |
29
|
|
|
|
30
|
|
|
/** @var bool */ |
31
|
|
|
protected $autoGenerateProxies = true; |
32
|
|
|
|
33
|
|
|
/** @var string|null */ |
34
|
|
|
protected $generatedClassesTargetDir; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
protected $generatedClassesNamespace = self::DEFAULT_GENERATED_CLASS_NAMESPACE; |
38
|
|
|
|
39
|
|
|
/** @var GeneratorStrategyInterface|null */ |
40
|
|
|
protected $generatorStrategy; |
41
|
|
|
|
42
|
|
|
/** @var callable|null */ |
43
|
|
|
protected $generatedClassesAutoloader; |
44
|
|
|
|
45
|
|
|
/** @var ClassNameInflectorInterface|null */ |
46
|
|
|
protected $classNameInflector; |
47
|
|
|
|
48
|
|
|
/** @var HydratorGenerator|null */ |
49
|
|
|
protected $hydratorGenerator; |
50
|
|
|
|
51
|
|
|
public function __construct(string $hydratedClassName) |
52
|
|
|
{ |
53
|
|
|
$this->setHydratedClassName($hydratedClassName); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function createFactory() : HydratorFactory |
57
|
|
|
{ |
58
|
|
|
return new HydratorFactory($this); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function setHydratedClassName(string $hydratedClassName) : void |
62
|
|
|
{ |
63
|
|
|
$this->hydratedClassName = $hydratedClassName; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getHydratedClassName() : string |
67
|
|
|
{ |
68
|
|
|
return $this->hydratedClassName; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function setAutoGenerateProxies(bool $autoGenerateProxies) : void |
72
|
|
|
{ |
73
|
|
|
$this->autoGenerateProxies = $autoGenerateProxies; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function doesAutoGenerateProxies() : bool |
77
|
|
|
{ |
78
|
|
|
return $this->autoGenerateProxies; |
79
|
1 |
|
} |
80
|
|
|
|
81
|
1 |
|
public function setGeneratedClassAutoloader(AutoloaderInterface $generatedClassesAutoloader) : void |
82
|
|
|
{ |
83
|
|
|
$this->generatedClassesAutoloader = $generatedClassesAutoloader; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
1 |
|
* @throws InvalidGeneratedClassesDirectoryException |
88
|
|
|
*/ |
89
|
1 |
|
public function getGeneratedClassAutoloader() : AutoloaderInterface |
90
|
1 |
|
{ |
91
|
|
|
if ($this->generatedClassesAutoloader === null) { |
92
|
|
|
$this->generatedClassesAutoloader = new Autoloader( |
93
|
|
|
new FileLocator($this->getGeneratedClassesTargetDir()), |
94
|
|
|
$this->getClassNameInflector() |
95
|
1 |
|
); |
96
|
|
|
} |
97
|
1 |
|
|
98
|
|
|
return $this->generatedClassesAutoloader; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function setGeneratedClassesNamespace(string $generatedClassesNamespace) : void |
102
|
|
|
{ |
103
|
1 |
|
$this->generatedClassesNamespace = $generatedClassesNamespace; |
104
|
|
|
} |
105
|
1 |
|
|
106
|
1 |
|
public function getGeneratedClassesNamespace() : string |
107
|
|
|
{ |
108
|
|
|
return $this->generatedClassesNamespace; |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
public function setGeneratedClassesTargetDir(string $generatedClassesTargetDir) : void |
112
|
|
|
{ |
113
|
1 |
|
$this->generatedClassesTargetDir = $generatedClassesTargetDir; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getGeneratedClassesTargetDir() : string |
117
|
|
|
{ |
118
|
|
|
if ($this->generatedClassesTargetDir === null) { |
119
|
1 |
|
$this->generatedClassesTargetDir = sys_get_temp_dir(); |
120
|
|
|
} |
121
|
1 |
|
|
122
|
1 |
|
return $this->generatedClassesTargetDir; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function setGeneratorStrategy(GeneratorStrategyInterface $generatorStrategy) : void |
126
|
|
|
{ |
127
|
|
|
$this->generatorStrategy = $generatorStrategy; |
128
|
|
|
} |
129
|
1 |
|
|
130
|
|
|
/** |
131
|
1 |
|
* @throws InvalidGeneratedClassesDirectoryException |
132
|
1 |
|
*/ |
133
|
1 |
|
public function getGeneratorStrategy() : GeneratorStrategyInterface |
134
|
1 |
|
{ |
135
|
|
|
if ($this->generatorStrategy === null) { |
136
|
|
|
$this->generatorStrategy = new FileWriterGeneratorStrategy( |
137
|
|
|
new FileLocator($this->getGeneratedClassesTargetDir()) |
138
|
1 |
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->generatorStrategy; |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
public function setClassNameInflector(ClassNameInflectorInterface $classNameInflector) : void |
145
|
|
|
{ |
146
|
1 |
|
$this->classNameInflector = $classNameInflector; |
147
|
1 |
|
} |
148
|
|
|
|
149
|
|
|
public function getClassNameInflector() : ClassNameInflectorInterface |
150
|
|
|
{ |
151
|
|
|
if ($this->classNameInflector === null) { |
152
|
1 |
|
$this->classNameInflector = new ClassNameInflector($this->getGeneratedClassesNamespace()); |
153
|
|
|
} |
154
|
1 |
|
|
155
|
|
|
return $this->classNameInflector; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function setHydratorGenerator(HydratorGenerator $hydratorGenerator) : void |
159
|
|
|
{ |
160
|
1 |
|
$this->hydratorGenerator = $hydratorGenerator; |
161
|
|
|
} |
162
|
1 |
|
|
163
|
1 |
|
public function getHydratorGenerator() : HydratorGenerator |
164
|
|
|
{ |
165
|
|
|
if ($this->hydratorGenerator === null) { |
166
|
|
|
$this->hydratorGenerator = new DefaultHydratorGenerator(); |
167
|
|
|
} |
168
|
1 |
|
|
169
|
|
|
return $this->hydratorGenerator; |
170
|
1 |
|
} |
171
|
|
|
} |
172
|
|
|
|