Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Commit

Permalink
Fixed Form 2.7 compatibility in type and crud generation
Browse files Browse the repository at this point in the history
  • Loading branch information
HeahDude committed Oct 9, 2016
1 parent ae67a68 commit c701fde
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 35 deletions.
2 changes: 2 additions & 0 deletions Generator/DoctrineCrudGenerator.php
Expand Up @@ -183,6 +183,8 @@ protected function generateControllerClass($forceOverwrite)
'namespace' => $this->bundle->getNamespace(),
'entity_namespace' => $entityNamespace,
'format' => $this->format,
// BC with Symfony 2.7
'use_form_type_instance' => !method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix'),
));
}

Expand Down
3 changes: 2 additions & 1 deletion Generator/DoctrineFormGenerator.php
Expand Up @@ -77,13 +77,14 @@ public function generate(BundleInterface $bundle, $entity, ClassMetadataInfo $me

$this->renderFile('form/FormType.php.twig', $this->classPath, array(
'fields' => $this->getFieldsFromMetadata($metadata),
'fields_mapping' => $metadata->fieldMappings,
'namespace' => $bundle->getNamespace(),
'entity_namespace' => implode('\\', $parts),
'entity_class' => $entityClass,
'bundle' => $bundle->getName(),
'form_class' => $this->className,
'form_type_name' => strtolower(str_replace('\\', '_', $bundle->getNamespace()).($parts ? '_' : '').implode('_', $parts).'_'.substr($this->className, 0, -4)),
// BC with Symfony 2.7
'get_name_required' => !method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix'),
));
}

Expand Down
2 changes: 1 addition & 1 deletion Resources/skeleton/crud/actions/delete.php.twig
Expand Up @@ -22,7 +22,7 @@
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove(${{ entity_singularized }});
$em->flush();
$em->flush(${{ entity_singularized }});
}
{% endblock method_body %}

Expand Down
10 changes: 6 additions & 4 deletions Resources/skeleton/crud/actions/edit.php.twig
Expand Up @@ -17,13 +17,15 @@
{
{% block method_body %}
$deleteForm = $this->createDeleteForm(${{ entity_singularized }});
$editForm = $this->createForm('{{ namespace }}\Form\{{ entity }}Type', ${{ entity_singularized }});
{% if use_form_type_instance -%}
$editForm = $this->createForm(new {{ entity_singularized|capitalize }}Type(), ${{ entity_singularized }});
{% else -%}
$editForm = $this->createForm('{{ namespace }}\Form\{{ entity }}Type', ${{ entity_singularized }});
{% endif -%}
$editForm->handleRequest($request);

if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist(${{ entity_singularized }});
$em->flush();
$this->getDoctrine()->getManager()->flush();

return $this->redirectToRoute('{{ route_name_prefix }}_edit', array('id' => ${{ entity_singularized }}->getId()));
}
Expand Down
8 changes: 6 additions & 2 deletions Resources/skeleton/crud/actions/new.php.twig
@@ -1,7 +1,7 @@

