Merge pull request #2845 from aRn0D/arche_type

[ArchetypeBundle] Form registration, missing specs and CS
This commit is contained in:
Paweł Jędrzejewski 2015-06-14 13:42:49 +02:00
commit aec2be1b57
8 changed files with 75 additions and 150 deletions

View file

@ -1,8 +1,9 @@
suites:
Addressing : { namespace: Sylius\Component\Addressing, psr4_prefix: Sylius\Component\Addressing, spec_path: src/Sylius/Component/Addressing, src_path: src/Sylius/Component/Addressing }
Attribute : { namespace: Sylius\Component\Attribute, psr4_prefix: Sylius\Component\Attribute, spec_path: src/Sylius/Component/Attribute, src_path: src/Sylius/Component/Attribute }
Archetype : { namespace: Sylius\Component\Archetype, psr4_prefix: Sylius\Component\Archetype, spec_path: src/Sylius/Component/Archetype, src_path: src/Sylius/Component/Archetype }
Cart : { namespace: Sylius\Component\Cart, psr4_prefix: Sylius\Component\Cart, spec_path: src/Sylius/Component/Cart, src_path: src/Sylius/Component/Cart }
Channel : { namespace: Sylius\Component\Channel, psr4_prefix: Sylius\Component\Channel, spec_path: src/Sylius/Component/Channel, src_path: src/Sylius/Component/Channel }
Channel : { namespace: Sylius\Component\Channel, psr4_prefix: Sylius\Component\Channel, spec_path: src/Sylius/Component/Channel, src_path: src/Sylius/Component/Channel }
Core : { namespace: Sylius\Component\Core, psr4_prefix: Sylius\Component\Core, spec_path: src/Sylius/Component/Core, src_path: src/Sylius/Component/Core }
Contact : { namespace: Sylius\Component\Contact, psr4_prefix: Sylius\Component\Contact, spec_path: src/Sylius/Component/Contact, src_path: src/Sylius/Component/Contact }
Currency : { namespace: Sylius\Component\Currency, psr4_prefix: Sylius\Component\Currency, spec_path: src/Sylius/Component/Currency, src_path: src/Sylius/Component/Currency }
@ -33,6 +34,7 @@ suites:
AddressingBundle : { namespace: Sylius\Bundle\AddressingBundle, psr4_prefix: Sylius\Bundle\AddressingBundle, spec_path: src/Sylius/Bundle/AddressingBundle, src_path: src/Sylius/Bundle/AddressingBundle }
AttributeBundle : { namespace: Sylius\Bundle\AttributeBundle, psr4_prefix: Sylius\Bundle\AttributeBundle, spec_path: src/Sylius/Bundle/AttributeBundle, src_path: src/Sylius/Bundle/AttributeBundle }
ArchetypeBundle : { namespace: Sylius\Bundle\ArchetypeBundle, psr4_prefix: Sylius\Bundle\ArchetypeBundle, spec_path: src/Sylius/Bundle/ArchetypeBundle, src_path: src/Sylius/Bundle/ArchetypeBundle }
CartBundle : { namespace: Sylius\Bundle\CartBundle, psr4_prefix: Sylius\Bundle\CartBundle, spec_path: src/Sylius/Bundle/CartBundle, src_path: src/Sylius/Bundle/CartBundle }
ChannelBundle : { namespace: Sylius\Bundle\ChannelBundle, psr4_prefix: Sylius\Bundle\ChannelBundle, spec_path: src/Sylius/Bundle/ChannelBundle, src_path: src/Sylius/Bundle/ChannelBundle }
CoreBundle : { namespace: Sylius\Bundle\CoreBundle, psr4_prefix: Sylius\Bundle\CoreBundle, spec_path: src/Sylius/Bundle/CoreBundle, src_path: src/Sylius/Bundle/CoreBundle }

View file

