-
Notifications
You must be signed in to change notification settings - Fork 156
/
AbstractCommand.php
648 lines (544 loc) · 20.5 KB
/
AbstractCommand.php
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
/**
* Wrapper for Propel commands.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author William DURAND <william.durand1@gmail.com>
*/
abstract class AbstractCommand extends ContainerAwareCommand
{
/**
* Additional Phing args to add in specialized commands.
* @var array
*/
protected $additionalPhingArgs = array();
/**
* Temporary XML schemas used on command execution.
* @var array
*/
protected $tempSchemas = array();
/**
* @var string
*/
protected $cacheDir = null;
/**
* The Phing output.
* @string
*/
protected $buffer = null;
/**
* @var Symfony\Component\HttpKernel\Bundle\BundleInterface
*/
protected $bundle = null;
/**
* @var Boolean
*/
private $alreadyWroteConnection = false;
/**
*
* @var InputInterface
*/
protected $input;
/**
* Return the package prefix for a given bundle.
*
* @param Bundle $bundle
* @param string $baseDirectory The base directory to exclude from prefix.
*
* @return string
*/
protected function getPackagePrefix(Bundle $bundle, $baseDirectory = '')
{
$parts = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath()));
$length = count(explode('\\', $bundle->getNamespace())) * (-1);
$prefix = implode(DIRECTORY_SEPARATOR, array_slice($parts, 0, $length));
$prefix = ltrim(str_replace($baseDirectory, '', $prefix), DIRECTORY_SEPARATOR);
if (!empty($prefix)) {
$prefix = str_replace(DIRECTORY_SEPARATOR, '.', $prefix).'.';
}
return $prefix;
}
/**
* {@inheritdoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
parent::initialize($input, $output);
$this->input = $input;
$this->checkConfiguration();
if ($input->hasArgument('bundle') && '@' === substr($input->getArgument('bundle'), 0, 1)) {
$this->bundle = $this
->getContainer()
->get('kernel')
->getBundle(substr($input->getArgument('bundle'), 1));
}
}
/**
* Call a Phing task.
*
* @param string $taskName A Propel task name.
* @param array $properties An array of properties to pass to Phing.
*/
protected function callPhing($taskName, $properties = array())
{
$kernel = $this->getApplication()->getKernel();
if (isset($properties['propel.schema.dir'])) {
$this->cacheDir = $properties['propel.schema.dir'];
} else {
$this->cacheDir = $kernel->getCacheDir().'/propel';
$filesystem = new Filesystem();
$filesystem->remove($this->cacheDir);
$filesystem->mkdir($this->cacheDir);
}
$this->copySchemas($kernel, $this->cacheDir);
// build.properties
$this->createBuildPropertiesFile($kernel, $this->cacheDir.'/build.properties');
// buildtime-conf.xml
$this->createBuildTimeFile($this->cacheDir.'/buildtime-conf.xml');
// Verbosity
$bufferPhingOutput = $this->getContainer()->getParameter('kernel.debug');
// Phing arguments
$args = $this->getPhingArguments($kernel, $this->cacheDir, $properties);
// Add any arbitrary arguments last
foreach ($this->additionalPhingArgs as $arg) {
if (in_array($arg, array('verbose', 'debug'))) {
$bufferPhingOutput = false;
}
$args[] = '-'.$arg;
}
$args[] = $taskName;
// Enable output buffering
Phing::setOutputStream(new \OutputStream(fopen('php://output', 'w')));
Phing::setErrorStream(new \OutputStream(fopen('php://output', 'w')));
Phing::startup();
Phing::setProperty('phing.home', getenv('PHING_HOME'));
ob_start();
$phing = new Phing();
$returnStatus = true; // optimistic way
try {
$phing->execute($args);
$phing->runBuild();
$this->buffer = ob_get_contents();
// Guess errors
if (strstr($this->buffer, 'failed. Aborting.') ||
strstr($this->buffer, 'Failed to execute') ||
strstr($this->buffer, 'failed for the following reason:')) {
$returnStatus = false;
}
} catch (\Exception $e) {
$returnStatus = false;
}
if ($bufferPhingOutput) {
ob_end_clean();
} else {
ob_end_flush();
}
return $returnStatus;
}
/**
* @param KernelInterface $kernel The application kernel.
*/
protected function copySchemas(KernelInterface $kernel, $cacheDir)
{
$filesystem = new Filesystem();
if (!is_dir($cacheDir)) {
$filesystem->mkdir($cacheDir);
}
$base = ltrim(realpath($kernel->getRootDir().'/..'), DIRECTORY_SEPARATOR);
$finalSchemas = $this->getFinalSchemas($kernel, $this->bundle);
foreach ($finalSchemas as $schema) {
list($bundle, $finalSchema) = $schema;
$packagePrefix = $this->getPackagePrefix($bundle, $base);
$tempSchema = $bundle->getName().'-'.$finalSchema->getBaseName();
$this->tempSchemas[$tempSchema] = array(
'bundle' => $bundle->getName(),
'basename' => $finalSchema->getBaseName(),
'path' => $finalSchema->getPathname(),
);
$file = $cacheDir.DIRECTORY_SEPARATOR.$tempSchema;
$filesystem->copy((string) $finalSchema, $file, true);
// the package needs to be set absolute
// besides, the automated namespace to package conversion has
// not taken place yet so it needs to be done manually
$database = simplexml_load_file($file);
if (isset($database['package'])) {
// Do not use the prefix!
// This is used to override the package resulting from namespace conversion.
$database['package'] = $database['package'];
} elseif (isset($database['namespace'])) {
$database['package'] = $packagePrefix . str_replace('\\', '.', $database['namespace']);
} else {
throw new \RuntimeException(
sprintf('%s : Please define a `package` attribute or a `namespace` attribute for schema `%s`',
$bundle->getName(), $finalSchema->getBaseName())
);
}
if ($this->input && $this->input->hasOption('connection') && $this->input->getOption('connection')
&& $database['name'] != $this->input->getOption('connection')) {
//we skip this schema because the connection name doesn't match the input value
unset($this->tempSchemas[$tempSchema]);
$filesystem->remove($file);
continue;
}
foreach ($database->table as $table) {
if (isset($table['package'])) {
$table['package'] = $table['package'];
} elseif (isset($table['namespace'])) {
$table['package'] = $packagePrefix . str_replace('\\', '.', $table['namespace']);
} else {
$table['package'] = $database['package'];
}
}
file_put_contents($file, $database->asXML());
}
}
/**
* Return a list of final schema files that will be processed.
*
* @param \Symfony\Component\HttpKernel\KernelInterface $kernel
*
* @return array
*/
protected function getFinalSchemas(KernelInterface $kernel, BundleInterface $bundle = null)
{
if (null !== $bundle) {
return $this->getSchemasFromBundle($bundle);
}
$finalSchemas = array();
foreach ($kernel->getBundles() as $bundle) {
$finalSchemas = array_merge($finalSchemas, $this->getSchemasFromBundle($bundle));
}
return $finalSchemas;
}
/**
* @param \Symfony\Component\HttpKernel\Bundle\BundleInterface
*/
protected function getSchemasFromBundle(BundleInterface $bundle)
{
$finalSchemas = array();
if (is_dir($dir = $bundle->getPath().'/Resources/config')) {
$finder = new Finder();
$schemas = $finder->files()->name('*schema.xml')->followLinks()->in($dir);
if (iterator_count($schemas)) {
foreach ($schemas as $schema) {
$logicalName = $this->transformToLogicalName($schema, $bundle);
$finalSchema = new \SplFileInfo($this->getFileLocator()->locate($logicalName));
$finalSchemas[(string) $finalSchema] = array($bundle, $finalSchema);
}
}
}
return $finalSchemas;
}
/**
* @param \SplFileInfo $file
* @return string
*/
protected function getRelativeFileName(\SplFileInfo $file)
{
return substr(str_replace(realpath($this->getContainer()->getParameter('kernel.root_dir') . '/../'), '', $file), 1);
}
/**
* Create a 'build.properties' file.
*
* @param KernelInterface $kernel The application kernel.
* @param string $file Should be 'build.properties'.
*/
protected function createBuildPropertiesFile(KernelInterface $kernel, $file)
{
$filesystem = new Filesystem();
$buildPropertiesFile = $kernel->getRootDir().'/config/propel.ini';
if (file_exists($buildPropertiesFile)) {
$filesystem->copy($buildPropertiesFile, $file);
} else {
$filesystem->touch($file);
}
}
/**
* Create an XML file which represents propel.configuration
*
* @param string $file Should be 'buildtime-conf.xml'.
*/
protected function createBuildTimeFile($file)
{
$container = $this->getContainer();
if (!$container->has('propel.configuration')) {
throw new \InvalidArgumentException('Could not find Propel configuration.');
}
$xml = strtr(<<<EOT
<?xml version="1.0"?>
<config>
<propel>
<datasources default="%default_connection%">
EOT
, array('%default_connection%' => $container->getParameter('propel.dbal.default_connection')));
$propelConfiguration = $container->get('propel.configuration');
foreach ($propelConfiguration['datasources'] as $name => $datasource) {
$xml .= strtr(<<<EOT
<datasource id="%name%">
<adapter>%adapter%</adapter>
<connection>
<dsn>%dsn%</dsn>
<user>%username%</user>
<password>%password%</password>
</connection>
</datasource>
EOT
, array(
'%name%' => $name,
'%adapter%' => $datasource['adapter'],
'%dsn%' => $datasource['connection']['dsn'],
'%username%' => $datasource['connection']['user'],
'%password%' => isset($datasource['connection']['password']) ? $datasource['connection']['password'] : '',
));
}
$xml .= <<<EOT
</datasources>
</propel>
</config>
EOT;
file_put_contents($file, $xml);
}
/**
* Returns an array of properties as key/value pairs from an input file.
*
* @param string $file A file properties.
* @return array An array of properties as key/value pairs.
*/
protected function getProperties($file)
{
$properties = array();
if (false === $lines = @file($file)) {
throw new \Exception(sprintf('Unable to parse contents of "%s".', $file));
}
foreach ($lines as $line) {
$line = trim($line);
if ('' == $line || in_array($line[0], array('#', ';'))) {
continue;
}
$pos = strpos($line, '=');
$property = trim(substr($line, 0, $pos));
$value = trim(substr($line, $pos + 1));
if ("true" === $value) {
$value = true;
} elseif ("false" === $value) {
$value = false;
}
$properties[$property] = $value;
}
return $properties;
}
/**
* Return the current Propel cache directory.
* @return string The current Propel cache directory.
*/
protected function getCacheDir()
{
return $this->cacheDir;
}
/**
* @return \Symfony\Component\Config\FileLocatorInterface
*/
protected function getFileLocator()
{
return $this->getContainer()->get('file_locator');
}
/**
* Get connection by checking the input option named 'connection'.
* Returns the default connection if no option specified or an exception
* if the specified connection doesn't exist.
*
* @param InputInterface $input
* @param OutputInterface $output
* @throw \InvalidArgumentException If the connection does not exist.
* @return array
*/
protected function getConnection(InputInterface $input, OutputInterface $output)
{
$propelConfiguration = $this->getContainer()->get('propel.configuration');
$name = $input->getOption('connection') ?: $this->getContainer()->getParameter('propel.dbal.default_connection');
if (isset($propelConfiguration['datasources'][$name])) {
$defaultConfig = $propelConfiguration['datasources'][$name];
} else {
throw new \InvalidArgumentException(sprintf('Connection named %s doesn\'t exist', $name));
}
if (false === $this->alreadyWroteConnection) {
$output->writeln(sprintf('Use connection named <comment>%s</comment> in <comment>%s</comment> environment.',
$name, $this->getApplication()->getKernel()->getEnvironment())
);
$this->alreadyWroteConnection = true;
}
// prevent errors
if (!isset($defaultConfig['connection']['password'])) {
$defaultConfig['connection']['password'] = null;
}
return array($name, $defaultConfig);
}
/**
* Extract the database name from a given DSN
*
* @param string $dsn A DSN
* @return string The database name extracted from the given DSN
*/
protected function parseDbName($dsn)
{
preg_match('#dbname=([a-zA-Z0-9\_]+)#', $dsn, $matches);
if (isset($matches[1])) {
return $matches[1];
}
// e.g. SQLite
return null;
}
/**
* Check the PropelConfiguration object.
*/
protected function checkConfiguration()
{
$parameters = $this->getContainer()->get('propel.configuration')->getParameters();
if (!isset($parameters['datasources']) || 0 === count($parameters['datasources'])) {
throw new \RuntimeException('Propel should be configured (no database configuration found).');
}
}
/**
* Write Propel output as summary based on a Regexp.
*
* @param OutputInterface $output The output object.
* @param string $taskname A task name
*/
protected function writeSummary(OutputInterface $output, $taskname)
{
foreach (explode("\n", $this->buffer) as $line) {
if (false !== strpos($line, '[' . $taskname . ']')) {
$arr = preg_split('#\[' . $taskname . '\] #', $line);
$info = $arr[1];
if ('"' === $info[0]) {
$info = sprintf('<info>%s</info>', $info);
}
$output->writeln($info);
}
}
}
/**
* Comes from the SensioGeneratorBundle.
* @see https://github.com/sensio/SensioGeneratorBundle/blob/master/Command/Helper/DialogHelper.php#L52
*
* @param OutputInterface $output The output.
* @param string $text A text message.
* @param string $style A style to apply on the section.
*/
protected function writeSection(OutputInterface $output, $text, $style = 'bg=blue;fg=white')
{
$output->writeln(array(
'',
$this->getHelperSet()->get('formatter')->formatBlock($text, $style, true),
'',
));
}
/**
* Renders an error message if a task has failed.
*
* @param OutputInterface $output The output.
* @param string $taskName A task name.
* @param Boolean $more Whether to add a 'more details' message or not.
*/
protected function writeTaskError($output, $taskName, $more = true)
{
$moreText = $more ? ' To get more details, run the command with the "--verbose" option.' : '';
return $this->writeSection($output, array(
'[Propel] Error',
'',
'An error has occured during the "' . $taskName . '" task process.' . $moreText
), 'fg=white;bg=red');
}
/**
* @param OutputInterface $output The output.
* @param string $filename The filename.
*/
protected function writeNewFile(OutputInterface $output, $filename)
{
$output->writeln('>> <info>File+</info> ' . $filename);
}
/**
* @param OutputInterface $output The output.
* @param string $directory The directory.
*/
protected function writeNewDirectory(OutputInterface $output, $directory)
{
$output->writeln('>> <info>Dir+</info> ' . $directory);
}
/**
* Ask confirmation from the user.
*
* @param OutputInterface $output The output.
* @param string $question A given question.
* @param string $default A default response.
*/
protected function askConfirmation(OutputInterface $output, $question, $default = null)
{
return $this->getHelperSet()->get('dialog')->askConfirmation($output, $question, $default);
}
/**
* @param \SplFileInfo $schema
* @param BundleInterface $bundle
* @return string
*/
protected function transformToLogicalName(\SplFileInfo $schema, BundleInterface $bundle)
{
$schemaPath = str_replace(
$bundle->getPath(). DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR,
'',
$schema->getRealPath()
);
return sprintf('@%s/Resources/config/%s', $bundle->getName(), $schemaPath);
}
/**
* Compiles arguments/properties for the Phing process.
* @return array
*/
private function getPhingArguments(KernelInterface $kernel, $workingDirectory, $properties)
{
$args = array();
// Default properties
$properties = array_merge(array(
'propel.database' => 'mysql',
'project.dir' => $workingDirectory,
'propel.output.dir' => $kernel->getRootDir().'/propel',
'propel.php.dir' => $kernel->getRootDir().'/..',
'propel.packageObjectModel' => true,
'propel.useDateTimeClass' => true,
'propel.dateTimeClass' => 'DateTime',
'propel.defaultTimeFormat' => '',
'propel.defaultDateFormat' => '',
'propel.addClassLevelComment' => false,
'propel.defaultTimeStampFormat' => '',
'propel.builder.pluralizer.class' => 'builder.util.StandardEnglishPluralizer',
), $properties);
// Adding user defined properties from the configuration
$properties = array_merge(
$properties,
$this->getContainer()->get('propel.build_properties')->getProperties()
);
foreach ($properties as $key => $value) {
$args[] = "-D$key=$value";
}
// Build file
$args[] = '-f';
$args[] = realpath($this->getContainer()->getParameter('propel.path').'/generator/build.xml');
return $args;
}
}