Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PHP8 attributes for our existing service annotations #3619

Merged
merged 7 commits into from Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions core-bundle/src/DependencyInjection/Attribute/AsCallback.php
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\DependencyInjection\Attribute;

/**
* An attribute to register a DCA callback.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class AsCallback
{
public function __construct(public string $table, public string $target, public ?string $method = null, public ?int $priority = null)
{
}
}
33 changes: 33 additions & 0 deletions core-bundle/src/DependencyInjection/Attribute/AsContentElement.php
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\DependencyInjection\Attribute;

/**
* Service tag to autoconfigure content elements.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class AsContentElement
{
public array $attributes;

public function __construct(string $type = null, string $category, string $template = null, string $method = null, string $renderer = null, array ...$attributes)
leofeyer marked this conversation as resolved.
Show resolved Hide resolved
{
$attributes['type'] = $type;
$attributes['category'] = $category;
$attributes['template'] = $template;
$attributes['method'] = $method;
$attributes['renderer'] = $renderer;

$this->attributes = $attributes;
}
}
24 changes: 24 additions & 0 deletions core-bundle/src/DependencyInjection/Attribute/AsCronJob.php
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\DependencyInjection\Attribute;

/**
* An attribute to register a Contao cron job.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class AsCronJob
{
public function __construct(public string $interval, public ?string $method = null)
{
leofeyer marked this conversation as resolved.
Show resolved Hide resolved
}
}
33 changes: 33 additions & 0 deletions core-bundle/src/DependencyInjection/Attribute/AsFrontendModule.php
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\DependencyInjection\Attribute;

/**
* Service tag to autoconfigure frontend module.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class AsFrontendModule
{
public array $attributes;

public function __construct(string $type = null, string $category, string $template = null, string $method = null, string $renderer = null, array ...$attributes)
{
$attributes['type'] = $type;
$attributes['category'] = $category;
$attributes['template'] = $template;
$attributes['method'] = $method;
$attributes['renderer'] = $renderer;

$this->attributes = $attributes;
}
}
24 changes: 24 additions & 0 deletions core-bundle/src/DependencyInjection/Attribute/AsHook.php
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\DependencyInjection\Attribute;

/**
* An attribute to register a Contao hook.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class AsHook
{
public function __construct(public string $hook, public ?string $method = null, public ?int $priority = null)
{
}
}
26 changes: 26 additions & 0 deletions core-bundle/src/DependencyInjection/Attribute/AsPage.php
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\DependencyInjection\Attribute;

/**
* An attribute class for page controllers.
*
* @see \Symfony\Component\Routing\Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class AsPage
aschempp marked this conversation as resolved.
Show resolved Hide resolved
{
public function __construct(public ?string $type = null, public bool $contentComposition = true, public ?string $path = null, public ?string $urlSuffix = null, public array $requirements = [], public array $options = [], public array $defaults = [], public array $methods = [])
{
aschempp marked this conversation as resolved.
Show resolved Hide resolved
}
}
24 changes: 24 additions & 0 deletions core-bundle/src/DependencyInjection/Attribute/AsPickerProvider.php
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\DependencyInjection\Attribute;

/**
* An attribute to register a Contao picker.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class AsPickerProvider
{
public function __construct(public ?int $priority = null)
{
}
}
59 changes: 59 additions & 0 deletions core-bundle/src/DependencyInjection/ContaoCoreExtension.php
Expand Up @@ -13,7 +13,16 @@
namespace Contao\CoreBundle\DependencyInjection;

use Contao\CoreBundle\Crawl\Escargot\Subscriber\EscargotSubscriberInterface;
use Contao\CoreBundle\DependencyInjection\Attribute\AsCallback;
use Contao\CoreBundle\DependencyInjection\Attribute\AsContentElement;
use Contao\CoreBundle\DependencyInjection\Attribute\AsCronJob;
use Contao\CoreBundle\DependencyInjection\Attribute\AsFrontendModule;
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
use Contao\CoreBundle\DependencyInjection\Attribute\AsPage;
use Contao\CoreBundle\DependencyInjection\Attribute\AsPickerProvider;
use Contao\CoreBundle\EventListener\SearchIndexListener;
use Contao\CoreBundle\Fragment\Reference\ContentElementReference;
use Contao\CoreBundle\Fragment\Reference\FrontendModuleReference;
use Contao\CoreBundle\Migration\MigrationInterface;
use Contao\CoreBundle\Picker\PickerProviderInterface;
use Contao\CoreBundle\Routing\Page\ContentCompositionInterface;
Expand All @@ -23,6 +32,7 @@
use Imagine\Gd\Imagine;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -119,6 +129,55 @@ public function load(array $configs, ContainerBuilder $container): void
->registerForAutoconfiguration(ContentCompositionInterface::class)
->addTag('contao.page')
;

$container->registerAttributeForAutoconfiguration(
AsContentElement::class,
static function (ChildDefinition $definition, AsContentElement $attribute): void {
$definition->addTag(ContentElementReference::TAG_NAME, $attribute->attributes);
leofeyer marked this conversation as resolved.
Show resolved Hide resolved
}
);

$container->registerAttributeForAutoconfiguration(
AsFrontendModule::class,
static function (ChildDefinition $definition, AsFrontendModule $attribute): void {
$definition->addTag(FrontendModuleReference::TAG_NAME, $attribute->attributes);
}
);

$container->registerAttributeForAutoconfiguration(
AsCronJob::class,
static function (ChildDefinition $definition, AsCronJob $attribute): void {
$definition->addTag('contao.cronjob', get_object_vars($attribute));
}
);

$container->registerAttributeForAutoconfiguration(
AsHook::class,
static function (ChildDefinition $definition, AsHook $attribute): void {
$definition->addTag('contao.hook', get_object_vars($attribute));
}
);

$container->registerAttributeForAutoconfiguration(
AsCallback::class,
static function (ChildDefinition $definition, AsCallback $attribute): void {
$definition->addTag('contao.callback', get_object_vars($attribute));
}
);

$container->registerAttributeForAutoconfiguration(
AsPage::class,
static function (ChildDefinition $definition, AsPage $attribute): void {
$definition->addTag('contao.page', get_object_vars($attribute));
}
);

$container->registerAttributeForAutoconfiguration(
AsPickerProvider::class,
static function (ChildDefinition $definition, AsPickerProvider $attribute): void {
$definition->addTag('contao.picker_provider', get_object_vars($attribute));
}
);
}

private function handleSearchConfig(array $config, ContainerBuilder $container): void
Expand Down