1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2015-09-19 |
5
|
|
|
*/ |
6
|
|
|
namespace Net\Bazzline\Propel\Behavior\EntityInstantiator; |
7
|
|
|
|
8
|
|
|
class FileContentGenerator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param EntityCollection $collection |
12
|
|
|
* @param Configuration $configuration |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
|
|
public function generate( |
16
|
|
|
EntityCollection $collection, |
17
|
|
|
Configuration $configuration |
18
|
|
|
) { |
19
|
|
|
$useStatements = $this->generateUseStatements( |
20
|
|
|
$configuration->doNotUseFullyQualifiedNames(), |
21
|
|
|
$configuration->getNamespace(), |
22
|
|
|
$collection |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
$content = $this->generateFileHeader( |
26
|
|
|
$configuration->getNamespace(), |
27
|
|
|
$useStatements |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
$content .= $this->generateClassHeader( |
31
|
|
|
$configuration->getClassName(), |
32
|
|
|
$configuration->getExtends() |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$content .= $this->generateGetConnectionMethod( |
36
|
|
|
$configuration->getIndention(), |
37
|
|
|
$configuration->getDefaultConnectionMode(), |
38
|
|
|
$configuration->getDefaultConnectionName() |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
foreach ($collection as $entity) { |
42
|
|
|
$content .= $this->generateObjectEntityOrQueryEntityGetMethod( |
43
|
|
|
$entity, |
44
|
|
|
$configuration->getIndention(), |
45
|
|
|
$configuration->useFullyQualifiedNames() |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$content .= '}'; |
50
|
|
|
|
51
|
|
|
return $content; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $className |
56
|
|
|
* @param null|string $extends |
57
|
|
|
* @return string |
58
|
|
|
* @todo find a better way to have it indented and readable |
59
|
|
|
*/ |
60
|
|
|
private function generateClassHeader( |
61
|
|
|
$className, |
62
|
|
|
$extends |
63
|
|
|
) { |
64
|
|
|
$extends = ($this->isValidString($extends)) ? ' extends ' . $extends : ''; |
65
|
|
|
|
66
|
|
|
return ' |
67
|
|
|
/** |
68
|
|
|
* Class ' . $className . ' |
69
|
|
|
* |
70
|
|
|
* @author ' . __CLASS__ . ' |
71
|
|
|
* @since ' . date('Y-m-d') . ' |
72
|
|
|
* @link http://www.bazzline.net |
73
|
|
|
*/ |
74
|
|
|
class ' . $className . $extends . ' |
75
|
|
|
{' . PHP_EOL; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param AbstractEntity $entity |
80
|
|
|
* @param string $indention |
81
|
|
|
* @param boolean $useFullyQualifiedName |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
private function generateObjectEntityOrQueryEntityGetMethod( |
85
|
|
|
AbstractEntity $entity, |
86
|
|
|
$indention, |
87
|
|
|
$useFullyQualifiedName |
88
|
|
|
) { |
89
|
|
|
$methodName = lcfirst($entity->methodNamePrefix() . ucfirst($entity->className())); |
90
|
|
|
$className = ($useFullyQualifiedName) |
91
|
|
|
? '\\' . $entity->fullQualifiedClassName() |
92
|
|
|
: $entity->className(); |
93
|
|
|
|
94
|
|
|
$content = PHP_EOL . |
95
|
|
|
$indention . '/**' . PHP_EOL . |
96
|
|
|
$indention . ' * @return ' . $entity->className() . PHP_EOL . |
97
|
|
|
$indention . ' */' . PHP_EOL . |
98
|
|
|
$indention . 'public function ' . $methodName . '()' . PHP_EOL . |
99
|
|
|
$indention . '{' . PHP_EOL; |
100
|
|
|
|
101
|
|
|
if ($entity instanceof ObjectEntity) { |
102
|
|
|
$content .= $indention . $indention . 'return new ' . $className . '();' . PHP_EOL; |
103
|
|
|
} else if ($entity instanceof QueryEntity) { |
104
|
|
|
$content .= $indention . $indention . 'return ' . $className . '::create();' . PHP_EOL; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$content .= $indention . '}' . PHP_EOL; |
108
|
|
|
|
109
|
|
|
return $content; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param null|string $namespace |
114
|
|
|
* @param array $uses |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
private function generateFileHeader( |
118
|
|
|
$namespace, |
119
|
|
|
$uses |
120
|
|
|
) { |
121
|
|
|
$content = '<?php'; |
122
|
|
|
$content .= ($this->isValidString($namespace)) |
123
|
|
|
? str_repeat(PHP_EOL, 2) . 'namespace ' . $namespace . ';' . PHP_EOL |
124
|
|
|
: PHP_EOL; |
125
|
|
|
|
126
|
|
|
$thereAreUseStatements = (!empty($uses)); |
127
|
|
|
|
128
|
|
|
if ($thereAreUseStatements) { |
129
|
|
|
$content .= PHP_EOL; |
130
|
|
|
$content .= implode(PHP_EOL, $uses); |
131
|
|
|
$content .= PHP_EOL; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $content; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $indention |
139
|
|
|
* @param null|string $defaultConnectionMode |
140
|
|
|
* @param null|string $defaultConnectionName |
141
|
|
|
* @return string |
142
|
|
|
* @todo find a better way to have it indented and readable |
143
|
|
|
* @todo move default values out |
144
|
|
|
*/ |
145
|
|
|
private function generateGetConnectionMethod( |
146
|
|
|
$indention, |
147
|
|
|
$defaultConnectionMode = null, |
148
|
|
|
$defaultConnectionName = null |
149
|
|
|
) { |
150
|
|
|
|
151
|
|
|
return $indention . '/** |
152
|
|
|
' . $indention . ' * @param null|string $name - The data source name that is used to look up the DSN from the runtime configuration file. |
153
|
|
|
' . $indention . ' * @param string $mode The connection mode (this applies to replication systems). |
154
|
|
|
' . $indention . ' * @return PDO |
155
|
|
|
' . $indention . ' */ |
156
|
|
|
' . $indention . 'public function getConnection($name = ' . $defaultConnectionName . ', $mode = ' . $defaultConnectionMode . ') |
157
|
|
|
' . $indention . '{ |
158
|
|
|
' . (str_repeat($indention, 2)) . 'return Propel::getConnection($name, $mode); |
159
|
|
|
' . $indention . '}' . PHP_EOL; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param boolean $includeCollection |
164
|
|
|
* @param null|string $namespace |
165
|
|
|
* @param EntityCollection $collection |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
private function generateUseStatements( |
169
|
|
|
$includeCollection, |
170
|
|
|
$namespace, |
171
|
|
|
EntityCollection $collection |
172
|
|
|
) { |
173
|
|
|
$uses = []; |
174
|
|
|
|
175
|
|
|
if ($this->isValidString($namespace)) { |
176
|
|
|
$uses[] = 'use Propel;'; |
177
|
|
|
$uses[] = 'use PDO;'; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($includeCollection) { |
181
|
|
|
foreach ($collection as $entity) { |
182
|
|
|
$uses[] = 'use ' . $entity->fullQualifiedClassName() . ';'; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
natsort($uses); |
187
|
|
|
|
188
|
|
|
return $uses; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param null|string $namespace |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
|
|
private function isValidString($namespace) |
196
|
|
|
{ |
197
|
|
|
return ((is_string($namespace)) |
198
|
|
|
&& (strlen($namespace) > 0)); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|