From 2c98688a2cecda6ab0de8e6cabd17528336ab86e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20B=C5=82oszyk?= Date: Sat, 31 Oct 2020 18:04:32 +0100 Subject: [PATCH] Make TemplateRegistry not mutable --- src/Admin/Pool.php | 5 + src/Resources/config/core.php | 1 + src/Templating/BaseTemplateRegistry.php | 64 +++++++++++++ src/Templating/MutableTemplateRegistry.php | 30 ++++++ src/Templating/TemplateRegistry.php | 95 +------------------ .../TemplateRegistryAwareInterface.php | 10 +- src/Templating/TemplateRegistryInterface.php | 46 +++++++++ tests/Templating/TemplateRegistryTest.php | 4 +- 8 files changed, 158 insertions(+), 97 deletions(-) create mode 100644 src/Templating/BaseTemplateRegistry.php create mode 100644 src/Templating/MutableTemplateRegistry.php diff --git a/src/Admin/Pool.php b/src/Admin/Pool.php index edc2720e0be..df0cd3bbc8e 100644 --- a/src/Admin/Pool.php +++ b/src/Admin/Pool.php @@ -80,6 +80,8 @@ class Pool protected $propertyAccessor; /** + * NEXT_MAJOR: change to TemplateRegistryInterface. + * * @var MutableTemplateRegistryInterface */ private $templateRegistry; @@ -469,6 +471,9 @@ public function getAdminClasses() return $this->adminClasses; } + /** + * NEXT_MAJOR: change to TemplateRegistryInterface. + */ final public function setTemplateRegistry(MutableTemplateRegistryInterface $templateRegistry): void { $this->templateRegistry = $templateRegistry; diff --git a/src/Resources/config/core.php b/src/Resources/config/core.php index af039764212..54c21d057b0 100644 --- a/src/Resources/config/core.php +++ b/src/Resources/config/core.php @@ -238,6 +238,7 @@ ->alias(TemplateRegistry::class, 'sonata.admin.global_template_registry') + // NEXT_MAJOR: remove this alias, global template registry should not be mutable ->alias(MutableTemplateRegistryInterface::class, 'sonata.admin.global_template_registry') ; }; diff --git a/src/Templating/BaseTemplateRegistry.php b/src/Templating/BaseTemplateRegistry.php new file mode 100644 index 00000000000..9d03b63ccb6 --- /dev/null +++ b/src/Templating/BaseTemplateRegistry.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Templating; + +abstract class BaseTemplateRegistry implements TemplateRegistryInterface +{ + /** + * @var array + */ + protected $templates = []; + + /** + * @param string[] $templates + */ + public function __construct(array $templates = []) + { + $this->templates = $templates; + } + + public function getTemplates(): array + { + return $this->templates; + } + + public function hasTemplate(string $name): bool + { + return isset($this->templates[$name]); + } + + /** + * @param string $name + */ + public function getTemplate($name): ?string + { + if ($this->hasTemplate($name)) { + return $this->templates[$name]; + } + + @trigger_error(sprintf( + 'Passing a nonexistent template name as argument 1 to %s() is deprecated since' + .' sonata-project/admin-bundle 3.52 and will throw an exception in 4.0.', + __METHOD__ + ), E_USER_DEPRECATED); + + // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare string as return type + // throw new \InvalidArgumentException(sprintf( + // 'Template named "%s" doesn\'t exist.', + // $name + // )); + + return null; + } +} diff --git a/src/Templating/MutableTemplateRegistry.php b/src/Templating/MutableTemplateRegistry.php new file mode 100644 index 00000000000..8ea37998d92 --- /dev/null +++ b/src/Templating/MutableTemplateRegistry.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Templating; + +/** + * @author Wojciech Błoszyk + */ +final class MutableTemplateRegistry extends BaseTemplateRegistry implements MutableTemplateRegistryInterface +{ + public function setTemplates(array $templates) + { + $this->templates = $templates; + } + + public function setTemplate($name, $template) + { + $this->templates[$name] = $template; + } +} diff --git a/src/Templating/TemplateRegistry.php b/src/Templating/TemplateRegistry.php index 959390c29d0..e37d19df89b 100644 --- a/src/Templating/TemplateRegistry.php +++ b/src/Templating/TemplateRegistry.php @@ -15,107 +15,22 @@ /** * @author Timo Bakx + * + * NEXT_MAJOR: remove `MutableTemplateRegistryInterface` implementation with setter methods */ -final class TemplateRegistry implements MutableTemplateRegistryInterface +final class TemplateRegistry extends BaseTemplateRegistry implements MutableTemplateRegistryInterface { - public const TYPE_ARRAY = 'array'; - public const TYPE_BOOLEAN = 'boolean'; - public const TYPE_DATE = 'date'; - public const TYPE_TIME = 'time'; - public const TYPE_DATETIME = 'datetime'; - /** - * NEXT_MAJOR: Remove this constant. - * - * @deprecated since sonata-project/admin-bundle 3.68, to be removed in 4.0. Use Sonata\AdminBundle\Templating\TemplateRegistry::TYPE_STRING instead. - */ - public const TYPE_TEXT = 'text'; - public const TYPE_TEXTAREA = 'textarea'; - public const TYPE_EMAIL = 'email'; - public const TYPE_TRANS = 'trans'; - public const TYPE_STRING = 'string'; - /** - * NEXT_MAJOR: Remove this constant. - * - * @deprecated since sonata-project/admin-bundle 3.68, to be removed in 4.0. Use Sonata\AdminBundle\Templating\TemplateRegistry::TYPE_INTEGER instead. - */ - public const TYPE_SMALLINT = 'smallint'; - /** - * NEXT_MAJOR: Remove this constant. - * - * @deprecated since sonata-project/admin-bundle 3.68, to be removed in 4.0. Use Sonata\AdminBundle\Templating\TemplateRegistry::TYPE_INTEGER instead. - */ - public const TYPE_BIGINT = 'bigint'; - public const TYPE_INTEGER = 'integer'; /** - * NEXT_MAJOR: Remove this constant. - * - * @deprecated since sonata-project/admin-bundle 3.68, to be removed in 4.0. Use Sonata\AdminBundle\Templating\TemplateRegistry::TYPE_FLOAT instead. + * NEXT_MAJOR: remove this method. */ - public const TYPE_DECIMAL = 'decimal'; - public const TYPE_FLOAT = 'float'; - public const TYPE_IDENTIFIER = 'identifier'; - public const TYPE_CURRENCY = 'currency'; - public const TYPE_PERCENT = 'percent'; - public const TYPE_CHOICE = 'choice'; - public const TYPE_URL = 'url'; - public const TYPE_HTML = 'html'; - public const TYPE_MANY_TO_MANY = 'many_to_many'; - public const TYPE_MANY_TO_ONE = 'many_to_one'; - public const TYPE_ONE_TO_MANY = 'one_to_many'; - public const TYPE_ONE_TO_ONE = 'one_to_one'; - - /** - * @var array - */ - private $templates = []; - - /** - * @param string[] $templates - */ - public function __construct(array $templates = []) - { - $this->templates = $templates; - } - - public function getTemplates(): array - { - return $this->templates; - } - public function setTemplates(array $templates) { $this->templates = $templates; } - public function hasTemplate(string $name): bool - { - return isset($this->templates[$name]); - } - /** - * @param string $name + * NEXT_MAJOR: remove this method. */ - public function getTemplate($name): ?string - { - if (isset($this->templates[$name])) { - return $this->templates[$name]; - } - - @trigger_error(sprintf( - 'Passing a nonexistent template name as argument 1 to %s() is deprecated since' - .' sonata-project/admin-bundle 3.52 and will throw an exception in 4.0.', - __METHOD__ - ), E_USER_DEPRECATED); - - // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare string as return type - // throw new \InvalidArgumentException(sprintf( - // 'Template named "%s" doesn\'t exist.', - // $name - // )); - - return null; - } - public function setTemplate($name, $template) { $this->templates[$name] = $template; diff --git a/src/Templating/TemplateRegistryAwareInterface.php b/src/Templating/TemplateRegistryAwareInterface.php index 14743c80978..ef73e938c62 100644 --- a/src/Templating/TemplateRegistryAwareInterface.php +++ b/src/Templating/TemplateRegistryAwareInterface.php @@ -17,18 +17,18 @@ * /** * @author Wojciech Błoszyk * - * @method MutableTemplateRegistryInterface getTemplateRegistry() - * @method bool hasTemplateRegistry() - * @method void setTemplateRegistry(MutableTemplateRegistryInterface $templateRegistry) + * @method TemplateRegistryInterface getTemplateRegistry() + * @method bool hasTemplateRegistry() + * @method void setTemplateRegistry(TemplateRegistryInterface $templateRegistry) */ interface TemplateRegistryAwareInterface { // NEXT_MAJOR: uncomment this method in 4.0 - //public function getTemplateRegistry(): MutableTemplateRegistryInterface; + //public function getTemplateRegistry(): TemplateRegistryInterface; // NEXT_MAJOR: uncomment this method in 4.0 //public function hasTemplateRegistry(): bool; // NEXT_MAJOR: uncomment this method in 4.0 - //public function setTemplateRegistry(MutableTemplateRegistryInterface $templateRegistry): void; + //public function setTemplateRegistry(TemplateRegistryInterface $templateRegistry): void; } diff --git a/src/Templating/TemplateRegistryInterface.php b/src/Templating/TemplateRegistryInterface.php index 97b5e93b606..e1a10f3eecb 100644 --- a/src/Templating/TemplateRegistryInterface.php +++ b/src/Templating/TemplateRegistryInterface.php @@ -20,6 +20,52 @@ */ interface TemplateRegistryInterface { + public const TYPE_ARRAY = 'array'; + public const TYPE_BOOLEAN = 'boolean'; + public const TYPE_DATE = 'date'; + public const TYPE_TIME = 'time'; + public const TYPE_DATETIME = 'datetime'; + /** + * NEXT_MAJOR: Remove this constant. + * + * @deprecated since sonata-project/admin-bundle 3.68, to be removed in 4.0. Use Sonata\AdminBundle\Templating\TemplateRegistry::TYPE_STRING instead. + */ + public const TYPE_TEXT = 'text'; + public const TYPE_TEXTAREA = 'textarea'; + public const TYPE_EMAIL = 'email'; + public const TYPE_TRANS = 'trans'; + public const TYPE_STRING = 'string'; + /** + * NEXT_MAJOR: Remove this constant. + * + * @deprecated since sonata-project/admin-bundle 3.68, to be removed in 4.0. Use Sonata\AdminBundle\Templating\TemplateRegistry::TYPE_INTEGER instead. + */ + public const TYPE_SMALLINT = 'smallint'; + /** + * NEXT_MAJOR: Remove this constant. + * + * @deprecated since sonata-project/admin-bundle 3.68, to be removed in 4.0. Use Sonata\AdminBundle\Templating\TemplateRegistry::TYPE_INTEGER instead. + */ + public const TYPE_BIGINT = 'bigint'; + public const TYPE_INTEGER = 'integer'; + /** + * NEXT_MAJOR: Remove this constant. + * + * @deprecated since sonata-project/admin-bundle 3.68, to be removed in 4.0. Use Sonata\AdminBundle\Templating\TemplateRegistry::TYPE_FLOAT instead. + */ + public const TYPE_DECIMAL = 'decimal'; + public const TYPE_FLOAT = 'float'; + public const TYPE_IDENTIFIER = 'identifier'; + public const TYPE_CURRENCY = 'currency'; + public const TYPE_PERCENT = 'percent'; + public const TYPE_CHOICE = 'choice'; + public const TYPE_URL = 'url'; + public const TYPE_HTML = 'html'; + public const TYPE_MANY_TO_MANY = 'many_to_many'; + public const TYPE_MANY_TO_ONE = 'many_to_one'; + public const TYPE_ONE_TO_MANY = 'one_to_many'; + public const TYPE_ONE_TO_ONE = 'one_to_one'; + /** * @return array 'name' => 'file_path.html.twig' */ diff --git a/tests/Templating/TemplateRegistryTest.php b/tests/Templating/TemplateRegistryTest.php index bd62168e215..803fe49727b 100644 --- a/tests/Templating/TemplateRegistryTest.php +++ b/tests/Templating/TemplateRegistryTest.php @@ -45,7 +45,7 @@ public function testGetTemplates(): void /** * @group legacy * - * @expectedDeprecation Passing a nonexistent template name as argument 1 to Sonata\AdminBundle\Templating\TemplateRegistry::getTemplate() is deprecated since sonata-project/admin-bundle 3.52 and will throw an exception in 4.0. + * @expectedDeprecation Passing a nonexistent template name as argument 1 to Sonata\AdminBundle\Templating\BaseTemplateRegistry::getTemplate() is deprecated since sonata-project/admin-bundle 3.52 and will throw an exception in 4.0. */ public function testGetTemplate1(): void { @@ -66,7 +66,7 @@ public function testGetTemplate1(): void /** * @group legacy * - * @expectedDeprecation Passing a nonexistent template name as argument 1 to Sonata\AdminBundle\Templating\TemplateRegistry::getTemplate() is deprecated since sonata-project/admin-bundle 3.52 and will throw an exception in 4.0. + * @expectedDeprecation Passing a nonexistent template name as argument 1 to Sonata\AdminBundle\Templating\BaseTemplateRegistry::getTemplate() is deprecated since sonata-project/admin-bundle 3.52 and will throw an exception in 4.0. */ public function testGetTemplate2(): void {