[Command] Added a new command named: propel:form:generate

Allows to quickly create Form Type stubs
Added a PropelGeneratorAwareCommand class (abstract) to write more code generation based commands
Added few tests, fixed some tests as well
This commit is contained in:
William DURAND 2012-04-11 11:54:25 +02:00
parent 6a22291eba
commit 0b85aba836
11 changed files with 329 additions and 58 deletions

View file

@ -235,15 +235,25 @@ abstract class AbstractPropelCommand extends ContainerAwareCommand
{
$finalSchemas = array();
foreach ($kernel->getBundles() as $bundle) {
if (is_dir($dir = $bundle->getPath().'/Resources/config')) {
$finder = new Finder();
$schemas = $finder->files()->name('*schema.xml')->followLinks()->in($dir);
$finalSchemas = array_merge($finalSchemas, $this->getSchemasFromBundle($bundle));
}
if (!iterator_count($schemas)) {
continue;
}
return $finalSchemas;
}
/**
* @param \Symfony\Component\HttpKernel\Bundle\BundleInterface
*/
protected function getSchemasFromBundle(BundleInterface $bundle)
{
$finalSchemas = array();
if (is_dir($dir = $bundle->getPath().'/Resources/config')) {
$finder = new Finder();
$schemas = $finder->files()->name('*schema.xml')->followLinks()->in($dir);
if (iterator_count($schemas)) {
foreach ($schemas as $schema) {
$logicalName = $this->transformToLogicalName($schema, $bundle);
$finalSchema = new \SplFileInfo($this->getFileLocator()->locate($logicalName));
@ -255,6 +265,11 @@ abstract class AbstractPropelCommand extends ContainerAwareCommand
return $finalSchemas;
}
protected function getRelativeFileName(\SplFileInfo $file)
{
return substr(str_replace(realpath($this->getContainer()->getParameter('kernel.root_dir') . '/../'), '', $file), 1);
}
/**
* Create a 'build.properties' file.
*
@ -424,7 +439,7 @@ EOT;
* Write Propel output as summary based on a Regexp.
*
* @param OutputInterface $output The output object.
* @param string $taskname A task name
* @param string $taskname A task name
*/
protected function writeSummary(OutputInterface $output, $taskname)
{
@ -447,8 +462,8 @@ EOT;
* @see https://github.com/sensio/SensioGeneratorBundle/blob/master/Command/Helper/DialogHelper.php#L52
*
* @param OutputInterface $output The output.
* @param string $text A text message.
* @param string $style A style to apply on the section.
* @param string $text A text message.
* @param string $style A style to apply on the section.
*/
protected function writeSection(OutputInterface $output, $text, $style = 'bg=blue;fg=white')
{
@ -463,8 +478,8 @@ EOT;
* Renders an error message if a task has failed.
*
* @param OutputInterface $output The output.
* @param string $taskName A task name.
* @param Boolean $more Whether to add a 'more details' message or not.
* @param string $taskName A task name.
* @param Boolean $more Whether to add a 'more details' message or not.
*/
protected function writeTaskError($output, $taskName, $more = true)
{
@ -479,25 +494,42 @@ EOT;
/**
* @param OutputInterface $output The output.
* @param string $filename The filename.
* @param string $filename The filename.
*/
protected function writeNewFile($output, $filename)
{
return $output->writeln('>> <info>File+</info> ' . $filename);
}
/**
* @param OutputInterface $output The output.
* @param string $directory The directory.
*/
protected function writeNewDirectory($output, $directory)
{
return $output->writeln('>> <info>Dir+</info> ' . $directory);
}
/**
* Ask confirmation from the user.
*
* @param OutputInterface $output The output.
* @param string $question A given question.
* @param string $default A default response.
* @param string $question A given question.
* @param string $default A default response.
*/
protected function askConfirmation(OutputInterface $output, $question, $default = null)
{
return $this->getHelperSet()->get('dialog')->askConfirmation($output, $question, $default);
}
/**
* @return \Symfony\Component\Config\FileLocatorInterface
*/
protected function getFileLocator()
{
return $this->getContainer()->get('file_locator');
}
private function transformToLogicalName(\SplFileInfo $schema, BundleInterface $bundle)
{
$schemaPath = str_replace($bundle->getPath(). DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR, '', $schema->getRealPath());
@ -505,11 +537,6 @@ EOT;
return sprintf('@%s/Resources/config/%s', $bundle->getName(), $schemaPath);
}
protected function getFileLocator()
{
return $this->getContainer()->get('file_locator');
}
/**
* Compiles arguments/properties for the Phing process.
* @return array

View file

@ -34,21 +34,25 @@ class FixturesLoadCommand extends AbstractPropelCommand
* @var string
*/
private $defaultFixturesDir = 'app/propel/fixtures';
/**
* Absolute path for fixtures directory
* @var string
*/
private $absoluteFixturesPath = '';
/**
* Filesystem for manipulating files
* @var \Symfony\Component\Filesystem\Filesystem
*/
private $filesystem = null;
/**
* Bundle the fixtures are being loaded from
* @var Symfony\Component\HttpKernel\Bundle\BundleInterface
*/
private $bundle;
/**
* @see Command
*/
@ -108,9 +112,9 @@ YAML fixtures are:
Description: Hello world !
</comment>
EOT
)
)
->setName('propel:fixtures:load')
;
;
}
/**
@ -123,19 +127,20 @@ EOT
$this->writeSection($output, '[Propel] You are running the command: propel:fixtures:load');
$this->filesystem = new Filesystem();
if ('@' === substr($input->getArgument('bundle'), 0, 1)) {
$this->bundle = $this
->getContainer()
->get('kernel')
->getBundle(substr($input->getArgument('bundle'), 1));
$this->absoluteFixturesPath = $this->getFixturesPath($this->bundle);
} else {
$this->absoluteFixturesPath = realpath($this->getApplication()->getKernel()->getRootDir() . '/../' . $input->getOption('dir'));
}
if ($input->getOption('verbose')) {
$this->additionalPhingArgs[] = 'verbose';
$this->additionalPhingArgs[] = 'verbose';
}
if (!$this->absoluteFixturesPath && !file_exists($this->absoluteFixturesPath)) {
@ -318,7 +323,7 @@ EOT
if (null === $this->bundle) {
return $files;
}
$finalFixtureFiles = array();
foreach ($files as $file) {
@ -338,5 +343,5 @@ EOT
protected function getFixturesPath(BundleInterface $bundle)
{
return $bundle->getPath().DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'fixtures';
}
}
}

View file

@ -1,5 +1,6 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
@ -13,14 +14,19 @@ namespace Propel\PropelBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
/**
* @author William DURAND <william.durand1@gmail.com>
*/
class FormGenerateCommand extends ContainerAwareCommand
class FormGenerateCommand extends PropelGeneratorAwareCommand
{
const DEFAULT_FORM_TYPE_DIRECTORY = '/Form/Type';
/**
* @see Command
*/
@ -29,7 +35,15 @@ class FormGenerateCommand extends ContainerAwareCommand
$this
->setDescription('Generate Form types stubs based on the schema.xml')
->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to use to generate Form types')
->setHelp('')
->addOption('force', 'f', InputOption::VALUE_NONE, 'Overwrite existing Form types')
->setHelp(<<<EOT
The <info>%command.name%</info> command allows you to quickly generate Form Type stubs for a given bundle.
<info>php app/console %command.full_name%</info>
The <info>--force</info> parameter allows you to overwrite existing files.
EOT
)
->setName('propel:form:generate');
}
@ -40,38 +54,77 @@ class FormGenerateCommand extends ContainerAwareCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$propelPath = $this->getContainer()->getParameter('propel.path');
require_once sprintf('%s/generator/lib/builder/util/XmlToAppData.php', $propelPath);
require_once sprintf('%s/generator/lib/config/GeneratorConfig.php', $propelPath);
require_once sprintf('%s/generator/lib/config/QuickGeneratorConfig.php', $propelPath);
set_include_path(sprintf('%s/generator/lib', $propelPath) . PATH_SEPARATOR . get_include_path());
if ('@' === substr($input->getArgument('bundle'), 0, 1)) {
$bundle = $this
->getContainer()
->get('kernel')
->getBundle(substr($input->getArgument('bundle'), 1));
if (is_dir($dir = $bundle->getPath().'/Resources/config')) {
$finder = new Finder();
$schemas = $finder->files()->name('*schema.xml')->followLinks()->in($dir);
$schemas = $this->getSchemasFromBundle($bundle);
$array = array();
foreach ($schemas as $schema) {
$array[] = $schema->getPathName();
if ($schemas) {
foreach ($schemas as $fileName => $array) {
foreach ($this->getDatabasesFromSchema($array[1]) as $database) {
$this->createFormTypeFromDatabase($bundle, $database, $output, $input->getOption('force'));
}
}
} else {
$output->writeln(sprintf('No <comment>*schemas.xml</comment> files found in bundle <comment>%s</comment>.', $bundle->getName()));
}
$transformer = new \XmlToAppData(null, null, 'UTF-8');
$transformer->setGeneratorConfig(new \QuickGeneratorConfig());
$appDatas = array();
foreach ($array as $xmlFile) {
$appDatas[] = $transformer->parseFile($xmlFile);
}
var_dump($appDatas);
}
}
private function createFormTypeFromDatabase(BundleInterface $bundle, \Database $database, OutputInterface $output, $force = false)
{
$dir = $this->createDirectory($bundle, $output);
foreach ($database->getTables() as $table) {
$file = new \SplFileInfo(sprintf('%s/%sType.php', $dir, $table->getPhpName()));
if (!file_exists($file) || true === $force) {
$this->writeFormType($bundle, $table, $file, $force, $output);
} else {
$output->writeln(sprintf('File <comment>%-60s</comment> exists, skipped. Try the <info>--force</info> option.', $this->getRelativeFileName($file)));
}
}
}
private function createDirectory(BundleInterface $bundle, OutputInterface $output)
{
$fs = new Filesystem();
if (!is_dir($dir = $bundle->getPath() . self::DEFAULT_FORM_TYPE_DIRECTORY)) {
$fs->mkdir($dir);
$this->writeNewDirectory($output, $dir);
}
return $dir;
}
private function writeFormType(BundleInterface $bundle, \Table $table, \SplFileInfo $file, $force, OutputInterface $output)
{
$modelName = $table->getPhpName();
$formTypeContent = file_get_contents(__DIR__ . '/../Resources/skeleton/FormType.php');
$formTypeContent = str_replace('##NAMESPACE##', $bundle->getNamespace() . str_replace('/', '\\', self::DEFAULT_FORM_TYPE_DIRECTORY), $formTypeContent);
$formTypeContent = str_replace('##CLASS##', $modelName . 'Type', $formTypeContent);
$formTypeContent = str_replace('##FQCN##', sprintf('%s\%s', $table->getNamespace(), $modelName), $formTypeContent);
$formTypeContent = str_replace('##TYPE_NAME##', strtolower($modelName), $formTypeContent);
$formTypeContent = $this->addFields($table, $formTypeContent);
file_put_contents($file->getPathName(), $formTypeContent);
$this->writeNewFile($output, $this->getRelativeFileName($file) . ($force ? ' (forced)' : ''));
}
private function addFields(\Table $table, $formTypeContent)
{
$buildCode = '';
foreach ($table->getColumns() as $column) {
if (!$column->isPrimaryKey()) {
$buildCode .= sprintf("\n \$builder->add('%s');", lcfirst($column->getPhpName()));
}
}
return str_replace('##BUILD_CODE##', $buildCode, $formTypeContent);
}
}

View file

@ -0,0 +1,49 @@
<?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;
/**
* @author William Durand <william.durand1@gmail.com>
*/
abstract class PropelGeneratorAwareCommand extends AbstractPropelCommand
{
/**
* {@inheritdoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
parent::initialize($input, $output);
$this->loadPropelGenerator();
}
protected function loadPropelGenerator()
{
$propelPath = $this->getContainer()->getParameter('propel.path');
require_once sprintf('%s/generator/lib/builder/util/XmlToAppData.php', $propelPath);
require_once sprintf('%s/generator/lib/config/GeneratorConfig.php', $propelPath);
require_once sprintf('%s/generator/lib/config/QuickGeneratorConfig.php', $propelPath);
set_include_path(sprintf('%s/generator/lib', $propelPath) . PATH_SEPARATOR . get_include_path());
}
protected function getDatabasesFromSchema(\SplFileInfo $file)
{
$transformer = new \XmlToAppData(null, null, 'UTF-8');
$transformer->setGeneratorConfig(new \QuickGeneratorConfig());
return $transformer->parseFile($file->getPathName())->getDatabases();
}
}

View file

@ -315,6 +315,15 @@ The table arguments define which table will be delete, by default all table.
Note that the `--force` option is needed to actually execute the deletion.
### Form Types ###
You can generate stub classes based on your `schema.xml` in a given bundle:
> php app/console propel:form:generate [-f|--force] bundle
It will write Form Type classes in `src/YourVendor/YourBundle/Form/Type`.
## PropelParamConverter ##
You can use the Propel ParamConverter with the SensioFrameworkExtraBundle.

View file

@ -0,0 +1,34 @@
<?php
namespace ##NAMESPACE##;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class ##CLASS## extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilder $builder, array $options)
{##BUILD_CODE##
}
/**
* {@inheritdoc}
*/
public function getDefaultOptions(array $options)
{
return array(
'data_class' => '##FQCN##',
);
}
/**
* {@inheritdoc}
*/
public function getName()
{
return '##TYPE_NAME##';
}
}

View file

@ -8,7 +8,7 @@
* @license MIT License
*/
namespace Tests\Command;
namespace Propel\PropelBundle\Tests\Command;
use Propel\PropelBundle\Tests\TestCase;
use Propel\PropelBundle\Command\AbstractPropelCommand;

View file

@ -8,7 +8,7 @@
* @license MIT License
*/
namespace Tests\Command;
namespace Propel\PropelBundle\Tests\Command;
use Symfony\Component\Filesystem\Filesystem;

View file

@ -0,0 +1,73 @@
<?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\Tests\Command;
use Propel\PropelBundle\Command\PropelGeneratorAwareCommand;
use Propel\PropelBundle\Tests\TestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @author William Durand <william.durand1@gmail.com>
*/
class PropelGeneratorAwareCommandTest extends TestCase
{
protected $container;
protected function setUp()
{
parent::setUp();
$this->container = $this->getContainer();
$this->container->setParameter('propel.path', __DIR__ . '/../../vendor/propel');
}
public function testGetDatabasesFromSchema()
{
$command = new PropelGeneratorAwareCommandTestable('testable-command');
$command->setContainer($this->container);
$databases = $command->getDatabasesFromSchema(new \SplFileInfo(__DIR__ . '/../Fixtures/schema.xml'));
$this->assertTrue(is_array($databases));
foreach ($databases as $database) {
$this->assertInstanceOf('\Database', $database);
}
$bookstore = $databases[0];
$this->assertEquals(1, count($bookstore->getTables()));
foreach ($bookstore->getTables() as $table) {
$this->assertInstanceOf('\Table', $table);
}
}
}
class PropelGeneratorAwareCommandTestable extends PropelGeneratorAwareCommand
{
protected $container;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
protected function getContainer()
{
return $this->container;
}
public function getDatabasesFromSchema(\SplFileInfo $file)
{
$this->loadPropelGenerator();
return parent::getDatabasesFromSchema($file);
}
}

View file

@ -44,9 +44,9 @@ class PropelExtensionTest extends TestCase
$container = $this->getContainer();
$loader = new PropelExtension();
$loader->load(array(array(
'path' => '/propel',
'path' => '/propel',
'phing_path' => '/phing',
'dbal' => array()
'dbal' => array()
)), $container);
$this->assertEquals('/propel', $container->getParameter('propel.path'), '->load() requires the Propel path');
$this->assertEquals('/phing', $container->getParameter('propel.phing_path'), '->load() requires the Phing path');
@ -270,7 +270,7 @@ class PropelExtensionTest extends TestCase
$this->assertArrayHasKey('query', $config['datasources']['default']['connection']['settings']['queries']);
$this->assertEquals('SET NAMES UTF8', $config['datasources']['default']['connection']['settings']['queries']['query']);
}
public function testDbalWithSlaves()
{
$container = $this->getContainer();
@ -299,13 +299,13 @@ class PropelExtensionTest extends TestCase
),
),
);
$configs = array($config_base, array('dbal' => $config_mysql));
$loader->load($configs, $container);
$arguments = $container->getDefinition('propel.configuration')->getArguments();
$config = $arguments[0];
$this->assertArrayHasKey('slaves', $config['datasources']['default']);
$this->assertArrayHasKey('connection', $config['datasources']['default']['slaves']);
$this->assertArrayHasKey('mysql_slave1', $config['datasources']['default']['slaves']['connection']);

21
Tests/Fixtures/schema.xml Normal file
View file

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<database name="bookstore" defaultIdMethod="native">
<table name="book" description="Book Table">
<column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" description="Book Id" />
<column name="title" type="VARCHAR" required="true" description="Book Title" primaryString="true" />
<column name="isbn" required="true" type="VARCHAR" size="24" phpName="ISBN" description="ISBN Number" primaryString="false" />
<column name="price" required="false" type="FLOAT" description="Price of the book." />
<column name="publisher_id" required="false" type="INTEGER" description="Foreign Key Publisher" />
<column name="author_id" required="false" type="INTEGER" description="Foreign Key Author" />
<validator column="title" translate="none">
<rule name="unique" message="Book title already in database." />
<rule name="minLength" value="10" message="Book title must be more than ${value} characters long." />
<rule name="maxLength" value="255" message="Book title must not be longer than ${value} characters." />
</validator>
<validator column="isbn" translate="none">
<rule name="class" class="bookstore.validator.ISBNValidator" message="ISBN does not validate!"/>
</validator>
</table>
</database>