@ -93,7 +93,13 @@ class Configuration implements ConfigurationInterface
->scalarNode('model')->defaultValue('Sylius\Component\Archetype\Model\Archetype')->end()
->scalarNode('controller')->defaultValue('Sylius\Bundle\ResourceBundle\Controller\ResourceController')->end()
->scalarNode('repository')->defaultValue('Sylius\Bundle\TranslationBundle\Doctrine\ORM\TranslatableResourceRepository')->end()
->scalarNode('form')->defaultValue('Sylius\Bundle\ArchetypeBundle\Form\Type\ArchetypeType')->end()
->arrayNode('form')
->addDefaultsIfNotSet()
->children()
->scalarNode('default')->defaultValue('Sylius\Bundle\ArchetypeBundle\Form\Type\ArchetypeType')->end()
->scalarNode('choice')->defaultValue('Sylius\Bundle\ResourceBundle\Form\Type\ResourceChoiceType')->end()
->end()
->end()
->arrayNode('translation')
->addDefaultsIfNotSet()
->children()

View file

@ -29,12 +29,22 @@ class SyliusArchetypeExtension extends AbstractResourceExtension
*/
public function load(array $config, ContainerBuilder $container)
{
$this->configure(
$config = $this->configure(
$config,
new Configuration(),
$container,
self::CONFIGURE_LOADER | self::CONFIGURE_DATABASE | self::CONFIGURE_PARAMETERS | self::CONFIGURE_VALIDATORS | self::CONFIGURE_TRANSLATIONS
self::CONFIGURE_LOADER | self::CONFIGURE_DATABASE | self::CONFIGURE_PARAMETERS | self::CONFIGURE_VALIDATORS | self::CONFIGURE_TRANSLATIONS | self::CONFIGURE_FORMS
);
foreach ($config['classes'] as $name => $parameters) {
$formDefinition = $container->getDefinition('sylius.form.type.'.$name);
$formDefinition->addArgument($parameters['subject']);
if (isset($parameters['translation'])) {
$formTranslationDefinition = $container->getDefinition('sylius.form.type.'.$name.'_translation');
$formTranslationDefinition->addArgument($parameters['subject']);
}
}
}
/**
@ -51,13 +61,17 @@ class SyliusArchetypeExtension extends AbstractResourceExtension
foreach ($parameters as $resource => $classes) {
$convertedConfig[$subject.'_'.$resource] = $classes;
$convertedConfig[$subject.'_'.$resource]['subject'] = $subject;
}
$this->createSubjectServices($container, $config['driver'], $subject, $convertedConfig);
$this->createSubjectServices($container, $subject);
if (!isset($config['validation_groups'][$subject]['archetype'])) {
$config['validation_groups'][$subject]['archetype'] = array('sylius');
}
if (!isset($config['validation_groups'][$subject]['archetype_translation'])) {
$config['validation_groups'][$subject]['archetype_translation'] = array('sylius');
}
}
$container->setParameter('sylius.archetype.subjects', $subjects);
@ -72,59 +86,16 @@ class SyliusArchetypeExtension extends AbstractResourceExtension
* Create services for every subject.
*
* @param ContainerBuilder $container
* @param string $driver
* @param string $subject
* @param array $config
*/
private function createSubjectServices(ContainerBuilder $container, $driver, $subject, array $config)
private function createSubjectServices(ContainerBuilder $container, $subject)
{
// Archetype form
$archetypeAlias = $subject.'_archetype';
$archetypeClasses = $config[$archetypeAlias];
$archeTypeFormValidationGroups = '%sylius.validation_group.'.$archetypeAlias.'%';
$archetypeFormType = new Definition($archetypeClasses['form']);
$archetypeFormType
->setArguments(array($archetypeClasses['model'], $archeTypeFormValidationGroups, $subject))
->addTag('form.type', array('alias' => 'sylius_'.$archetypeAlias))
;
$container->setDefinition('sylius.form.type.'.$archetypeAlias, $archetypeFormType);
// Archetype translation form
$archetypeTranslationAlias = $subject.'_archetype_translation';
$archetypeTranslationClasses = $config[$archetypeAlias]['translation'];
$archetypeTranslationFormType = new Definition($archetypeTranslationClasses['form']['default']);
$archetypeTranslationFormType
->setArguments(array($archetypeTranslationClasses['model'], $archeTypeFormValidationGroups, $subject))
->addTag('form.type', array('alias' => 'sylius_'.$archetypeTranslationAlias))
;
$container->setDefinition('sylius.form.type.'.$archetypeTranslationAlias, $archetypeTranslationFormType);
// Archetype choice form
$choiceTypeClasses = array(
SyliusResourceBundle::DRIVER_DOCTRINE_ORM => 'Sylius\Bundle\ArchetypeBundle\Form\Type\ArchetypeEntityChoiceType'
);
$archetypeChoiceFormType = new Definition($choiceTypeClasses[$driver]);
$archetypeChoiceFormType
->setArguments(array($subject, $archetypeClasses['model']))
->addTag('form.type', array('alias' => 'sylius_'.$archetypeAlias.'_choice'))
;
$container->setDefinition('sylius.form.type.'.$archetypeAlias.'_choice', $archetypeChoiceFormType);
$builder = new Definition('Sylius\Component\Archetype\Builder\ArchetypeBuilder');
$builder
$builderDefintion = new Definition('Sylius\Component\Archetype\Builder\ArchetypeBuilder');
$builderDefintion
->setArguments(array(new Reference(sprintf('sylius.repository.%s_attribute_value', $subject))))
;
$container->setDefinition('sylius.builder.'.$archetypeAlias, $builder);
$container->setDefinition('sylius.builder.'.$subject.'_archetype', $builderDefintion);
}
/**

View file

@ -1,69 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\ArchetypeBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* Archetype choice form type.
*
* @author Adam Elsodaney <adam.elso@gmail.com>
*/
abstract class ArchetypeChoiceType extends AbstractType
{
/**
* Name of the archetype subject.
*
* @var string
*/
protected $subjectName;
/**
* Archetype class name.
*
* @var string
*/
protected $className;
/**
* Constructor.
*
* @param string $subjectName
* @param string $className
*/
public function __construct($subjectName, $className)
{
$this->subjectName = $subjectName;
$this->className = $className;
}
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setDefaults(array(
'class' => $this->className
))
;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return sprintf('sylius_%s_archetype_choice', $this->subjectName);
}
}

