Skip to content

Commit

Permalink
Introduced the sql:build command
Browse files Browse the repository at this point in the history
Added a `Filesystem` class (thanks to Symfony2), added `Finder`
(Symfony2 component) as a dependency, added a bunch of new named
exceptions
  • Loading branch information
willdurand committed Dec 22, 2011
1 parent 476c9c2 commit b141fdd
Show file tree
Hide file tree
Showing 13 changed files with 826 additions and 29 deletions.
1 change: 1 addition & 0 deletions bin/propel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@

$app = new Application('Propel', '2.0 (dev)');
$app->add(new \Propel\Generator\Command\TestPrepare());
$app->add(new \Propel\Generator\Command\SqlBuild());
$app->run();
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"php": ">=5.3.2",
"symfony/yaml": ">=2.0",
"symfony/console": ">=2.0",
"monolog/monolog": ">=1.0.2"
"monolog/monolog": ">=1.0.2",
"symfony/finder": ">=2.0"
},
"autoload": {
"psr-0": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions src/Propel/Generator/Command/SqlBuild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

/**
* This file is part of the Propel 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\Generator\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Finder\Finder;

use Propel\Generator\Config\GeneratorConfig;
use Propel\Generator\Manager\SqlManager;
use Propel\Generator\Util\Filesystem;

/**
* @author William Durand <william.durand1@gmail.com>
*/
class SqlBuild extends Command
{
const DEFAULT_INPUT_DIRECTORY = '.';

const DEFAULT_OUTPUT_DIRECTORY = 'generated-sql';

const DEFAULT_PLATFORM = 'MysqlPlatform';

/**
* @var \Symfony\Component\Console\Output\OutputInterface
*/
private $output;

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setDefinition(array(
new InputOption('input-dir', null, InputOption::VALUE_REQUIRED, 'The input directory', self::DEFAULT_INPUT_DIRECTORY),
new InputOption('output-dir', null, InputOption::VALUE_REQUIRED, 'The output directory', self::DEFAULT_OUTPUT_DIRECTORY),
new InputOption('platform', null, InputOption::VALUE_REQUIRED, 'The platform', self::DEFAULT_PLATFORM),
new InputOption('validate', null, InputOption::VALUE_NONE, '')
))
->setName('sql:build')
->setDescription('Build SQL files')
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$manager = new SqlManager();

$buildProperties = realpath($input->getOption('input-dir') . DIRECTORY_SEPARATOR . 'build.properties');
$defaultProperties = realpath(__DIR__.'/../../../../tools/generator/default.properties');

$generatorConfig = new GeneratorConfig(array_merge(
$this->getBuildProperties($defaultProperties),
$this->getBuildProperties($buildProperties),
array(
'propel.platform.class' => $input->getOption('platform'),
)
));

$finder = new Finder();
$files = $finder
->name('*schema.xml')
->in($input->getOption('input-dir'))
->depth(0)
->files()
;

$filesystem = new Filesystem();
$filesystem->mkdir($input->getOption('output-dir'));

$manager->setValidate($input->getOption('validate'));
$manager->setGeneratorConfig($generatorConfig);
$manager->setIncludedFiles($files);
$manager->setLoggerClosure(function($message) use ($output) {
$output->writeln($message);
});
$manager->setWorkingDirectory($input->getOption('output-dir'));

$manager->buildSql();
}

protected function getBuildProperties($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, '=');
$properties[trim(substr($line, 0, $pos))] = trim(substr($line, $pos + 1));
}

return $properties;
}
}
15 changes: 15 additions & 0 deletions src/Propel/Generator/Exception/BuildException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* This file is part of the Propel 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\Generator\Exception;

class BuildException extends RuntimeException implements ExceptionInterface
{
}
18 changes: 18 additions & 0 deletions src/Propel/Generator/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* This file is part of the Propel 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\Generator\Exception;

/**
* @author William Durand <william.durand1@gmail.com>
*/
interface ExceptionInterface
{
}
15 changes: 15 additions & 0 deletions src/Propel/Generator/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* This file is part of the Propel 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\Generator\Exception;

class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{
}
15 changes: 15 additions & 0 deletions src/Propel/Generator/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* This file is part of the Propel 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\Generator\Exception;

class RuntimeException extends \RuntimeException implements ExceptionInterface
{
}
3 changes: 1 addition & 2 deletions src/Propel/Generator/Exception/SchemaException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

/**
* Class for exceptions thrown during schema parsing
*
*/
class SchemaException extends Exception
class SchemaException extends Exception implements ExceptionInterface
{
}
Loading

0 comments on commit b141fdd

Please sign in to comment.