This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Form/PreloadedExtension.php

101 lines
2.4 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form;
use Symfony\Component\Form\Exception\InvalidArgumentException;
/**
2016-09-14 08:33:27 +01:00
* A form extension with preloaded types, type extensions and type guessers.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class PreloadedExtension implements FormExtensionInterface
{
/**
2014-07-09 11:03:33 +01:00
* @var FormTypeInterface[]
*/
private $types = array();
/**
2014-07-09 11:03:33 +01:00
* @var array[FormTypeExtensionInterface[]]
*/
private $typeExtensions = array();
/**
* @var FormTypeGuesserInterface
*/
private $typeGuesser;
/**
* Creates a new preloaded extension.
*
feature #15079 [Form] Deprecated FormTypeInterface::getName() and passing of type instances (webmozart) This PR was merged into the 2.8 branch. Discussion ---------- [Form] Deprecated FormTypeInterface::getName() and passing of type instances | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #5321, #15008 | License | MIT | Doc PR | TODO #### Type Names This PR deprecates the definition of the `getName()` method of form types. See #15008 for a more detailed description. Before: ```php class MyType extends AbstractType { public function getName() { return 'mytype'; } // ... } ``` After: ```php class MyType extends AbstractType { // ... } ``` You should always reference other types by their fully-qualified class names. Thanks to PHP 5.5, that's easy: Before: ```php $form = $this->createFormBuilder() ->add('name', 'text') ->add('age', 'integer') ->getForm(); ``` After: ```php $form = $this->createFormBuilder() ->add('name', TextType::class) ->add('age', IntegerType::class) ->getForm(); ``` #### Type Instances Furthermore, passing of type instances is deprecated. Before: ```php $form = $this->createForm(new AuthorType()); ``` After: ```php $form = $this->createForm(AuthorType::class); ``` #### DIC Aliases When registering a type in the DIC, you should omit the "alias" attribute now. Before: ```xml <service id="my.type" class="Vendor\Type\MyType"> <tag name="form.type" alias="mytype" /> <argument type="service" id="some.service.id" /> </service> ``` After: ```xml <service id="my.type" class="Vendor\Type\MyType"> <tag name="form.type" /> <argument type="service" id="some.service.id" /> </service> ``` Types without dependencies don't need to be registered in the DIC as they can be instantiated right away. #### Template Block Prefixes By default, the class name of the type in underscore notation minus "Type" suffix is used as Twig template block prefix (e.g. `UserProfileType` => `user_profile_*`). If you want to customize that, overwrite the new `getBlockPrefix()` method in your type: ```php class UserProfileType extends AbstractType { public function getBlockPrefix() { return 'profile'; } // ... } ``` Commits ------- 3d9e5de [Form] Deprecated FormTypeInterface::getName() and passing of type instances
2015-08-01 07:44:19 +01:00
* @param FormTypeInterface[] $types The types that the extension should support
* @param FormTypeExtensionInterface[][] $typeExtensions The type extensions that the extension should support
* @param FormTypeGuesserInterface|null $typeGuesser The guesser that the extension should support
*/
public function __construct(array $types, array $typeExtensions, FormTypeGuesserInterface $typeGuesser = null)
{
$this->typeExtensions = $typeExtensions;
$this->typeGuesser = $typeGuesser;
foreach ($types as $type) {
$this->types[get_class($type)] = $type;
}
}
/**
* {@inheritdoc}
*/
public function getType($name)
{
if (!isset($this->types[$name])) {
throw new InvalidArgumentException(sprintf('The type "%s" can not be loaded by this extension', $name));
}
return $this->types[$name];
}
/**
* {@inheritdoc}
*/
public function hasType($name)
{
return isset($this->types[$name]);
}
/**
* {@inheritdoc}
*/
public function getTypeExtensions($name)
{
return isset($this->typeExtensions[$name])
? $this->typeExtensions[$name]
: array();
}
/**
* {@inheritdoc}
*/
public function hasTypeExtensions($name)
{
return !empty($this->typeExtensions[$name]);
}
/**
* {@inheritdoc}
*/
public function getTypeGuesser()
{
return $this->typeGuesser;
}
}