View file

@ -1,28 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\ArchetypeBundle\Form\Type;
/**
* Archetype choice form type.
*
* @author Adam Elsodaney <adam.elso@gmail.com>
*/
class ArchetypeEntityChoiceType extends ArchetypeChoiceType
{
/**
* {@inheritdoc}
*/
public function getParent()
{
return 'entity';
}
}

View file

@ -0,0 +1,37 @@
<?php
namespace spec\Sylius\Bundle\ArchetypeBundle\Form\Type;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Component\Form\FormBuilderInterface;
class ArchetypeTranslationTypeSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith('ArchetypeTranslation', array('sylius'), 'subject');
}
function it_is_initializable()
{
$this->shouldHaveType('Sylius\Bundle\ArchetypeBundle\Form\Type\ArchetypeTranslationType');
}
function it_is_a_form()
{
$this->shouldHaveType('Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType');
}
function it_buils_a_form(FormBuilderInterface $builder)
{
$builder->add('name', 'text', Argument::type('array'))->shouldBeCalled();
$this->buildForm($builder, array());
}
function it_has_a_name()
{
$this->getName()->shouldReturn('sylius_subject_archetype_translation');
}
}

View file

@ -44,7 +44,7 @@ class ArchetypeTypeSpec extends ObjectBehavior
;
$builder
->add('name', 'text', Argument::any())
->add('translations', 'a2lix_translationsForms', Argument::any())
->willReturn($builder)
;

View file

@ -24,6 +24,12 @@ use Sylius\Component\Variation\Model\OptionInterface;
*/
class ArchetypeSpec extends ObjectBehavior
{
function let()
{
$this->setCurrentLocale('en_US');
$this->setFallbackLocale('en_US');
}
function it_is_initializable()
{
$this->shouldHaveType('Sylius\Component\Archetype\Model\Archetype');