|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
7
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
8
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
9
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
10
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
11
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
12
|
|
|
* THE SOFTWARE. |
|
13
|
|
|
* |
|
14
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
15
|
|
|
* and is licensed under the MIT license. |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright (c) 2018 Yuuki Takezawa |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Ytake\Lom\Factory; |
|
21
|
|
|
|
|
22
|
|
|
use PhpParser\BuilderFactory; |
|
23
|
|
|
use ReflectionClass; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class GeneratorFactory. |
|
27
|
|
|
* |
|
28
|
|
|
* @author yuuki.takezawa<[email protected]> |
|
29
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
30
|
|
|
*/ |
|
31
|
|
|
class GeneratorFactory |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var array */ |
|
34
|
|
|
protected $parsed; |
|
35
|
|
|
|
|
36
|
|
|
/** @var ReflectionClass */ |
|
37
|
|
|
protected $reflectionClass; |
|
38
|
|
|
|
|
39
|
|
|
/** @var */ |
|
40
|
|
|
protected $annotation; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param ReflectionClass $reflectionClass |
|
44
|
|
|
* @param array $parsed |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(ReflectionClass $reflectionClass, array $parsed) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->reflectionClass = $reflectionClass; |
|
49
|
|
|
$this->parsed = $parsed; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param $annotation |
|
54
|
|
|
* |
|
55
|
|
|
* @return AbstractDriver |
|
56
|
|
|
*/ |
|
57
|
|
|
public function driver($annotation): AbstractDriver |
|
58
|
|
|
{ |
|
59
|
|
|
$className = explode('\\', get_class($annotation)); |
|
60
|
|
|
$driverClass = 'create'.ucfirst(end($className)).'Driver'; |
|
61
|
|
|
$this->annotation = $annotation; |
|
62
|
|
|
|
|
63
|
|
|
return $this->$driverClass(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return DataDriver |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function createDataDriver(): DataDriver |
|
70
|
|
|
{ |
|
71
|
|
|
return (new DataDriver( |
|
72
|
|
|
$this->parsed, |
|
73
|
|
|
new BuilderFactory() |
|
74
|
|
|
))->setReflector($this->reflectionClass); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return ValueDriver |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function createValueDriver(): ValueDriver |
|
81
|
|
|
{ |
|
82
|
|
|
return (new ValueDriver( |
|
83
|
|
|
$this->parsed, |
|
84
|
|
|
new BuilderFactory() |
|
85
|
|
|
))->setReflector($this->reflectionClass); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* for NoArgsConstructor Annotation Driver. |
|
90
|
|
|
* |
|
91
|
|
|
* @return NoArgsConstructorDriver |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function createNoArgsConstructorDriver(): NoArgsConstructorDriver |
|
94
|
|
|
{ |
|
95
|
|
|
return (new NoArgsConstructorDriver( |
|
96
|
|
|
$this->parsed, |
|
97
|
|
|
new BuilderFactory() |
|
98
|
|
|
))->setReflector($this->reflectionClass); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* for AllArgsConstructor Annotation Driver. |
|
103
|
|
|
* |
|
104
|
|
|
* @return AllArgsConstructorDriver |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function createAllArgsConstructorDriver(): AllArgsConstructorDriver |
|
107
|
|
|
{ |
|
108
|
|
|
return (new AllArgsConstructorDriver( |
|
109
|
|
|
$this->parsed, |
|
110
|
|
|
new BuilderFactory() |
|
111
|
|
|
))->setReflector($this->reflectionClass) |
|
112
|
|
|
->setAnnotationInstance($this->annotation); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* for Getter Annotation Driver. |
|
117
|
|
|
* |
|
118
|
|
|
* @return GetterDriver |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function createGetterDriver(): GetterDriver |
|
121
|
|
|
{ |
|
122
|
|
|
return (new GetterDriver( |
|
123
|
|
|
$this->parsed, |
|
124
|
|
|
new BuilderFactory() |
|
125
|
|
|
))->setReflector($this->reflectionClass) |
|
126
|
|
|
->setAnnotationInstance($this->annotation); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* for Setter Annotation Driver. |
|
131
|
|
|
* |
|
132
|
|
|
* @return SetterDriver |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function createSetterDriver(): SetterDriver |
|
135
|
|
|
{ |
|
136
|
|
|
return (new SetterDriver( |
|
137
|
|
|
$this->parsed, |
|
138
|
|
|
new BuilderFactory() |
|
139
|
|
|
))->setReflector($this->reflectionClass) |
|
140
|
|
|
->setAnnotationInstance($this->annotation); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|