Skip to content

Commit

Permalink
Introduced model:build command
Browse files Browse the repository at this point in the history
Refactored Managers to be more generic
  • Loading branch information
willdurand committed Dec 22, 2011
1 parent d08238e commit 16d77be
Show file tree
Hide file tree
Showing 6 changed files with 373 additions and 82 deletions.
1 change: 1 addition & 0 deletions bin/propel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
$app = new Application('Propel', '2.0 (dev)');
$app->add(new \Propel\Generator\Command\TestPrepare());
$app->add(new \Propel\Generator\Command\SqlBuild());
$app->add(new \Propel\Generator\Command\ModelBuild());
$app->run();
132 changes: 132 additions & 0 deletions src/Propel/Generator/Command/ModelBuild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

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\ModelManager;
use Propel\Generator\Util\Filesystem;

/**
* @author Florian Klein <florian.klein@free.fr>
* @author William Durand <william.durand1@gmail.com>
*/
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';

const DEFAULT_OBJECT_BUILDER = '\Propel\Generator\Builder\Om\ObjectBuilder';

const DEFAULT_OBJECT_STUB_BUILDER = '\Propel\Generator\Builder\Om\ExtensionObjectBuilder';

const DEFAULT_MULTIEXTEND_OBJECT_BUILDER = '\Propel\Generator\Builder\Om\MultiExtendObjectBuilder';

const DEFAULT_QUERY_BUILDER = '\Propel\Generator\Builder\Om\QueryBuilder';

const DEFAULT_QUERY_STUB_BUILDER = '\Propel\Generator\Builder\Om\ExtensionQueryBuilder';

const DEFAULT_QUERY_INHERITANCE_BUILDER = '\Propel\Generator\Builder\Om\QueryInheritanceBuilder';

const DEFAULT_QUERY_INHERITANCE_STUB_BUILDER = '\Propel\Generator\Builder\Om\ExtensionQueryInheritanceBuilder';

const DEFAULT_TABLEMAP_BUILDER = '\Propel\Generator\Builder\Om\TableMapBuilder';

const DEFAULT_PLURALIZER = '\Propel\Generator\Builder\Util\DefaultEnglishPluralizer';

