Skip to content

Commit

Permalink
Make TemplateRegistry not mutable
Browse files Browse the repository at this point in the history
  • Loading branch information
wbloszyk committed Nov 1, 2020
1 parent be653e2 commit 2c98688
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 97 deletions.
5 changes: 5 additions & 0 deletions src/Admin/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class Pool
protected $propertyAccessor;

/**
* NEXT_MAJOR: change to TemplateRegistryInterface.
*
* @var MutableTemplateRegistryInterface
*/
private $templateRegistry;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
;
};
64 changes: 64 additions & 0 deletions src/Templating/BaseTemplateRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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<string, string>
*/
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;
}
}
30 changes: 30 additions & 0 deletions src/Templating/MutableTemplateRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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 <[email protected]>
*/
final class MutableTemplateRegistry extends BaseTemplateRegistry implements MutableTemplateRegistryInterface
{
public function setTemplates(array $templates)
{
$this->templates = $templates;
}

public function setTemplate($name, $template)
{
$this->templates[$name] = $template;
}
}
95 changes: 5 additions & 90 deletions src/Templating/TemplateRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,107 +15,22 @@

/**
* @author Timo Bakx <[email protected]>
*
* 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<string, string>
*/
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;
Expand Down
10 changes: 5 additions & 5 deletions src/Templating/TemplateRegistryAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
* /**
* @author Wojciech Błoszyk <[email protected]>
*
* @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;
}
46 changes: 46 additions & 0 deletions src/Templating/TemplateRegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> 'name' => 'file_path.html.twig'
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/Templating/TemplateRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down

0 comments on commit 2c98688

Please sign in to comment.