From 54c60cf4a19cf98d4bf6d23b130cd693277c2411 Mon Sep 17 00:00:00 2001 From: Martin Richard Date: Tue, 31 Jul 2012 11:08:35 +0200 Subject: [PATCH] sql:build take existing files into account Signed-off-by: Martin Richard --- Command/SqlBuildCommand.php | 43 ++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/Command/SqlBuildCommand.php b/Command/SqlBuildCommand.php index 43cdad1..4d6f41d 100644 --- a/Command/SqlBuildCommand.php +++ b/Command/SqlBuildCommand.php @@ -58,23 +58,36 @@ EOT $filesystem = new Filesystem(); $sqlDir = $this->getApplication()->getKernel()->getRootDir(). DIRECTORY_SEPARATOR . 'propel'. DIRECTORY_SEPARATOR . 'sql'; + $cacheDir = $this->getApplication()->getKernel()->getCacheDir(). DIRECTORY_SEPARATOR . 'sql'; - $filesystem->remove($sqlDir); - $filesystem->mkdir($sqlDir); + $filesystem->remove($cacheDir); + $filesystem->mkdir($cacheDir); // Execute the task $ret = $this->callPhing('build-sql', array( - 'propel.sql.dir' => $sqlDir + 'propel.sql.dir' => $cacheDir )); + // Show the list of generated files if (true === $ret) { - $files = $finder->name('*')->in($sqlDir); + $files = $finder->name('*')->in($cacheDir); $nbFiles = 0; foreach ($files as $file) { + $fileExt = pathinfo($file->getFilename(), PATHINFO_EXTENSION); + $finalLocation = $sqlDir. DIRECTORY_SEPARATOR. $file->getFilename(); + + if($fileExt === 'map' && $filesystem->exists($finalLocation)) { + $this->mergeMapFiles($finalLocation, (string) $file); + } + else { + $filesystem->remove($finalLocation); + $filesystem->rename((string) $file, $finalLocation); + } + $this->writeNewFile($output, (string) $file); - if ('sql' === pathinfo($file->getFilename(), PATHINFO_EXTENSION)) { + if ('sql' === $fileExt) { $nbFiles++; } } @@ -90,4 +103,24 @@ EOT ), 'fg=white;bg=red'); } } + + /** + * Reads the existing target and the generated map files, and adds to the + * target the missing lines that are in the generated file. + * + * @param string $target target map filename + * @param string $generated generated map filename + * + * @return boolean result + */ + protected function mergeMapFiles($target, $generated) { + if(($targetContent = file($target)) === false) + return false; + if(($generatedContent = file($generated)) === false) + return false; + + $targetContent = array_merge($generatedContent, array_diff($targetContent, $generatedContent)); + + return file_put_contents($target, $targetContent); + } }