Skip to content
This repository
Browse code

Merge pull request #171 from Martiusweb/build_sql_update

sql:build take existing files into account
  • Loading branch information...
commit b70c73f674ad9c1d68ad24f83d8b5dec4f5f008c 2 parents abf90dd + 54c60cf
William Durand willdurand authored

Showing 1 changed file with 38 additions and 5 deletions. Show diff stats Hide diff stats

  1. +38 5 Command/SqlBuildCommand.php
43 Command/SqlBuildCommand.php
@@ -58,23 +58,36 @@ protected function execute(InputInterface $input, OutputInterface $output)
58 58 $filesystem = new Filesystem();
59 59
60 60 $sqlDir = $this->getApplication()->getKernel()->getRootDir(). DIRECTORY_SEPARATOR . 'propel'. DIRECTORY_SEPARATOR . 'sql';
  61 + $cacheDir = $this->getApplication()->getKernel()->getCacheDir(). DIRECTORY_SEPARATOR . 'sql';
61 62
62   - $filesystem->remove($sqlDir);
63   - $filesystem->mkdir($sqlDir);
  63 + $filesystem->remove($cacheDir);
  64 + $filesystem->mkdir($cacheDir);
64 65
65 66 // Execute the task
66 67 $ret = $this->callPhing('build-sql', array(
67   - 'propel.sql.dir' => $sqlDir
  68 + 'propel.sql.dir' => $cacheDir
68 69 ));
69 70
  71 + // Show the list of generated files
70 72 if (true === $ret) {
71   - $files = $finder->name('*')->in($sqlDir);
  73 + $files = $finder->name('*')->in($cacheDir);
72 74
73 75 $nbFiles = 0;
74 76 foreach ($files as $file) {
  77 + $fileExt = pathinfo($file->getFilename(), PATHINFO_EXTENSION);
  78 + $finalLocation = $sqlDir. DIRECTORY_SEPARATOR. $file->getFilename();
  79 +
  80 + if($fileExt === 'map' && $filesystem->exists($finalLocation)) {
  81 + $this->mergeMapFiles($finalLocation, (string) $file);
  82 + }
  83 + else {
  84 + $filesystem->remove($finalLocation);
  85 + $filesystem->rename((string) $file, $finalLocation);
  86 + }
  87 +
75 88 $this->writeNewFile($output, (string) $file);
76 89
77   - if ('sql' === pathinfo($file->getFilename(), PATHINFO_EXTENSION)) {
  90 + if ('sql' === $fileExt) {
78 91 $nbFiles++;
79 92 }
80 93 }
@@ -90,4 +103,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
90 103 ), 'fg=white;bg=red');
91 104 }
92 105 }
  106 +
  107 + /**
  108 + * Reads the existing target and the generated map files, and adds to the
  109 + * target the missing lines that are in the generated file.
  110 + *
  111 + * @param string $target target map filename
  112 + * @param string $generated generated map filename
  113 + *
  114 + * @return boolean result
  115 + */
  116 + protected function mergeMapFiles($target, $generated) {
  117 + if(($targetContent = file($target)) === false)
  118 + return false;
  119 + if(($generatedContent = file($generated)) === false)
  120 + return false;
  121 +
  122 + $targetContent = array_merge($generatedContent, array_diff($targetContent, $generatedContent));
  123 +
  124 + return file_put_contents($target, $targetContent);
  125 + }
93 126 }

0 comments on commit b70c73f

Please sign in to comment.
Something went wrong with that request. Please try again.