From b64f7446e76fe8b722219b06aa11e6b5260b648e Mon Sep 17 00:00:00 2001 From: Moritz Schroeder Date: Wed, 17 Feb 2016 18:57:00 +0100 Subject: [PATCH] Initial commit for new sf3 form creation --- Command/BundleTrait.php | 65 +++++++++++++++++++++++++++++++++ Command/FormGenerateCommand.php | 51 ++++++-------------------- Form/FormBuilder.php | 62 +++++++++++++++++++++++++++++++ Resources/skeleton/FormType.php | 21 +++++++---- 4 files changed, 153 insertions(+), 46 deletions(-) create mode 100644 Command/BundleTrait.php create mode 100644 Form/FormBuilder.php diff --git a/Command/BundleTrait.php b/Command/BundleTrait.php new file mode 100644 index 0000000..c3043fb --- /dev/null +++ b/Command/BundleTrait.php @@ -0,0 +1,65 @@ + + */ +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 = 'Select the bundle: '; + $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('Bundle "%s" does not exist.', $bundleName)); + } while (true); + + return $kernel->getBundle($bundleName); + } +} \ No newline at end of file diff --git a/Command/FormGenerateCommand.php b/Command/FormGenerateCommand.php index 46f68c9..91c6bd7 100644 --- a/Command/FormGenerateCommand.php +++ b/Command/FormGenerateCommand.php @@ -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; @@ -30,6 +30,8 @@ use Symfony\Component\HttpKernel\Bundle\BundleInterface; 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(<<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 *schemas.xml files found in bundle %s.', $this->bundle->getName())); - + $schemas = $this->getFinalSchemas($kernel, $bundle); + if (!$schemas) { + $output->writeln(sprintf('No *schemas.xml files found in bundle %s.', $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 diff --git a/Form/FormBuilder.php b/Form/FormBuilder.php new file mode 100644 index 0000000..a6c43eb --- /dev/null +++ b/Form/FormBuilder.php @@ -0,0 +1,62 @@ + + */ +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; + } +} \ No newline at end of file diff --git a/Resources/skeleton/FormType.php b/Resources/skeleton/FormType.php index 566c43c..fd8c2f8 100644 --- a/Resources/skeleton/FormType.php +++ b/Resources/skeleton/FormType.php @@ -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##', + ]); + } }