Skip to content

Commit

Permalink
[FEATURE] Introduce AsController attribute for auto configuration
Browse files Browse the repository at this point in the history
This introduces a new PHP attribute `AsController`,
which serves as a drop-in replacement for the former
introduced `Controller` attribute (#99055), which is
now deprecated.

This is done to unify the naming of our attributes.

The old `Controller` attribute is still working using
class alias mapping.

For upwards compatibility, the #[AsController] attribute
will be backported to v12 with a more slim variant of
this patch.

Resolves: #102631
Related: #99055
Releases: main, 12.4
Change-Id: Ib0ff0d08660a89c4a5c6c14a327542ec89095743
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/82152
Tested-by: core-ci <typo3@b13.com>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
  • Loading branch information
o-ba committed Dec 8, 2023
1 parent b611228 commit cc3f807
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
Expand Up @@ -21,7 +21,7 @@
* Service tag to autoconfigure Backend controllers
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class Controller
class AsController
{
public const TAG_NAME = 'backend.controller';

Expand Down
8 changes: 8 additions & 0 deletions typo3/sysext/backend/Configuration/Services.php
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use TYPO3\CMS\Backend\Attribute\AsController;
use TYPO3\CMS\Backend\Attribute\Controller;
use TYPO3\CMS\Backend\ContextMenu\ItemProviders\ProviderInterface;
use TYPO3\CMS\Backend\ElementBrowser\ElementBrowserInterface;
Expand All @@ -22,6 +23,13 @@
$containerBuilder->addCompilerPass(new PublicServicePass('backend.controller'));

// adds tag backend.controller to services
$containerBuilder->registerAttributeForAutoconfiguration(
AsController::class,
static function (ChildDefinition $definition, AsController $attribute): void {
$definition->addTag(AsController::TAG_NAME);
}
);
// Old attribute "#[Controller]", deprecated in TYPO3 v13 and will be removed with v14.
$containerBuilder->registerAttributeForAutoconfiguration(
Controller::class,
static function (ChildDefinition $definition, Controller $attribute): void {
Expand Down
1 change: 1 addition & 0 deletions typo3/sysext/backend/Migrations/Code/ClassAliasMap.php
Expand Up @@ -52,4 +52,5 @@
'TYPO3\\CMS\\Recordlist\\Tree\\View\\LinkParameterProviderInterface' => \TYPO3\CMS\Backend\Tree\View\LinkParameterProviderInterface::class,
'TYPO3\\CMS\\Recordlist\\View\\RecordSearchBoxComponent' => \TYPO3\CMS\Backend\View\RecordSearchBoxComponent::class,
'TYPO3\\CMS\\Recordlist\\View\\FolderUtilityRenderer' => \TYPO3\CMS\Backend\View\FolderUtilityRenderer::class,
'TYPO3\\CMS\\Backend\\Attribute\\Controller' => \TYPO3\CMS\Backend\Attribute\AsController::class,
];
4 changes: 4 additions & 0 deletions typo3/sysext/backend/Migrations/Code/LegacyClassesForIde.php
Expand Up @@ -19,6 +19,10 @@
die('Access denied');
}

namespace TYPO3\CMS\Backend\Attribute {
class Controller extends \TYPO3\CMS\Backend\Attribute\AsController {}
}

namespace TYPO3\CMS\Backend\Form\Element {
/**
* @deprecated since TYPO3 v12, will be removed in TYPO3 v13
Expand Down
Expand Up @@ -11,10 +11,17 @@ See :issue:`99055`
Description
===========

A new PHP attribute :php:`TYPO3\CMS\Backend\Attribute\Controller` has
A new PHP attribute :php:`TYPO3\CMS\Backend\Attribute\AsController` has
been added in order to register services to the BackendController dependency
injection container.

.. note::

In early TYPO3 v12 versions the attribute was named :php:`#[Controller]` and
has later been renamed to :php:`#[AsController]`. Both work with TYPO3 v12,
but developers should use :php:`#[AsController]` for upwards compatibility
since :php:`#[Controller]` has been deprecated with TYPO3 v13.

In addition to tag :yaml:`backend.controller` in the :file:`Services.yaml` file,
tagging services as backend controller can be done like:

Expand All @@ -23,9 +30,9 @@ Example implementation

.. code-block:: php
use TYPO3\CMS\Backend\Attribute\Controller;
use TYPO3\CMS\Backend\Attribute\AsController;
#[Controller]
#[AsController]
class MyBackendController {
}
Expand All @@ -34,7 +41,7 @@ Impact
======

It is now possible to tag services as backend controller by the PHP attribute
:php:`TYPO3\CMS\Backend\Attribute\Controller` instead of tagging them with
:php:`TYPO3\CMS\Backend\Attribute\AsController` instead of tagging them with
:yaml:`backend.controller` in the :file:`Services.yaml` file.

.. index:: Backend

0 comments on commit cc3f807

Please sign in to comment.