Skip to content

Commit

Permalink
Introduce SonataConfiguration class
Browse files Browse the repository at this point in the history
This class will handle configuration for sonata which is currently
holded by Pool class.

It adds a globaly twig "sonata_configuration" variable in order to
replace the current "sonata_admin" variable which all its methods
are deprecated.
  • Loading branch information
franmomu committed Dec 5, 2020
1 parent 2122648 commit 6809da5
Show file tree
Hide file tree
Showing 23 changed files with 498 additions and 34 deletions.
25 changes: 25 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ UPGRADE 3.x
UPGRADE FROM 3.xx to 3.xx
=========================

### Deprecated `admin_pool` parameter in `sonata.admin.dashboard.top` and `sonata.admin.dashboard.bottom` block events.

This parameter will be removed in 4.0. If you are using it, you SHOULD inject `Pool` service instead.

### Deprecated global Twig `sonata_admin` variable

This variable has been deprecated in favor of `sonata_config` variable.

### Sonata\AdminBundle\Twig\GlobalVariables

This class has been deprecated without replacement.

### Sonata\AdminBundle\Model\ModelManagerInterface

Argument 2 of `Sonata\AdminBundle\Model\ModelManagerInterface::createQuery()` method has been removed.

### Sonata\AdminBundle\Admin\Pool

- `Sonata\AdminBundle\Admin\Pool::getTitle()` method has been deprecated.
Use `Sonata\AdminBundle\SonataConfiguration::getTitle()` instead.
- `Sonata\AdminBundle\Admin\Pool::getTitleLogo()` method has been deprecated.
Use `Sonata\AdminBundle\SonataConfiguration::getLogo()` instead.
- `Sonata\AdminBundle\Admin\Pool::getOption()` method has been deprecated.
Use `Sonata\AdminBundle\SonataConfiguration::getOption()` instead.

### Sonata\AdminBundle\Admin\FieldDescriptionInterface

The following methods have been deprecated from the interface and will be added as abstract methods to
Expand Down
47 changes: 47 additions & 0 deletions src/Admin/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Admin;

use Sonata\AdminBundle\SonataConfiguration;
use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
Expand Down Expand Up @@ -60,16 +61,28 @@ class Pool
protected $assets = [];

/**
* NEXT_MAJOR: Remove this property.
*
* @deprecated since sonata-project/admin-bundle 3.x, will be dropped in 4.0.
*
* @var string
*/
protected $title;

/**
* NEXT_MAJOR: Remove this property.
*
* @deprecated since sonata-project/admin-bundle 3.x, will be dropped in 4.0.
*
* @var string
*/
protected $titleLogo;

/**
* NEXT_MAJOR: Remove this property.
*
* @deprecated since sonata-project/admin-bundle 3.x, will be dropped in 4.0.
*
* @var array
*/
protected $options = [];
Expand All @@ -92,6 +105,7 @@ class Pool