/**
{% block phpdoc_method_header %}
* Creates a new {{ entity }} entity.
* Creates a new {{ entity_singularized }} entity.
{% endblock phpdoc_method_header %}
*
{% block phpdoc_method_annotations %}
Expand All @@ -16,7 +16,11 @@
{% endblock method_definition %}
{
{% block method_body %}
$form = $this->createForm('{{ namespace }}\Form\{{ entity }}Type');
{% if use_form_type_instance -%}
$form = $this->createForm(new {{ entity_singularized|capitalize }}Type());
{% else -%}
$form = $this->createForm('{{ namespace }}\Form\{{ entity }}Type');
{% endif -%}
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
Expand Down
15 changes: 7 additions & 8 deletions Resources/skeleton/crud/controller.php.twig
Expand Up @@ -3,20 +3,19 @@
namespace {{ namespace }}\Controller{{ entity_namespace ? '\\' ~ entity_namespace : '' }};
{% block use_statements %}
{% if 'new' in actions or 'edit' in actions or 'delete' in actions %}
use Symfony\Component\HttpFoundation\Request;
{%- endif %}
use {{ namespace }}\Entity\{{ entity }};
{% if ('new' in actions or 'edit' in actions) and use_form_type_instance %}
use {{ namespace }}\Form\{{ entity }}Type;
{% endif %}
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
{% if 'annotation' == format -%}
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
{%- endif %}
{% if 'new' in actions or 'edit' in actions or 'delete' in actions %}
use Symfony\Component\HttpFoundation\Request;
{%- endif %}
use {{ namespace }}\Entity\{{ entity }};
{% if 'new' in actions or 'edit' in actions %}
use {{ namespace }}\Form\{{ entity }}Type;
{% endif %}
{% endblock use_statements %}
/**
Expand Down
33 changes: 22 additions & 11 deletions Resources/skeleton/form/FormType.php.twig
Expand Up @@ -20,19 +20,11 @@ class {{ form_class }} extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
{%- for field in fields -%}
{%- for field in fields -%}
{%- if fields_mapping[field] is defined and fields_mapping[field]['type'] in ['date', 'time', 'datetime'] %}
->add('{{ field }}', '{{ fields_mapping[field]['type'] }}')
{%- else %}
->add('{{ field }}')
{%- endif -%}
{%- endfor %}
->add('{{ field }}')
{%- endfor %}
;
}
{% endif %}
Expand All @@ -47,5 +39,24 @@ class {{ form_class }} extends AbstractType
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return '{{ form_type_name }}';
}
{# BC with Symfony 2.7 #}
{%- if get_name_required %}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
{% endif %}
{% endblock class_body %}
}
10 changes: 8 additions & 2 deletions Tests/Generator/DoctrineCrudGeneratorTest.php
Expand Up @@ -202,9 +202,15 @@ public function testGenerateNamespacedEntity()
'public function showAction(Post $post)',
'\'post\' => $post,',
'\'posts\' => $posts,',
'$form = $this->createForm(\'Foo\BarBundle\Form\Blog\PostType\');',
'$editForm = $this->createForm(\'Foo\BarBundle\Form\Blog\PostType\', $post);',
);
if (method_exists('Symfony\Compoennt\Form\AbstractType', 'getBlockPrefix')) {
// Symfony >= 2.8
$strings[] ='$form = $this->createForm(\'Foo\BarBundle\Form\Blog\PostType\');';
$strings[] = '$editForm = $this->createForm(\'Foo\BarBundle\Form\Blog\PostType\', $post);';
} else {
$strings[] ='$form = $this->createForm(new PostType());';
$strings[] = '$editForm = $this->createForm(new PostType(), $post);';
}
foreach ($strings as $string) {
$this->assertContains($string, $content);
}
Expand Down
21 changes: 15 additions & 6 deletions Tests/Generator/DoctrineFormGeneratorTest.php
Expand Up @@ -25,9 +25,9 @@ public function testGenerate()
$this->assertContains('namespace Foo\BarBundle\Form', $content);
$this->assertContains('class PostType extends AbstractType', $content);
$this->assertContains('->add(\'title\')', $content);
$this->assertContains('->add(\'createdAt\', \'date\')', $content);
$this->assertContains('->add(\'publishedAt\', \'time\')', $content);
$this->assertContains('->add(\'updatedAt\', \'datetime\')', $content);
$this->assertContains('->add(\'createdAt\')', $content);
$this->assertContains('->add(\'publishedAt\')', $content);
$this->assertContains('->add(\'updatedAt\')', $content);
$this->assertContains('public function configureOptions(OptionsResolver $resolver)', $content);
$this->assertContains('\'data_class\' => \'Foo\BarBundle\Entity\Post\'', $content);
}
Expand All @@ -42,11 +42,20 @@ public function testGenerateSubNamespacedEntity()
$this->assertContains('namespace Foo\BarBundle\Form\Blog', $content);
$this->assertContains('class PostType extends AbstractType', $content);
$this->assertContains('->add(\'title\')', $content);
$this->assertContains('->add(\'createdAt\', \'date\')', $content);
$this->assertContains('->add(\'publishedAt\', \'time\')', $content);
$this->assertContains('->add(\'updatedAt\', \'datetime\')', $content);
$this->assertContains('->add(\'createdAt\')', $content);
$this->assertContains('->add(\'publishedAt\')', $content);
$this->assertContains('->add(\'updatedAt\')', $content);
$this->assertContains('public function configureOptions(OptionsResolver $resolver)', $content);
$this->assertContains('\'data_class\' => \'Foo\BarBundle\Entity\Blog\Post\'', $content);
$this->assertContains('public function getBlockPrefix()', $content);
$this->assertContains('return \'foo_barbundle_blog_post\';', $content);
if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
// Symfony >= 2.8
$this->assertNotContains('public function getName()', $content);
} else {
// BC Symfony 2.7
$this->assertContains('public function getName()', $content);
}
}

/**
Expand Down

0 comments on commit c701fde

Please sign in to comment.