Initial commit for new sf3 form creation
This commit is contained in:
parent
6119fc9806
commit
b64f7446e7
65
Command/BundleTrait.php
Normal file
65
Command/BundleTrait.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?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\Question\Question;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
|
||||
|
||||
/**
|
||||
* @author Moritz Schroeder <moritz.schroeder@molabs.de>
|
||||
*/
|
||||
trait BundleTrait
|
||||
{
|
||||
/**
|
||||
* @return ContainerInterface
|
||||
*/
|
||||
protected abstract function getContainer();
|
||||
|
||||
/**
|
||||
* Returns the selected bundle.
|
||||
* If no bundle argument is set, the user will get ask for it.
|
||||
*
|
||||
* @param InputInterface $input
|
||||
* @param OutputInterface $output
|
||||
*
|
||||
* @return BundleInterface
|
||||
*/
|
||||
protected function getBundle(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$kernel = $this
|
||||
->getContainer()
|
||||
->get('kernel');
|
||||
|
||||
if ($input->hasArgument('bundle') && '@' === substr($input->getArgument('bundle'), 0, 1)) {
|
||||
return $kernel->getBundle(substr($input->getArgument('bundle'), 1));
|
||||
}
|
||||
|
||||
$bundleNames = array_keys($kernel->getBundles());
|
||||
|
||||
do {
|
||||
$question = '<info>Select the bundle</info>: ';
|
||||
$question = new Question($question);
|
||||
$question->setAutocompleterValues($bundleNames);
|
||||
|
||||
$bundleName = $this->getHelperSet()->get('question')->ask($input, $output, $question);
|
||||
|
||||
if (in_array($bundleName, $bundleNames)) {
|
||||
break;
|
||||
}
|
||||
$output->writeln(sprintf('<bg=red>Bundle "%s" does not exist.</bg>', $bundleName));
|
||||
} while (true);
|
||||
|
||||
return $kernel->getBundle($bundleName);
|
||||
}
|
||||
}
|
|
@ -10,12 +10,12 @@
|
|||
|
||||
namespace Propel\Bundle\PropelBundle\Command;
|
||||
|
||||
use Propel\Bundle\PropelBundle\Form\FormBuilder;
|
||||
use Propel\Generator\Config\GeneratorConfig;
|
||||
use Propel\Generator\Command\ModelBuildCommand as BaseModelBuildCommand;
|
||||
use Propel\Generator\Model\Database;
|
||||
use Propel\Generator\Model\Table;
|
||||
use Propel\Generator\Manager\ModelManager;
|
||||
use Propel\Runtime\Propel;
|
||||
use Propel\PropelBundle\Command\BundleTrait;
|
||||
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
|
@ -31,6 +31,8 @@ class FormGenerateCommand extends AbstractCommand
|
|||
{
|
||||
const DEFAULT_FORM_TYPE_DIRECTORY = '/Form/Type';
|
||||
|
||||
use BundleTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -42,7 +44,7 @@ class FormGenerateCommand extends AbstractCommand
|
|||
|
||||
->addOption('force', 'f', InputOption::VALUE_NONE, 'Overwrite existing Form types')
|
||||
->addOption('platform', null, InputOption::VALUE_REQUIRED, 'The platform')
|
||||
->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to use to generate Form types (Ex: @AcmeDemoBundle)')
|
||||
->addArgument('bundle', InputArgument::OPTIONAL, 'The bundle to use to generate Form types (Ex: @AcmeDemoBundle)')
|
||||
->addArgument('models', InputArgument::IS_ARRAY, 'Model classes to generate Form Types from')
|
||||
|
||||
->setHelp(<<<EOT
|
||||
|
@ -64,15 +66,12 @@ EOT
|
|||
$models = $input->getArgument('models');
|
||||
$force = $input->getOption('force');
|
||||
|
||||
if (!$this->bundle) {
|
||||
throw new \InvalidArgumentException('No valid bundle given');
|
||||
}
|
||||
$bundle = $this->getBundle($input, $output);
|
||||
|
||||
$this->setupBuildTimeFiles();
|
||||
|
||||
if (!($schemas = $this->getFinalSchemas($kernel, $this->bundle))) {
|
||||
$output->writeln(sprintf('No <comment>*schemas.xml</comment> files found in bundle <comment>%s</comment>.', $this->bundle->getName()));
|
||||
|
||||
$schemas = $this->getFinalSchemas($kernel, $bundle);
|
||||
if (!$schemas) {
|
||||
$output->writeln(sprintf('No <comment>*schemas.xml</comment> files found in bundle <comment>%s</comment>.', $bundle->getName()));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -138,45 +137,19 @@ EOT
|
|||
*
|
||||
* @param BundleInterface $bundle The bundle in which the FormType will be created.
|
||||
* @param Table $table The table for which the FormType will be created.
|
||||
* @param SplFileInfo $file File representing the FormType.
|
||||
* @param \SplFileInfo $file File representing the FormType.
|
||||
* @param boolean $force Is the write forced?
|
||||
* @param OutputInterface $output An OutputInterface instance.
|
||||
*/
|
||||
protected 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);
|
||||
$formBuilder = new FormBuilder();
|
||||
$formTypeContent = $formBuilder->buildFormType($bundle, $table, self::DEFAULT_FORM_TYPE_DIRECTORY);
|
||||
|
||||
file_put_contents($file->getPathName(), $formTypeContent);
|
||||
$this->writeNewFile($output, $this->getRelativeFileName($file) . ($force ? ' (forced)' : ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the fields in the FormType.
|
||||
*
|
||||
* @param Table $table Table from which the fields will be extracted.
|
||||
* @param string $formTypeContent FormType skeleton.
|
||||
*
|
||||
* @return string The FormType code.
|
||||
*/
|
||||
protected 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \SplFileInfo $file
|
||||
* @return string
|
||||
|
|
62
Form/FormBuilder.php
Normal file
62
Form/FormBuilder.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?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\Bundle\PropelBundle\Form;
|
||||
|
||||
use Propel\Generator\Model\Table;
|
||||
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
|
||||
|
||||
/**
|
||||
* @author Moritz Schroeder <moritz.schroeder@molabs.de>
|
||||
*/
|
||||
class FormBuilder
|
||||
{
|
||||
/**
|
||||
* Build a form based on the given table.
|
||||
*
|
||||
* @param BundleInterface $bundle
|
||||
* @param Table $table
|
||||
* @param string $formTypeNamespace
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function buildFormType(BundleInterface $bundle, Table $table, $formTypeNamespace)
|
||||
{
|
||||
$modelName = $table->getPhpName();
|
||||
$formTypeContent = file_get_contents(__DIR__ . '/../Resources/skeleton/FormType.php');
|
||||
|
||||
$formTypeContent = str_replace('##NAMESPACE##', $bundle->getNamespace() . str_replace('/', '\\', $formTypeNamespace), $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 = str_replace('##BUILD_CODE##', $this->buildFormFields($table), $formTypeContent);
|
||||
|
||||
return $formTypeContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the fields in the FormType.
|
||||
*
|
||||
* @param Table $table Table from which the fields will be extracted.
|
||||
*
|
||||
* @return string The FormType code.
|
||||
*/
|
||||
protected function buildFormFields(Table $table)
|
||||
{
|
||||
$buildCode = '';
|
||||
foreach ($table->getColumns() as $column) {
|
||||
if (!$column->isPrimaryKey()) {
|
||||
$buildCode .= sprintf("\n \$builder->add('%s');", lcfirst($column->getPhpName()));
|
||||
}
|
||||
}
|
||||
|
||||
return $buildCode;
|
||||
}
|
||||
}
|
|
@ -2,20 +2,27 @@
|
|||
|
||||
namespace ##NAMESPACE##;
|
||||
|
||||
use Propel\Bundle\PropelBundle\Form\BaseAbstractType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class ##CLASS## extends BaseAbstractType
|
||||
class ##CLASS## extends AbstractType
|
||||
{
|
||||
protected $options = array(
|
||||
'data_class' => '##FQCN##',
|
||||
'name' => '##TYPE_NAME##',
|
||||
);
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{##BUILD_CODE##
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => '##FQCN##',
|
||||
'name' => '##TYPE_NAME##',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue