-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathGeneratorConfig.php
More file actions
339 lines (300 loc) · 10.9 KB
/
GeneratorConfig.php
File metadata and controls
339 lines (300 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
require_once dirname(__FILE__) . '/GeneratorConfigInterface.php';
// Phing dependencies
require_once 'phing/Phing.php';
/**
* A class that holds build properties and provide a class loading mechanism for the generator.
*
* @author Hans Lellelid <hans@xmpl.org>
* @package propel.generator.config
*/
class GeneratorConfig implements GeneratorConfigInterface
{
/**
* The build properties.
*
* @var array
*/
private $buildProperties = array();
protected $buildConnections = null;
protected $defaultBuildConnection = null;
/**
* Construct a new GeneratorConfig.
*
* @param mixed $props Array or Iterator
*/
public function __construct($props = null)
{
if ($props) {
$this->setBuildProperties($props);
}
}
/**
* Gets the build properties.
*
* @return array
*/
public function getBuildProperties()
{
return $this->buildProperties;
}
/**
* Parses the passed-in properties, renaming and saving eligible properties in this object.
*
* Renames the propel.xxx properties to just xxx and renames any xxx.yyy properties
* to xxxYyy as PHP doesn't like the xxx.yyy syntax.
*
* @param mixed $props Array or Iterator
*/
public function setBuildProperties($props)
{
$this->buildProperties = array();
$renamedPropelProps = array();
foreach ($props as $key => $propValue) {
if (strpos($key, "propel.") === 0) {
$newKey = substr($key, strlen("propel."));
$j = strpos($newKey, '.');
while ($j !== false) {
$newKey = substr($newKey, 0, $j) . ucfirst(substr($newKey, $j + 1));
$j = strpos($newKey, '.');
}
$this->setBuildProperty($newKey, $propValue);
}
}
}
/**
* Gets a specific propel (renamed) property from the build.
*
* @param string $name
*
* @return mixed
*/
public function getBuildProperty($name)
{
return isset($this->buildProperties[$name]) ? $this->buildProperties[$name] : null;
}
/**
* Sets a specific propel (renamed) property from the build.
*
* @param string $name
* @param mixed $value
*/
public function setBuildProperty($name, $value)
{
$this->buildProperties[$name] = $value;
}
/**
* Resolves and returns the class name based on the specified property value.
*
* @param string $propname The name of the property that holds the class path (dot-path notation).
*
* @return string The class name.
* @throws BuildException If the classname cannot be determined or class cannot be loaded.
*/
public function getClassname($propname)
{
$classpath = $this->getBuildProperty($propname);
if (null === $classpath) {
throw new BuildException("Unable to find class path for '$propname' property.");
}
// This is a slight hack to workaround camel case inconsistencies for the DataSQL classes.
// Basically, we want to turn ?.?.?.sqliteDataSQLBuilder into ?.?.?.SqliteDataSQLBuilder
$lastdotpos = strrpos($classpath, '.');
if ($lastdotpos !== false) {
$classpath[$lastdotpos + 1] = strtoupper($classpath[$lastdotpos + 1]);
} else {
// Allows to configure full classname instead of a dot-path notation
if (class_exists($classpath)) {
return $classpath;
}
$classpath = ucfirst($classpath);
}
if (empty($classpath)) {
throw new BuildException("Unable to find class path for '$propname' property.");
}
$clazz = Phing::import($classpath);
return $clazz;
}
/**
* Resolves and returns the builder class name.
*
* @param string $type
*
* @return string The class name.
*/
public function getBuilderClassname($type)
{
$propname = 'builder' . ucfirst(strtolower($type)) . 'Class';
return $this->getClassname($propname);
}
/**
* Creates and configures a new Platform class.
*
* @param PDO $con
*
* @return Platform
* @throws BuildException
*/
public function getConfiguredPlatform(PDO $con = null, $database = null)
{
$buildConnection = $this->getBuildConnection($database);
//First try to load platform from the user provided build properties
if ($this->getBuildProperty('platformClass')) {
// propel.platform.class = platform.${propel.database}Platform by default
$clazz = $this->getClassname('platformClass');
} elseif (null !== $buildConnection['adapter']) {
$clazz = Phing::import('platform.' . ucfirst($buildConnection['adapter']) . 'Platform');
} else {
return null;
}
$platform = new $clazz();
if (!$platform instanceof PropelPlatformInterface) {
throw new BuildException("Specified platform class ($clazz) does not implement teh PropelPlatformInterface interface.");
}
if ($this->getBuildProperty('disableIdentifierQuoting')) {
$platform->setIdentifierQuoting(false);
} else {
$platform->setIdentifierQuoting(true);
}
$platform->setConnection($con);
$platform->setGeneratorConfig($this);
return $platform;
}
/**
* Creates and configures a new SchemaParser class for specified platform.
*
* @param PDO $con
*
* @return SchemaParser
* @throws BuildException
*/
public function getConfiguredSchemaParser(PDO $con = null)
{
$clazz = $this->getClassname("reverseParserClass");
$parser = new $clazz();
if (!$parser instanceof SchemaParser) {
throw new BuildException("Specified platform class ($clazz) does implement SchemaParser interface.", $this->getLocation());
}
$parser->setConnection($con);
$parser->setMigrationTable($this->getBuildProperty('migrationTable'));
$parser->setGeneratorConfig($this);
return $parser;
}
/**
* Gets a configured data model builder class for specified table and based on type.
*
* @param Table $table
* @param string $type The type of builder ('ddl', 'sql', etc.)
*
* @return DataModelBuilder
*/
public function getConfiguredBuilder(Table $table, $type, $cache = true)
{
$classname = $this->getBuilderClassname($type);
$builder = new $classname($table);
$builder->setGeneratorConfig($this);
return $builder;
}
/**
* Gets a configured Pluralizer class.
*
* @return Pluralizer
*/
public function getConfiguredPluralizer()
{
$classname = $this->getBuilderClassname('pluralizer');
$pluralizer = new $classname();
return $pluralizer;
}
/**
* Gets a configured behavior class
*
* @param string $name a behavior name
*
* @return string a behavior class name
*/
public function getConfiguredBehavior($name)
{
$propname = 'behavior' . ucfirst(strtolower($name)) . 'Class';
try {
$ret = $this->getClassname($propname);
} catch (BuildException $e) {
// class path not configured
$ret = false;
}
return $ret;
}
public function setBuildConnections($buildConnections)
{
$this->buildConnections = $buildConnections;
}
public function getBuildConnections()
{
if (null === $this->buildConnections) {
$buildTimeConfigPath = $this->getBuildProperty('buildtimeConfFile') ? $this->getBuildProperty('projectDir') . DIRECTORY_SEPARATOR . $this->getBuildProperty('buildtimeConfFile') : null;
if ($buildTimeConfigString = $this->getBuildProperty('buildtimeConf')) {
// configuration passed as propel.buildtimeConf string
// probably using the command line, which doesn't accept whitespace
// therefore base64 encoded
$this->parseBuildConnections(base64_decode($buildTimeConfigString));
} elseif (file_exists($buildTimeConfigPath)) {
// configuration stored in a buildtime-conf.xml file
$this->parseBuildConnections(file_get_contents($buildTimeConfigPath));
} else {
$this->buildConnections = array();
}
}
return $this->buildConnections;
}
protected function parseBuildConnections($xmlString)
{
$conf = simplexml_load_string($xmlString);
$this->defaultBuildConnection = (string) $conf->propel->datasources['default'];
$buildConnections = array();
foreach ($conf->propel->datasources->datasource as $datasource) {
$buildConnections[(string) $datasource['id']] = array(
'adapter' => (string) $datasource->adapter,
'dsn' => (string) $datasource->connection->dsn,
'user' => (string) $datasource->connection->user,
'password' => (string) $datasource->connection->password,
);
}
$this->buildConnections = $buildConnections;
}
public function getBuildConnection($databaseName = null)
{
$connections = $this->getBuildConnections();
if (null === $databaseName) {
$databaseName = $this->defaultBuildConnection;
}
if (isset($connections[$databaseName])) {
return $connections[$databaseName];
} else {
// fallback to the single connection from build.properties
return array(
'adapter' => $this->getBuildProperty('databaseAdapter'),
'dsn' => $this->getBuildProperty('databaseUrl'),
'user' => $this->getBuildProperty('databaseUser'),
'password' => $this->getBuildProperty('databasePassword'),
);
}
}
public function getBuildPDO($database)
{
$buildConnection = $this->getBuildConnection($database);
$dsn = str_replace("@DB@", $database, $buildConnection['dsn']);
// Set user + password to null if they are empty strings or missing
$username = isset($buildConnection['user']) && $buildConnection['user'] ? $buildConnection['user'] : null;
$password = isset($buildConnection['password']) && $buildConnection['password'] ? $buildConnection['password'] : null;
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
}