/**
* {@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),
))
->addOption('peer-class', null, InputOption::VALUE_REQUIRED,
'The peer class generator name', self::DEFAULT_PEER_BUILDER)
->addOption('peer-stub-class', null, InputOption::VALUE_REQUIRED,
'The peer stub class generator name', self::DEFAULT_PEER_STUB_BUILDER)
->addOption('object-class', null, InputOption::VALUE_REQUIRED,
'The object class generator name', self::DEFAULT_OBJECT_BUILDER)
->addOption('object-stub-class', null, InputOption::VALUE_REQUIRED,
'The object stub class generator name', self::DEFAULT_OBJECT_STUB_BUILDER)
->addOption('object-multiextend-class', null, InputOption::VALUE_REQUIRED,
'The object multiextend class generator name', self::DEFAULT_MULTIEXTEND_OBJECT_BUILDER)
->addOption('query-class', null, InputOption::VALUE_REQUIRED,
'The query class generator name', self::DEFAULT_QUERY_BUILDER)
->addOption('query-stub-class', null, InputOption::VALUE_REQUIRED,
'The query stub class generator name', self::DEFAULT_QUERY_STUB_BUILDER)
->addOption('query-inheritance-class', null, InputOption::VALUE_REQUIRED,
'The query inheritance class generator name', self::DEFAULT_QUERY_INHERITANCE_BUILDER)
->addOption('query-inheritance-stub-class', null, InputOption::VALUE_REQUIRED,
'The query inheritance stub class generator name', self::DEFAULT_QUERY_INHERITANCE_STUB_BUILDER)
->addOption('tablemap-class', null, InputOption::VALUE_REQUIRED,
'The tablemap class generator name', self::DEFAULT_TABLEMAP_BUILDER)
->addOption('pluralizer-class', null, InputOption::VALUE_REQUIRED,
'The pluralizer class name', self::DEFAULT_PLURALIZER)
->addOption('disable-identifier-quoting', null, InputOption::VALUE_NONE,
'Identifier quoting may result in undesired behavior (especially in Postgres)')
->setName('model:build')
->setDescription('Build the model classes based on Propel XML schemas')
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$manager = new ModelManager();
$generatorConfig = new GeneratorConfig(array(
'propel.platform.class' => $input->getOption('platform'),
'propel.builder.peer.class' => $input->getOption('peer-class'),
'propel.builder.peerstub.class' => $input->getOption('peer-stub-class'),
'propel.builder.object.class' => $input->getOption('object-class'),
'propel.builder.objectstub.class' => $input->getOption('object-stub-class'),
'propel.builder.objectmultiextend.class' => $input->getOption('object-multiextend-class'),
'propel.builder.query.class' => $input->getOption('query-class'),
'propel.builder.querystub.class' => $input->getOption('query-stub-class'),
'propel.builder.queryinheritance.class' => $input->getOption('query-inheritance-class'),
'propel.builder.queryinheritancestub.class' => $input->getOption('query-inheritance-stub-class'),
'propel.builder.tablemap.class' => $input->getOption('tablemap-class'),
'propel.builder.pluralizer.class' => $input->getOption('pluralizer-class'),
'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->setGeneratorConfig($generatorConfig);
$manager->setSchemas($files);
$manager->setLoggerClosure(function($message) use ($output) {
$output->writeln($message);
});
$manager->setWorkingDirectory($input->getOption('output-dir'));

$manager->build();
}
}
2 changes: 1 addition & 1 deletion src/Propel/Generator/Command/SqlBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$manager->setValidate($input->getOption('validate'));
$manager->setGeneratorConfig($generatorConfig);
$manager->setIncludedFiles($files);
$manager->setSchemas($files);
$manager->setLoggerClosure(function($message) use ($output) {
$output->writeln($message);
});
Expand Down
75 changes: 67 additions & 8 deletions src/Propel/Generator/Manager/AbstractManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
namespace Propel\Generator\Manager;

use Propel\Generator\Builder\Util\XmlToAppData;
use Propel\Generator\Config\GeneratorConfig;
use Propel\Generator\Config\GeneratorConfigInterface;
use Propel\Generator\Exception\BuildException;
use Propel\Generator\Exception\EngineException;
use Propel\Generator\Model\AppData;
use Propel\Generator\Model\Database;

use \DomDocument;
use \Exception;

/**
* An abstract base Propel manager to perform work related to the XML schema file.
Expand Down Expand Up @@ -69,7 +70,12 @@ abstract class AbstractManager
*
* @var array
*/
protected $includedFiles = array();
protected $schemas = array();

/**
* @var string
*/
protected $workingDirectory;

/**
* @var \closure
Expand All @@ -92,19 +98,36 @@ abstract class AbstractManager
/**
* @return array
*/
public function getIncludedFiles()
public function getSchemas()
{
return $this->includedFiles;
return $this->schemas;
}

/**
* @param array
*/
public function setIncludedFiles($includedFiles)
public function setSchemas($schemas)
{
$this->includedFiles = $includedFiles;
$this->schemas = $schemas;
}

/**
* @param string $workingDirectory
*/
public function setWorkingDirectory($workingDirectory)
{
$this->workingDirectory = $workingDirectory;
}

/**
* return string
*/
public function getWorkingDirectory()
{
return $this->workingDirectory;
}


/**
* Return the data models that have been
* processed.
Expand Down Expand Up @@ -190,7 +213,7 @@ protected function loadDataModels()
{
$ads = array();
$totalNbTables = 0;
$dataModelFiles = $this->getIncludedFiles();
$dataModelFiles = $this->getSchemas();
$defaultPlatform = $this->getGeneratorConfig()->getConfiguredPlatform();

// Make a transaction for each file
Expand Down Expand Up @@ -327,13 +350,21 @@ protected function joinDataModels($ads)
/**
* Gets the GeneratorConfig object for this manager or creates it on-demand.
*
* @return \Propel\Generator\Config\GeneratorConfig
* @return \Propel\Generator\Config\GeneratorConfigInterface
*/
protected function getGeneratorConfig()
{
return $this->generatorConfig;
}

/**
* @param GeneratorConfigInterface $generatorConfig
*/
public function setGeneratorConfig(GeneratorConfigInterface $generatorConfig)
{
$this->generatorConfig = $generatorConfig;
}

protected function validate()
{
if ($this->validate) {
Expand All @@ -352,4 +383,32 @@ protected function log($message)
var_dump($message);
}
}

/**
* Returns an array of properties as key/value pairs from an input file.
*
* @param string $file A file properties.
* @return array An array of properties as key/value pairs.
*/
protected function getProperties($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;
}
}
Loading

0 comments on commit 16d77be

Please sign in to comment.