Permalink
Browse files

[Command] Refactored commands by adding an AbstractCommand class

  • Loading branch information...
willdurand committed Dec 21, 2011
1 parent cc4ea67 commit b1dc3fb0de265f21629bf40cd0fccd4ea719ee61
@@ -0,0 +1,70 @@
+<?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\InputInterface;
+use Symfony\Component\Finder\Finder;
+
+/**
+ * @author William Durand <william.durand1@gmail.com>
+ */
+abstract class AbstractCommand extends Command
+{
+ const DEFAULT_INPUT_DIRECTORY = '.';
+
+ const DEFAULT_PLATFORM = 'MysqlPlatform';
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function configure()
+ {
+ $this
+ ->setDefinition(array(
+ new InputOption('input-dir', null, InputOption::VALUE_REQUIRED, 'The input directory', self::DEFAULT_INPUT_DIRECTORY),
+ new InputOption('platform', null, InputOption::VALUE_REQUIRED, 'The platform', self::DEFAULT_PLATFORM),
+ ));
+ }
+
+ 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;
+ }
+
+ protected function getSchemas(InputInterface $input)
+ {
+ $finder = new Finder();
+ return $finder
+ ->name('*schema.xml')
+ ->in($input->getOption('input-dir'))
+ ->depth(0)
+ ->files()
+ ;
+ }
+}
@@ -19,12 +19,8 @@
*/
class ModelBuild extends Command
{
- const DEFAULT_INPUT_DIRECTORY = '.';
-
const DEFAULT_OUTPUT_DIRECTORY = 'generated-classes';
- const DEFAULT_PLATFORM = 'MysqlPlatform';
-
const DEFAULT_PEER_BUILDER = '\Propel\Generator\Builder\Om\PeerBuilder';
const DEFAULT_PEER_STUB_BUILDER = '\Propel\Generator\Builder\Om\ExtensionPeerBuilder';
@@ -54,9 +50,7 @@ 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),
))
->addOption('peer-class', null, InputOption::VALUE_REQUIRED,
'The peer class generator name', self::DEFAULT_PEER_BUILDER)
@@ -92,7 +86,8 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
- $manager = new ModelManager();
+ parent::configure();
+
$generatorConfig = new GeneratorConfig(array(
'propel.platform.class' => $input->getOption('platform'),
'propel.builder.peer.class' => $input->getOption('peer-class'),
@@ -109,19 +104,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
'propel.disableIdentifierQuoting' => $input->getOption('disable-identifier-quoting'),
));
- $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 = new ModelManager();
$manager->setGeneratorConfig($generatorConfig);
- $manager->setSchemas($files);
+ $manager->setSchemas($this->getSchemas());
$manager->setLoggerClosure(function($message) use ($input, $output) {
if ($input->getOption('verbose')) {
$output->writeln($message);
@@ -10,12 +10,12 @@
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;
@@ -24,24 +24,21 @@
/**
* @author William Durand <william.durand1@gmail.com>
*/
-class SqlBuild extends Command
+class SqlBuild extends AbstractCommand
{
- const DEFAULT_INPUT_DIRECTORY = '.';
-
const DEFAULT_OUTPUT_DIRECTORY = 'generated-sql';
- const DEFAULT_PLATFORM = 'MysqlPlatform';
-
const DEFAULT_MYSQL_ENGINE = 'MyISAM';
/**
* {@inheritdoc}
*/
protected function configure()
{
+ parent::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('validate', null, InputOption::VALUE_NONE, ''),
new InputOption('platform', null, InputOption::VALUE_REQUIRED, 'The platform', self::DEFAULT_PLATFORM),
@@ -61,7 +58,6 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
- $manager = new SqlManager();
$generatorConfig = new GeneratorConfig(array(
'propel.platform.class' => $input->getOption('platform'),
'propel.database.schema' => $input->getOption('schema-name'),
@@ -71,20 +67,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
'propel.mysql.tableType' => $input->getOption('mysql-engine'),
));
- $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 = new SqlManager();
$manager->setValidate($input->getOption('validate'));
$manager->setGeneratorConfig($generatorConfig);
- $manager->setSchemas($files);
+ $manager->setSchemas($this->getSchemas());
$manager->setLoggerClosure(function($message) use ($input, $output) {
if ($input->getOption('verbose')) {
$output->writeln($message);
@@ -94,26 +83,4 @@ protected function execute(InputInterface $input, OutputInterface $output)
$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;
- }
}

0 comments on commit b1dc3fb

Please sign in to comment.