propel-bundle/Command/SqlBuildCommand.php

131 lines
4.2 KiB
PHP
Raw Normal View History

2010-10-28 14:41:03 +02:00
<?php
2011-08-30 23:29:49 +02:00
/**
* 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;
2010-10-28 14:41:03 +02:00
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
2010-10-28 14:41:03 +02:00
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
2011-12-25 14:21:54 +01:00
use Symfony\Component\Filesystem\Filesystem;
2011-08-31 16:12:56 +02:00
use Symfony\Component\Finder\Finder;
2010-10-28 14:41:03 +02:00
2012-04-20 13:54:05 +02:00
use Propel\PropelBundle\Command\AbstractCommand;
2011-08-31 16:12:56 +02:00
2010-10-28 14:41:03 +02:00
/**
2012-04-07 21:58:45 +02:00
* SqlBuildCommand.
2010-10-28 14:41:03 +02:00
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
2011-03-25 16:24:34 +01:00
* @author William DURAND <william.durand1@gmail.com>
2010-10-28 14:41:03 +02:00
*/
2012-04-20 13:54:05 +02:00
class SqlBuildCommand extends AbstractCommand
2010-10-28 14:41:03 +02:00
{
/**
* @see Command
*/
protected function configure()
{
$this
->setDescription('Build the SQL generation code for all tables based on Propel XML schemas')
->setHelp(<<<EOT
2012-04-07 21:58:45 +02:00
The <info>%command.name%</info> command builds the SQL table generation code based on the XML schemas defined in all Bundles.
2010-10-28 14:41:03 +02:00
2012-04-07 21:58:45 +02:00
<info>php %command.full_name%</info>
2010-10-28 14:41:03 +02:00
EOT
)
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'Set this parameter to define a connection to use')
2012-04-07 21:58:45 +02:00
->setName('propel:sql:build')
2010-10-28 14:41:03 +02:00
;
}
/**
* @see Command
*
* @throws \InvalidArgumentException When the target directory does not exist
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
2011-08-31 16:12:56 +02:00
$finder = new Finder();
$filesystem = new Filesystem();
2011-03-26 12:35:06 +01:00
2011-08-31 16:12:56 +02:00
$sqlDir = $this->getApplication()->getKernel()->getRootDir(). DIRECTORY_SEPARATOR . 'propel'. DIRECTORY_SEPARATOR . 'sql';
$cacheDir = $this->getApplication()->getKernel()->getCacheDir(). DIRECTORY_SEPARATOR . 'sql';
2011-08-30 16:10:52 +02:00
$filesystem->remove($cacheDir);
$filesystem->mkdir($cacheDir);
2011-08-30 16:10:52 +02:00
2012-08-23 16:17:56 +02:00
if (!$filesystem->exists($sqlDir)) {
$filesystem->mkdir($sqlDir);
}
2011-08-31 16:12:56 +02:00
// Execute the task
$ret = $this->callPhing('build-sql', array(
'propel.sql.dir' => $cacheDir
2011-08-31 16:12:56 +02:00
));
2011-04-19 14:14:20 +02:00
// Show the list of generated files
2011-08-31 16:12:56 +02:00
if (true === $ret) {
$files = $finder->name('*')->in($cacheDir);
2011-08-31 16:12:56 +02:00
$nbFiles = 0;
foreach ($files as $file) {
$fileExt = pathinfo($file->getFilename(), PATHINFO_EXTENSION);
$finalLocation = $sqlDir. DIRECTORY_SEPARATOR. $file->getFilename();
2012-08-23 16:17:56 +02:00
if ($fileExt === 'map' && $filesystem->exists($finalLocation)) {
$this->mergeMapFiles($finalLocation, (string) $file);
2012-08-23 16:17:56 +02:00
} else {
$filesystem->remove($finalLocation);
$filesystem->rename((string) $file, $finalLocation);
}
2011-08-31 16:12:56 +02:00
$this->writeNewFile($output, (string) $file);
if ('sql' === $fileExt) {
2011-08-31 16:12:56 +02:00
$nbFiles++;
}
}
2011-03-26 12:35:06 +01:00
2012-06-24 01:09:07 +02:00
$output->writeln(sprintf('<comment>%d</comment> <info>SQL file%s ha%s been generated.</info>',
$nbFiles, $nbFiles > 1 ? 's' : '', $nbFiles > 1 ? 've' : 's'
));
} else {
2011-08-30 16:10:52 +02:00
$this->writeSection($output, array(
'[Propel] Error',
'',
2012-04-20 02:27:47 +02:00
'An error has occured during the "propel:sql:build" command process. To get more details, run the command with the "--verbose" option.'
2011-08-30 16:10:52 +02:00
), 'fg=white;bg=red');
}
2010-10-28 14:41:03 +02:00
}
/**
2012-08-23 16:17:56 +02:00
* Reads the existing target and the generated map files, and adds to the
* target the missing lines that are in the generated file.
*
2012-08-23 16:17:56 +02:00
* @param string $target target map filename
* @param string $generated generated map filename
*
* @return boolean result
*/
2012-08-23 16:17:56 +02:00
protected function mergeMapFiles($target, $generated)
{
if(($targetContent = file($target)) === false)
2012-08-23 16:17:56 +02:00
return false;
if(($generatedContent = file($generated)) === false)
2012-08-23 16:17:56 +02:00
return false;
$targetContent = array_merge($generatedContent, array_diff($targetContent, $generatedContent));
return file_put_contents($target, $targetContent);
}
2010-10-28 14:41:03 +02:00
}