/**
* NEXT_MAJOR: Remove $propertyAccessor argument.
* NEXT_MAJOR: Remove $title, $logoTitle and $options.
*
* @param string $title
* @param string $logoTitle
Expand Down Expand Up @@ -532,29 +546,62 @@ public function getTemplate($name)
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/admin-bundle 3.x, will be dropped in 4.0.
*
* @return string
*/
public function getTitleLogo()
{
@trigger_error(sprintf(
'The "%s" method is deprecated since version 3.x and will be removed in 4.0.'
.' Use "%s::getTitle()" instead.',
SonataConfiguration::class,
__METHOD__
), E_USER_DEPRECATED);

return $this->titleLogo;
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/admin-bundle 3.x, will be dropped in 4.0.
*
* @return string
*/
public function getTitle()
{
@trigger_error(sprintf(
'The "%s" method is deprecated since version 3.x and will be removed in 4.0.'
.' Use "%s::getLogo()" instead.',
SonataConfiguration::class,
__METHOD__
), E_USER_DEPRECATED);

return $this->title;
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/admin-bundle 3.x, will be dropped in 4.0.
*
* @param string $name
* @param mixed $default
*
* @return mixed
*/
public function getOption($name, $default = null)
{
@trigger_error(sprintf(
'The "%s" method is deprecated since version 3.x and will be removed in 4.0.'
.' Use "%s::getOption()" instead.',
SonataConfiguration::class,
__METHOD__
), E_USER_DEPRECATED);

if (isset($this->options[$name])) {
return $this->options[$name];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class GlobalVariablesCompilerPass implements CompilerPassInterface
public function process(ContainerBuilder $container)
{
$container->getDefinition('twig')
->addMethodCall('addGlobal', ['sonata_config', new Reference('sonata.admin.configuration')])
// NEXT_MAJOR: Remove next line.
->addMethodCall('addGlobal', ['sonata_admin', new Reference('sonata.admin.twig.global')]);
}
}
6 changes: 6 additions & 0 deletions src/DependencyInjection/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,17 @@ public function load(array $configs, ContainerBuilder $container)
$config['options']['role_super_admin'] = $config['security']['role_super_admin'];
$config['options']['search'] = $config['search'];

// NEXT_MAJOR: Remove this Pool configuration.
$pool = $container->getDefinition('sonata.admin.pool');
$pool->replaceArgument(1, $config['title']);
$pool->replaceArgument(2, $config['title_logo']);
$pool->replaceArgument(3, $config['options']);

$sonataConfiguration = $container->getDefinition('sonata.admin.configuration');
$sonataConfiguration->replaceArgument(0, $config['title']);
$sonataConfiguration->replaceArgument(1, $config['title_logo']);
$sonataConfiguration->replaceArgument(2, $config['options']);

if (false === $config['options']['lock_protection']) {
$container->removeDefinition('sonata.admin.lock.extension');
}
Expand Down
11 changes: 11 additions & 0 deletions src/Resources/config/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Sonata\AdminBundle\Model\AuditManagerInterface;
use Sonata\AdminBundle\Route\AdminPoolLoader;
use Sonata\AdminBundle\Search\SearchHandler;
use Sonata\AdminBundle\SonataConfiguration;
use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface;
use Sonata\AdminBundle\Templating\TemplateRegistry;
use Sonata\AdminBundle\Translator\BCLabelTranslatorStrategy;
Expand Down Expand Up @@ -61,6 +62,15 @@

->alias(Pool::class, 'sonata.admin.pool')

->set('sonata.admin.configuration', SonataConfiguration::class)
->args([
'',
'',
[],
])

->alias(SonataConfiguration::class, 'sonata.admin.configuration')

->set('sonata.admin.route_loader', AdminPoolLoader::class)
->public()
->tag('routing.loader')
Expand Down Expand Up @@ -213,6 +223,7 @@
->public()
->tag('sonata.admin.extension', ['global' => true])

// NEXT_MAJOR: Remove this service definition and alias.
->set('sonata.admin.twig.global', GlobalVariables::class)
->public()
->args([
Expand Down
7 changes: 7 additions & 0 deletions src/Resources/config/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* file that was distributed with this source code.
*/

use Sonata\AdminBundle\Twig\Extension\GroupExtension;
use Sonata\AdminBundle\Twig\Extension\PaginationExtension;
use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
use Sonata\AdminBundle\Twig\Extension\TemplateRegistryExtension;
Expand Down Expand Up @@ -65,6 +66,12 @@
new ReferenceConfigurator('service_container'),
])

->set('sonata.admin.group.extension', GroupExtension::class)
->tag('twig.extension')
->args([
new ReferenceConfigurator('sonata.admin.pool'),
])

// NEXT_MAJOR: Remove this service.
->set('sonata.pagination.twig.extension', PaginationExtension::class)
->tag('twig.extension')
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/views/Block/block_admin_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ file that was distributed with this source code.

{% block block %}
{% for group in groups %}
{% set display = group.roles is empty or is_granted(sonata_admin.adminPool.getOption('role_super_admin')) or group.roles|filter(role => is_granted(role))|length > 0 %}
{% set display = group.roles is empty or is_granted(sonata_config.getOption('role_super_admin')) or group.roles|filter(role => is_granted(role))|length > 0 %}

{% if display %}
<div class="box">
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/views/CRUD/base_acl.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ file that was distributed with this source code.

{% block form %}
{% block form_acl_roles %}
{{ acl.render_form(aclRolesForm, permissions, 'td_role', admin, sonata_admin.adminPool, object) }}
{{ acl.render_form(aclRolesForm, permissions, 'td_role', admin, sonata_config, object) }}
{% endblock %}
{% block form_acl_users %}
{{ acl.render_form(aclUsersForm, permissions, 'td_username', admin, sonata_admin.adminPool, object) }}
{{ acl.render_form(aclUsersForm, permissions, 'td_username', admin, sonata_config, object) }}
{% endblock %}
{% endblock %}
4 changes: 2 additions & 2 deletions src/Resources/views/CRUD/base_acl_macro.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ file that was distributed with this source code.
#}

{% macro render_form(form, permissions, td_type, admin, admin_pool, object) %}
{% macro render_form(form, permissions, td_type, admin, admin_configuration, object) %}
<form class="form-horizontal"
action="{{ admin.generateUrl('acl', {'id': admin.id(object), 'uniqid': admin.uniqid, 'subclass': app.request.get('subclass')}) }}"
{% if form.vars.multipart %} enctype="multipart/form-data"{% endif %}
method="POST"
{% if not admin_pool.getOption('html5_validate') %}novalidate="novalidate"{% endif %}
{% if not admin_configuration.getOption('html5_validate') %}novalidate="novalidate"{% endif %}
>

{{ include('@SonataAdmin/Helper/render_form_dismissable_errors.html.twig') }}
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/views/CRUD/base_edit_form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
</div>
{% else %}
<form
{% if sonata_admin.adminPool.getOption('form_type') == 'horizontal' %}class="form-horizontal"{% endif %}
{% if sonata_config.getOption('form_type') == 'horizontal' %}class="form-horizontal"{% endif %}
role="form"
{# NEXT_MAJOR: remove default filter #}
action="{% block sonata_form_action_url %}{{ admin.generateUrl(url, {'id': objectId|default(admin.id(object)), 'uniqid': admin.uniqid, 'subclass': app.request.get('subclass')}) }}{% endblock %}"
{% if form.vars.multipart %} enctype="multipart/form-data"{% endif %}
method="POST"
{% if not sonata_admin.adminPool.getOption('html5_validate') %}novalidate="novalidate"{% endif %}
{% if not sonata_config.getOption('html5_validate') %}novalidate="novalidate"{% endif %}
{% block sonata_form_attributes %}{% endblock %}
>

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/views/CRUD/list_outer_rows_mosaic.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ This template can be customized to match your needs. You should only extends the

<div class="mosaic-inner-box-default">
{% block sonata_mosaic_background %}
{% set metaImage = meta.isImageAvailable is defined and not meta.isImageAvailable ? sonata_admin.mosaicBackground : meta.image %}
{% set metaImage = meta.isImageAvailable is defined and not meta.isImageAvailable ? sonata_config.getOption('mosaic_background') : meta.image %}
{% if not (metaImage starts with 'data:') %}
{% set metaImage = asset(metaImage) %}
{% endif %}
Expand Down
9 changes: 3 additions & 6 deletions src/Resources/views/Core/add_block.html.twig
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
{% block user_block %}
{% set items_per_column = sonata_admin.adminPool.getOption('dropdown_number_groups_per_colums') %}
{% set groups = [] %}
{% set items_per_column = sonata_config.getOption('dropdown_number_groups_per_colums') %}

{% for group in sonata_admin.adminPool.dashboardgroups|filter(group => group.items|filter(admin => admin.hasRoute('create') and admin.hasAccess('create'))|length > 0) %}
{% set groups = [group]|merge(groups) %}
{% endfor %}
{% set groups = get_sonata_dashboard_groups_with_creatable_admins() %}

{% set column_count = (groups|length / items_per_column)|round(0, 'ceil') %}

<div class="dropdown-menu multi-column dropdown-add"
{% if column_count > 1 %}style="width: {{ column_count*140 }}px;"{% endif %}
>
{% for group in groups|reverse %}
{% set display = group.roles is empty or is_granted(sonata_admin.adminPool.getOption('role_admin')) or group.roles|filter(role => is_granted(role))|length > 0 %}
{% set display = group.roles is empty or is_granted(sonata_config.getOption('role_admin')) or group.roles|filter(role => is_granted(role))|length > 0 %}

{% if loop.first %}
{% set render_first_element = true %}
Expand Down
6 changes: 4 additions & 2 deletions src/Resources/views/Core/dashboard.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ file that was distributed with this source code.
{% endif %}
{% endfor %}

{{ sonata_block_render_event('sonata.admin.dashboard.top', { 'admin_pool': sonata_admin.adminPool }) }}
{# NEXT_MAJOR: Remove the admin_pool argument #}
{{ sonata_block_render_event('sonata.admin.dashboard.top', { 'admin_pool': sonata_admin.adminPool('sonata_deprecation_mute') }) }}

{% if has_top %}
<div class="row">
Expand Down Expand Up @@ -125,6 +126,7 @@ file that was distributed with this source code.
</div>
{% endif %}

{{ sonata_block_render_event('sonata.admin.dashboard.bottom', { 'admin_pool': sonata_admin.adminPool }) }}
{# NEXT_MAJOR: Remove the admin_pool argument #}
{{ sonata_block_render_event('sonata.admin.dashboard.bottom', { 'admin_pool': sonata_admin.adminPool('sonata_deprecation_mute') }) }}

{% endblock %}
2 changes: 1 addition & 1 deletion src/Resources/views/Core/search.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ file that was distributed with this source code.
{% set count = 0 %}
<div class="row" data-masonry='{ "itemSelector": ".search-box-item" }'>
{% for group in groups %}
{% set display = group.roles is empty or is_granted(sonata_admin.adminPool.getOption('role_super_admin')) or group.roles|filter(role => is_granted(role))|length > 0 %}
{% set display = group.roles is empty or is_granted(sonata_config.getOption('role_super_admin')) or group.roles|filter(role => is_granted(role))|length > 0 %}

{% if display %}
{% for admin in group.items %}
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/views/Menu/sonata_menu.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{% block item %}
{%- if item.displayed -%}
{#- check role of the group #}
{%- set display = item.extra('roles') is empty or is_granted(sonata_admin.adminPool.getOption('role_super_admin')) or item.extra('roles')|filter(role => is_granted(role))|length > 0 -%}
{%- set display = item.extra('roles') is empty or is_granted(sonata_config.getOption('role_super_admin')) or item.extra('roles')|filter(role => is_granted(role))|length > 0 -%}
{%- endif -%}

{%- if item.displayed and display|default -%}
Expand Down
Loading

0 comments on commit 6809da5

Please sign in to comment.