Skip to content

Commit

Permalink
IBX-8434: Added ProviderConfiguratorInterface to allow configuration …
Browse files Browse the repository at this point in the history
…post-processing
  • Loading branch information
Steveb-p authored Jul 10, 2024
1 parent 0d6ae20 commit 1d29e0c
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 127 deletions.
80 changes: 0 additions & 80 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,6 @@ parameters:
count: 1
path: src/lib/Configuration/Provider/CustomStyle.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\Provider\\\\CustomTag\\:\\:getConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Configuration/Provider/CustomTag.php

-
message: "#^Property Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\Provider\\\\CustomTag\\:\\:\\$customTagConfigurationMapper \\(Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\) does not accept Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTemplateConfigMapper\\.$#"
count: 1
path: src/lib/Configuration/Provider/CustomTag.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomStyle\\:\\:__construct\\(\\) has parameter \\$customStylesConfiguration with no value type specified in iterable type array\\.$#"
count: 1
Expand All @@ -350,76 +340,6 @@ parameters:
count: 1
path: src/lib/Configuration/UI/Mapper/CustomStyle.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\:\\:__construct\\(\\) has parameter \\$customTagAttributeMappers with no value type specified in iterable type Traversable\\.$#"
count: 1
path: src/lib/Configuration/UI/Mapper/CustomTag.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\:\\:__construct\\(\\) has parameter \\$customTagsConfiguration with no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Configuration/UI/Mapper/CustomTag.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\:\\:mapConfig\\(\\) has parameter \\$enabledCustomTags with no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Configuration/UI/Mapper/CustomTag.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\:\\:mapConfig\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Configuration/UI/Mapper/CustomTag.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\:\\:translateLabels\\(\\) has parameter \\$config with no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Configuration/UI/Mapper/CustomTag.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\:\\:translateLabels\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Configuration/UI/Mapper/CustomTag.php

-
message: "#^Property Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\:\\:\\$customTagAttributeMappers \\(array\\<Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\\\AttributeMapper\\>\\) does not accept Traversable\\.$#"
count: 1
path: src/lib/Configuration/UI/Mapper/CustomTag.php

-
message: "#^Property Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\:\\:\\$customTagsConfiguration type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Configuration/UI/Mapper/CustomTag.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\\\AttributeMapper\\:\\:mapConfig\\(\\) has parameter \\$customTagAttributeProperties with no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Configuration/UI/Mapper/CustomTag/AttributeMapper.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\\\AttributeMapper\\:\\:mapConfig\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Configuration/UI/Mapper/CustomTag/AttributeMapper.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\\\ChoiceAttributeMapper\\:\\:mapConfig\\(\\) has parameter \\$customTagAttributeProperties with no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Configuration/UI/Mapper/CustomTag/ChoiceAttributeMapper.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\\\ChoiceAttributeMapper\\:\\:mapConfig\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Configuration/UI/Mapper/CustomTag/ChoiceAttributeMapper.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\\\CommonAttributeMapper\\:\\:mapConfig\\(\\) has parameter \\$customTagAttributeProperties with no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Configuration/UI/Mapper/CustomTag/CommonAttributeMapper.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTag\\\\CommonAttributeMapper\\:\\:mapConfig\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Configuration/UI/Mapper/CustomTag/CommonAttributeMapper.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\Configuration\\\\UI\\\\Mapper\\\\CustomTemplateConfigMapper\\:\\:mapConfig\\(\\) has parameter \\$enabledCustomTemplates with no value type specified in iterable type array\\.$#"
count: 1
Expand Down
24 changes: 24 additions & 0 deletions src/bundle/Resources/config/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,42 @@ services:
arguments:
$customStyleConfigurationMapper: '@Ibexa\FieldTypeRichText\Configuration\UI\Mapper\CustomStyle'

ibexa.field_type_rich_text.configuration.provider.custom_style.configurable:
class: Ibexa\FieldTypeRichText\Configuration\Provider\ConfigurableProvider
decorates: Ibexa\FieldTypeRichText\Configuration\Provider\CustomStyle
arguments:
$configurators: !tagged_iterator ibexa.field_type.richtext.configuration.custom_style.configurator

Ibexa\FieldTypeRichText\Configuration\Provider\CustomTag:
arguments:
$customTagConfigurationMapper: '@Ibexa\FieldTypeRichText\Configuration\UI\Mapper\CustomTag'

ibexa.field_type_rich_text.configuration.provider.custom_tag.configurable:
class: Ibexa\FieldTypeRichText\Configuration\Provider\ConfigurableProvider
decorates: Ibexa\FieldTypeRichText\Configuration\Provider\CustomTag
arguments:
$configurators: !tagged_iterator ibexa.field_type.richtext.configuration.custom_tag.configurator

