2012-07-30 08:07:04 +01:00
< ? 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 ;
2013-04-15 21:11:02 +01:00
use Symfony\Component\Form\Exception\InvalidArgumentException ;
2012-07-30 08:07:04 +01:00
/**
2016-09-14 08:33:27 +01:00
* A form extension with preloaded types , type extensions and type guessers .
2012-07-30 08:07:04 +01:00
*
* @ author Bernhard Schussek < bschussek @ gmail . com >
*/
class PreloadedExtension implements FormExtensionInterface
{
2019-01-16 09:39:14 +00:00
private $types = [];
private $typeExtensions = [];
2012-07-30 08:07:04 +01:00
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
2012-07-30 08:07:04 +01:00
*/
2012-11-08 18:49:46 +00:00
public function __construct ( array $types , array $typeExtensions , FormTypeGuesserInterface $typeGuesser = null )
2012-07-30 08:07:04 +01:00
{
2017-10-12 09:26:52 +01:00
foreach ( $typeExtensions as $extensions ) {
foreach ( $extensions as $typeExtension ) {
if ( ! method_exists ( $typeExtension , 'getExtendedTypes' )) {
@ trigger_error ( sprintf ( 'Not implementing the static getExtendedTypes() method in %s when implementing the %s is deprecated since Symfony 4.2. The method will be added to the interface in 5.0.' , \get_class ( $typeExtension ), FormTypeExtensionInterface :: class ), E_USER_DEPRECATED );
}
}
}
2012-07-30 08:07:04 +01:00
$this -> typeExtensions = $typeExtensions ;
$this -> typeGuesser = $typeGuesser ;
2015-06-23 17:42:21 +01:00
foreach ( $types as $type ) {
2018-07-05 12:24:53 +01:00
$this -> types [ \get_class ( $type )] = $type ;
2015-06-23 17:42:21 +01:00
}
2012-07-30 08:07:04 +01:00
}
/**
* { @ inheritdoc }
*/
public function getType ( $name )
{
if ( ! isset ( $this -> types [ $name ])) {
2013-04-15 21:11:02 +01:00
throw new InvalidArgumentException ( sprintf ( 'The type "%s" can not be loaded by this extension' , $name ));
2012-07-30 08:07:04 +01:00
}
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 ]
2019-01-16 09:39:14 +00:00
: [];
2012-07-30 08:07:04 +01:00
}
/**
* { @ inheritdoc }
*/
public function hasTypeExtensions ( $name )
{
2012-07-30 11:49:07 +01:00
return ! empty ( $this -> typeExtensions [ $name ]);
2012-07-30 08:07:04 +01:00
}
/**
* { @ inheritdoc }
*/
public function getTypeGuesser ()
{
return $this -> typeGuesser ;
}
}