Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 133 lines (109 sloc) 4.252 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
<?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\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

use Propel\PropelBundle\Command\AbstractCommand;

/**
* SqlBuildCommand.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author William DURAND <william.durand1@gmail.com>
*/
class SqlBuildCommand extends AbstractCommand
{
    /**
* @see Command
*/
    protected function configure()
    {
        $this
            ->setDescription('Build the SQL generation code for all tables based on Propel XML schemas')
            ->setHelp(<<<EOT
The <info>%command.name%</info> command builds the SQL table generation code based on the XML schemas defined in all Bundles.

<info>php %command.full_name%</info>
EOT
            )
            ->setName('propel:sql:build')
        ;
    }

    /**
* @see Command
*
* @throws \InvalidArgumentException When the target directory does not exist
*/
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if ($input->getOption('verbose')) {
            $this->additionalPhingArgs[] = 'verbose';
        }

        $finder = new Finder();
        $filesystem = new Filesystem();

        $sqlDir = $this->getApplication()->getKernel()->getRootDir(). DIRECTORY_SEPARATOR . 'propel'. DIRECTORY_SEPARATOR . 'sql';
        $cacheDir = $this->getApplication()->getKernel()->getCacheDir(). DIRECTORY_SEPARATOR . 'sql';

        $filesystem->remove($cacheDir);
        $filesystem->mkdir($cacheDir);

        if (!$filesystem->exists($sqlDir)) {
            $filesystem->mkdir($sqlDir);
        }

        // Execute the task
        $ret = $this->callPhing('build-sql', array(
            'propel.sql.dir' => $cacheDir
        ));

        // Show the list of generated files
        if (true === $ret) {
            $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' === $fileExt) {
                    $nbFiles++;
                }
            }

            $output->writeln(sprintf('<comment>%d</comment> <info>SQL file%s ha%s been generated.</info>',
                $nbFiles, $nbFiles > 1 ? 's' : '', $nbFiles > 1 ? 've' : 's'
            ));
        } else {
            $this->writeSection($output, array(
                '[Propel] Error',
                '',
                'An error has occured during the "propel:sql:build" command process. To get more details, run the command with the "--verbose" option.'
            ), '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);
    }
}
Something went wrong with that request. Please try again.