Ibexa\FieldTypeRichText\Configuration\Provider\AlloyEditor:
arguments:
$alloyEditorConfiguration: '%ibexa.field_type.richtext.alloy_editor%'

ibexa.field_type_rich_text.configuration.provider.alloy_editor.configurable:
class: Ibexa\FieldTypeRichText\Configuration\Provider\ConfigurableProvider
decorates: Ibexa\FieldTypeRichText\Configuration\Provider\AlloyEditor
arguments:
$configurators: !tagged_iterator ibexa.field_type.richtext.configuration.alloy_editor.configurator

Ibexa\FieldTypeRichText\Configuration\Provider\CKEditor:
arguments:
$customStylesConfiguration: '%ibexa.field_type.richtext.custom_styles%'

ibexa.field_type_rich_text.configuration.provider.ck_editor.configurable:
class: Ibexa\FieldTypeRichText\Configuration\Provider\ConfigurableProvider
decorates: Ibexa\FieldTypeRichText\Configuration\Provider\CKEditor
arguments:
$configurators: !tagged_iterator ibexa.field_type.richtext.configuration.ck_editor.configurator

Ibexa\FieldTypeRichText\Configuration\AggregateProvider:
arguments:
$providers: !tagged ibexa.field_type.richtext.configuration.provider
2 changes: 1 addition & 1 deletion src/bundle/Resources/config/ui/mappers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
$customTagsConfiguration: '%ibexa.field_type.richtext.custom_tags%'
$translatorBag: '@translator'
$translationDomain: '%ibexa.field_type.richtext.custom_tags.translation_domain%'
$customTagAttributeMappers: !tagged ibexa.field_type.richtext.configuration.custom_tag.mapper
$customTagAttributeMappers: !tagged_iterator ibexa.field_type.richtext.configuration.custom_tag.mapper

# RichText Custom Styles UI config mapper
Ibexa\FieldTypeRichText\Configuration\UI\Mapper\CustomStyle:
Expand Down
19 changes: 19 additions & 0 deletions src/contracts/Configuration/ProviderConfiguratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\FieldTypeRichText\Configuration;

interface ProviderConfiguratorInterface
{
/**
* @param array<string, mixed> $configuration
*
* @return array<string, mixed>
*/
public function getConfiguration(array $configuration): array;
}
48 changes: 48 additions & 0 deletions src/lib/Configuration/Provider/ConfigurableProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\FieldTypeRichText\Configuration\Provider;

use Ibexa\Contracts\FieldTypeRichText\Configuration\Provider;

