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 3, 2020
1 parent 6bc37ad commit fd96314
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 133 deletions.
14 changes: 14 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
UPGRADE 3.x
===========

UPGRADE FROM 3.x to 3.x
=========================

### Changes in templates registry stucture.

`Sonata\AdminBundle\Templating\TemplateRegistry` was split to:
- `TemplateRegistry` based on `Sonata\AdminBundle\Templating\TemplateRegistryInterface`
- `MutableTemplateRegistry` based on `Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface`
- `AbstracTemplateRegistry` if you want create your own template registry

Some change are also made in aware interfaces:
- `Sonata\AdminBundle\Templating\TemplateRegistryAwareInterface` is for `TemplateRegistryInterface` now
- `Sonata\AdminBundle\Templating\MutableTemplateRegistryAwareInterface` was added and it is for `MutableTemplateRegistryInterface`

### Deprecated `Sonata\AdminBundle\Model\DatagridManagerInterface` interface.

This interface has been deprecated without replacement.
Expand Down
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')
;
};
66 changes: 66 additions & 0 deletions src/Templating/AbstractTemplateRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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 AbstractTemplateRegistry 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]);
}

/**
* NEXT_MAJOR: add type hint.
*
* @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;
}
}
34 changes: 34 additions & 0 deletions src/Templating/MutableTemplateRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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 AbstractTemplateRegistry implements MutableTemplateRegistryInterface
{
// NEXT_MAJOR: change method declaration for new one
// public function setTemplates(array $templates): void
public function setTemplates(array $templates)
{
$this->templates = $templates;
}

// NEXT_MAJOR: change method declaration for new one
// public function setTemplate(string $name, string $template): void
public function setTemplate($name, $template)
{
$this->templates[$name] = $template;
}
}
30 changes: 30 additions & 0 deletions src/Templating/MutableTemplateRegistryAwareInterface.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]>
*
* @method MutableTemplateRegistryInterface getTemplateRegistry()
* @method bool hasTemplateRegistry()
* @method void setTemplateRegistry(MutableTemplateRegistryInterface $templateRegistry)
*/
interface MutableTemplateRegistryAwareInterface
{
// NEXT_MAJOR: uncomment this method in 4.0
//public function getTemplateRegistry(): MutableTemplateRegistryInterface;

// NEXT_MAJOR: uncomment this method in 4.0
//public function setTemplateRegistry(MutableTemplateRegistryInterface $templateRegistry): void;
}
11 changes: 11 additions & 0 deletions src/Templating/MutableTemplateRegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,26 @@
interface MutableTemplateRegistryInterface extends TemplateRegistryInterface
{
/**
* NEXT_MAJOR: remove this method declaration with docblock and uncomment code below.
*
* @param array<string, string> $templates 'name' => 'file_path.html.twig'
*/
public function setTemplates(array $templates);

///**
// * @param array<string, string> $templates 'name' => 'file_path.html.twig'
// */
//public function setTemplates(array $templates): void;

/**
* NEXT_MAJOR: remove this method declaration with docblock and uncomment code below.
*
* @param string $name
* @param string $template
*
* @return void
*/
public function setTemplate($name, $template);

//public function setTemplate(string $name, string $template): void;
}
101 changes: 15 additions & 86 deletions src/Templating/TemplateRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,109 +15,38 @@

/**
* @author Timo Bakx <[email protected]>
*
* NEXT_MAJOR: remove `MutableTemplateRegistryInterface` implementation.
*/
final class TemplateRegistry implements MutableTemplateRegistryInterface
final class TemplateRegistry extends AbstractTemplateRegistry 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.
* NEXT_MAJOR: remove this method.
*
* @deprecated since sonata-project/admin-bundle 3.68, to be removed in 4.0. Use Sonata\AdminBundle\Templating\TemplateRegistry::TYPE_FLOAT instead.
* @deprecated since version sonata-project/admin-bundle 3.39.0 and will be removed in 4.0. Use Sonata\AdminBundle\Templating\MutableTemplateRegistry 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';

/**
* @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]);
@trigger_error(sprintf(
'Method "%s()" is deprecated since sonata-admin/admin-bundle 3.39 and will be removed in 4.0.',
__METHOD__
), E_USER_DEPRECATED);
}

/**
* @param string $name
* NEXT_MAJOR: remove this method.
*
* @deprecated since version sonata-project/admin-bundle 3.39.0 and will be removed in 4.0. Use Sonata\AdminBundle\Templating\MutableTemplateRegistry instead.
*/
public function getTemplate($name): ?string
public function setTemplate($name, $template)
{
if (isset($this->templates[$name])) {
return $this->templates[$name];
}
$this->templates[$name] = $template;

@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 "%s()" is deprecated since sonata-admin/admin-bundle 3.39 and will be removed 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;
}
}
11 changes: 5 additions & 6 deletions src/Templating/TemplateRegistryAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,20 @@
namespace Sonata\AdminBundle\Templating;

/**
* /**
* @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;
}
Loading

0 comments on commit fd96314

Please sign in to comment.