final class ConfigurableProvider implements Provider
{
private Provider $inner;

/** @var iterable<\Ibexa\Contracts\FieldTypeRichText\Configuration\ProviderConfiguratorInterface> */
private iterable $configurators;

/**
* @param iterable<\Ibexa\Contracts\FieldTypeRichText\Configuration\ProviderConfiguratorInterface> $configurators
*/
public function __construct(
Provider $inner,
iterable $configurators
) {
$this->inner = $inner;
$this->configurators = $configurators;
}

public function getName(): string
{
return $this->inner->getName();
}

/**
* @return array<string, mixed>
*/
public function getConfiguration(): array
{
$configuration = $this->inner->getConfiguration();
foreach ($this->configurators as $configurator) {
$configuration = $configurator->getConfiguration($configuration);
}

return $configuration;
}
}
24 changes: 19 additions & 5 deletions src/lib/Configuration/Provider/CustomTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,28 @@
* Custom Tags configuration provider.
*
* @internal For internal use by RichText package
*
* @phpstan-type TConfigOutput array{
* label: string,
* description: string,
* isInline: bool,
* icon?: string,
* attributes?: array<string, TConfigAttributeOutput>
* }
* @phpstan-type TConfigAttributeOutput array{
* type: string,
* required: bool,
* defaultValue: mixed,
* label: string,
* choices?: array<string>,
* choicesLabel?: array<string, string>,
* }
*/
final class CustomTag implements Provider
{
/** @var \Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface */
private $configResolver;
private ConfigResolverInterface $configResolver;

/** @var \Ibexa\FieldTypeRichText\Configuration\UI\Mapper\CustomTag */
private $customTagConfigurationMapper;
private CustomTemplateConfigMapper $customTagConfigurationMapper;

public function __construct(
ConfigResolverInterface $configResolver,
Expand All @@ -39,7 +53,7 @@ public function getName(): string
}

/**
* @return array RichText Custom Tags config
* @phpstan-return array<TConfigOutput> RichText Custom Tags config
*/
public function getConfiguration(): array
{
Expand Down
67 changes: 35 additions & 32 deletions src/lib/Configuration/UI/Mapper/CustomTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,65 @@
use Symfony\Component\Asset\Packages;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Traversable;

/**
* RichText Custom Tag configuration mapper.
*
* @internal For internal use by RichText package
*
* @phpstan-type TConfig array{
* label: string,
* description: string,
* is_inline: bool,
* icon?: string,
* attributes: array<string, TConfigAttribute>
* }
* @phpstan-type TConfigAttribute array{
* type: string,
* required: bool,
* default_value: mixed,
* choices?: array<string>,
* }
*
* @phpstan-import-type TConfigOutput from \Ibexa\FieldTypeRichText\Configuration\Provider\CustomTag
* @phpstan-import-type TConfigAttributeOutput from \Ibexa\FieldTypeRichText\Configuration\Provider\CustomTag
*/
final class CustomTag implements CustomTemplateConfigMapper
{
/** @var array */
private $customTagsConfiguration;
/** @phpstan-var array<TConfig> */
private array $customTagsConfiguration;

/** @var \Symfony\Contracts\Translation\TranslatorInterface */
private $translator;
private TranslatorInterface $translator;

/** @var \Symfony\Component\Translation\TranslatorBagInterface */
private $translatorBag;
private TranslatorBagInterface $translatorBag;

/** @var \Symfony\Component\Asset\Packages */
private $packages;
private Packages $packages;

/** @var \Ibexa\FieldTypeRichText\Configuration\UI\Mapper\CustomTag\AttributeMapper[] */
private $customTagAttributeMappers;
/** @var iterable<\Ibexa\FieldTypeRichText\Configuration\UI\Mapper\CustomTag\AttributeMapper> */
private iterable $customTagAttributeMappers;

/** @var \Ibexa\FieldTypeRichText\Configuration\UI\Mapper\CustomTag\AttributeMapper[] */
private $supportedTagAttributeMappersCache;
/** @var array<\Ibexa\FieldTypeRichText\Configuration\UI\Mapper\CustomTag\AttributeMapper> */
private array $supportedTagAttributeMappersCache;

/** @var string */
private $translationDomain;
private string $translationDomain;

/**
* CustomTag configuration mapper constructor.
*
* Note: type-hinting Translator to have an instance which implements
* both TranslatorInterface and TranslatorBagInterface.
*
* @param array $customTagsConfiguration
* @param \Symfony\Contracts\Translation\TranslatorInterface $translator
* @param \Symfony\Component\Translation\TranslatorBagInterface $translatorBag
* @param string $translationDomain
* @param \Symfony\Component\Asset\Packages $packages
* @param \Traversable $customTagAttributeMappers
* @phpstan-param array<TConfig> $customTagsConfiguration
*
* @param iterable<\Ibexa\FieldTypeRichText\Configuration\UI\Mapper\CustomTag\AttributeMapper> $customTagAttributeMappers
*/
public function __construct(
array $customTagsConfiguration,
TranslatorInterface $translator,
TranslatorBagInterface $translatorBag,
string $translationDomain,
Packages $packages,
Traversable $customTagAttributeMappers
iterable $customTagAttributeMappers
) {
$this->customTagsConfiguration = $customTagsConfiguration;
$this->translator = $translator;
Expand All @@ -76,9 +85,9 @@ public function __construct(
/**
* Map Configuration for the given list of enabled Custom Tags.
*
* @param array $enabledCustomTags
* @phpstan-param array<string> $enabledCustomTags
*
* @return array Mapped configuration
* @phpstan-return array<TConfigOutput> Mapped configuration
*/
public function mapConfig(array $enabledCustomTags): array
{
Expand Down Expand Up @@ -123,12 +132,6 @@ public function mapConfig(array $enabledCustomTags): array

/**
* Get first available Custom Tag Attribute Type mapper.
*
* @param string $tagName
* @param string $attributeName
* @param string $attributeType
*
* @return \Ibexa\FieldTypeRichText\Configuration\UI\Mapper\CustomTag\AttributeMapper
*/
private function getAttributeTypeMapper(
string $tagName,
Expand All @@ -154,9 +157,9 @@ private function getAttributeTypeMapper(
/**
* Process Custom Tags config and translate labels for UI.
*
* @param array $config
* @param array<string, TConfigOutput> $config
*
* @return array processed Custom Tags config with translated labels
* @return array<string, TConfigOutput> processed Custom Tags config with translated labels
*/
private function translateLabels(array $config): array
{
Expand Down
Loading

0 comments on commit 1d29e0c

Please sign in to comment.