From 1a91a2df80267a66caf80c17c85297a1ef577645 Mon Sep 17 00:00:00 2001 From: Andrei Shapiro Date: Tue, 10 Sep 2024 17:26:55 +0300 Subject: [PATCH] chore: use proxies instead of manager --- helpers/form/class.FormContainer.php | 15 +++-- manifest.php | 2 - ...difier.php => TranslationFormModifier.php} | 34 ++++------- .../TranslationServiceProvider.php | 16 +---- .../FeatureFlagCheckerInterface.php | 1 - .../form/Modifier/AbstractFormModifier.php | 25 ++++---- .../form/Modifier/FormModifierInterface.php | 35 ----------- .../form/Modifier/FormModifierManager.php | 58 ------------------- .../Modifier/FormModifierServiceProvider.php | 38 ------------ 9 files changed, 31 insertions(+), 193 deletions(-) rename models/classes/Translation/Form/Modifier/{TranslationInstanceFormModifier.php => TranslationFormModifier.php} (71%) delete mode 100644 models/classes/form/Modifier/FormModifierInterface.php delete mode 100644 models/classes/form/Modifier/FormModifierManager.php delete mode 100644 models/classes/form/Modifier/FormModifierServiceProvider.php diff --git a/helpers/form/class.FormContainer.php b/helpers/form/class.FormContainer.php index eaae9c496b..bd5437a8ad 100644 --- a/helpers/form/class.FormContainer.php +++ b/helpers/form/class.FormContainer.php @@ -26,7 +26,7 @@ use oat\oatbox\service\ServiceManager; use oat\oatbox\validator\ValidatorInterface; -use oat\tao\model\form\Modifier\FormModifierManager; +use oat\tao\model\form\Modifier\AbstractFormModifier; use oat\tao\model\security\xsrf\TokenService; use Psr\Container\ContainerInterface; use tao_helpers_form_FormFactory as FormFactory; @@ -121,8 +121,12 @@ public function __construct(array $data = [], array $options = []) // initialize the elements of the form $this->initElements(); - if (!empty($options[self::FORM_MODIFIERS])) { - $this->getFormModifierManager()->modify($this->form, $options[self::FORM_MODIFIERS]); + foreach ($options[self::FORM_MODIFIERS] ?? [] as $modifierClass) { + $modifier = $this->getContainer()->get($modifierClass); + + if ($modifier instanceof AbstractFormModifier) { + $modifier->modify($this->form, $options); + } } if ($this->form !== null) { @@ -330,11 +334,6 @@ private function getSanitizerValidationFeeder(): SanitizerValidationFeederInterf return $this->sanitizerValidationFeeder; } - private function getFormModifierManager(): FormModifierManager - { - return $this->getContainer()->get(FormModifierManager::class); - } - private function withServiceManager(array &$options): void { if ( diff --git a/manifest.php b/manifest.php index 6f5a7ca714..4d92c7f509 100755 --- a/manifest.php +++ b/manifest.php @@ -43,7 +43,6 @@ use oat\tao\model\featureFlag\FeatureFlagServiceProvider; use oat\tao\model\featureVisibility\FeatureVisibilityServiceProvider; use oat\tao\model\form\DataProvider\FormDataProviderServiceProvider; -use oat\tao\model\form\Modifier\FormModifierServiceProvider; use oat\tao\model\import\ServiceProvider\ImportServiceProvider; use oat\tao\model\LanguageServiceProvider; use oat\tao\model\listener\PropertyServiceProvider; @@ -422,7 +421,6 @@ FormDataProviderServiceProvider::class, PropertyServiceProvider::class, DynamicConfigServiceProvider::class, - FormModifierServiceProvider::class, TranslationServiceProvider::class ], 'middlewares' => [ diff --git a/models/classes/Translation/Form/Modifier/TranslationInstanceFormModifier.php b/models/classes/Translation/Form/Modifier/TranslationFormModifier.php similarity index 71% rename from models/classes/Translation/Form/Modifier/TranslationInstanceFormModifier.php rename to models/classes/Translation/Form/Modifier/TranslationFormModifier.php index f10d95f5b4..b9af7af51d 100644 --- a/models/classes/Translation/Form/Modifier/TranslationInstanceFormModifier.php +++ b/models/classes/Translation/Form/Modifier/TranslationFormModifier.php @@ -22,48 +22,31 @@ namespace oat\tao\model\Translation\Form\Modifier; -use oat\generis\model\data\Ontology; use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; use oat\tao\model\form\Modifier\AbstractFormModifier; use oat\tao\model\TaoOntology; -use tao_helpers_form_Form as Form; +use tao_helpers_form_Form; use tao_helpers_Uri; -class TranslationInstanceFormModifier extends AbstractFormModifier +class TranslationFormModifier extends AbstractFormModifier { - public const ID = 'tao.form_modifier.translation_instance'; - private FeatureFlagCheckerInterface $featureFlagChecker; - public function __construct(Ontology $ontology, FeatureFlagCheckerInterface $featureFlagChecker) + public function __construct(FeatureFlagCheckerInterface $featureFlagChecker) { - parent::__construct($ontology); - $this->featureFlagChecker = $featureFlagChecker; } - public function supports(Form $form, array $options = []): bool - { - $instance = $this->getInstance($form, $options); - - if ($instance === null) { - return false; - } - - return $instance->isInstanceOf($this->ontology->getClass(TaoOntology::CLASS_URI_ITEM)) - || $instance->isInstanceOf($this->ontology->getClass(TaoOntology::CLASS_URI_TEST)); - } - - public function modify(Form $form, array $options = []): void + public function modify(tao_helpers_form_Form $form, array $options = []): void { foreach ($this->getTranslationElementsToRemove($form) as $elementUri) { $form->removeElement(tao_helpers_Uri::encode($elementUri)); } } - private function getTranslationElementsToRemove(Form $form): array + private function getTranslationElementsToRemove(tao_helpers_form_Form $form): array { - if (!$this->featureFlagChecker->isEnabled(FeatureFlagCheckerInterface::FEATURE_TRANSLATION_ENABLED)) { + if (!$this->featureFlagChecker->isEnabled('FEATURE_TRANSLATION_ENABLED')) { return [ TaoOntology::PROPERTY_UNIQUE_IDENTIFIER, TaoOntology::PROPERTY_LANGUAGE, @@ -74,6 +57,11 @@ private function getTranslationElementsToRemove(Form $form): array } $elementsToRemove = []; + + if (!$this->featureFlagChecker->isEnabled('FEATURE_TRANSLATION_DEVELOPER_MODE')) { + $elementsToRemove[] = TaoOntology::PROPERTY_TRANSLATION_TYPE; + } + $translationTypeValue = $form->getValue(tao_helpers_Uri::encode(TaoOntology::PROPERTY_TRANSLATION_TYPE)); $isTranslationTypeValueEmpty = empty($translationTypeValue); diff --git a/models/classes/Translation/ServiceProvider/TranslationServiceProvider.php b/models/classes/Translation/ServiceProvider/TranslationServiceProvider.php index 4b952950d6..c5d55c4d5c 100644 --- a/models/classes/Translation/ServiceProvider/TranslationServiceProvider.php +++ b/models/classes/Translation/ServiceProvider/TranslationServiceProvider.php @@ -27,10 +27,9 @@ use oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService; use oat\oatbox\log\LoggerService; use oat\tao\model\featureFlag\FeatureFlagChecker; -use oat\tao\model\form\Modifier\FormModifierManager; use oat\tao\model\Translation\Factory\ResourceTranslatableFactory; use oat\tao\model\Translation\Factory\ResourceTranslationFactory; -use oat\tao\model\Translation\Form\Modifier\TranslationInstanceFormModifier; +use oat\tao\model\Translation\Form\Modifier\TranslationFormModifier; use oat\tao\model\Translation\Repository\ResourceTranslatableRepository; use oat\tao\model\Translation\Repository\ResourceTranslationRepository; use oat\tao\model\Translation\Service\ResourceMetadataPopulateService; @@ -106,20 +105,9 @@ public function __invoke(ContainerConfigurator $configurator): void ->public(); $services - ->set(TranslationInstanceFormModifier::class, TranslationInstanceFormModifier::class) + ->set(TranslationFormModifier::class, TranslationFormModifier::class) ->args([ - service(Ontology::SERVICE_ID), service(FeatureFlagChecker::class), ]); - - $formModifierManager = $services->get(FormModifierManager::class); - $formModifierManager - ->call( - 'add', - [ - service(TranslationInstanceFormModifier::class), - TranslationInstanceFormModifier::ID, - ] - ); } } diff --git a/models/classes/featureFlag/FeatureFlagCheckerInterface.php b/models/classes/featureFlag/FeatureFlagCheckerInterface.php index 2debcb982d..f4bb9616d5 100644 --- a/models/classes/featureFlag/FeatureFlagCheckerInterface.php +++ b/models/classes/featureFlag/FeatureFlagCheckerInterface.php @@ -31,7 +31,6 @@ interface FeatureFlagCheckerInterface public const FEATURE_FLAG_PASSWORD_CHANGE_DISABLED = 'FEATURE_FLAG_PASSWORD_CHANGE_DISABLED'; public const FEATURE_FLAG_TAO_AS_A_TOOL = 'FEATURE_FLAG_TAO_AS_A_TOOL'; public const FEATURE_FLAG_SOLAR_DESIGN_ENABLED = 'FEATURE_FLAG_SOLAR_DESIGN_ENABLED'; - public const FEATURE_TRANSLATION_ENABLED = 'FEATURE_TRANSLATION_ENABLED'; public function isEnabled(string $feature): bool; } diff --git a/models/classes/form/Modifier/AbstractFormModifier.php b/models/classes/form/Modifier/AbstractFormModifier.php index 7b948d8ddc..752c39bf5e 100644 --- a/models/classes/form/Modifier/AbstractFormModifier.php +++ b/models/classes/form/Modifier/AbstractFormModifier.php @@ -22,27 +22,24 @@ namespace oat\tao\model\form\Modifier; -use core_kernel_classes_Resource; -use oat\generis\model\data\Ontology; use tao_helpers_form_Form; -abstract class AbstractFormModifier implements FormModifierInterface +abstract class AbstractFormModifier { - protected Ontology $ontology; + /** @var AbstractFormModifier[] */ + private array $modifiers = []; - public function __construct(Ontology $ontology) + public function addModifier(AbstractFormModifier $modifier): void { - $this->ontology = $ontology; + if (!in_array($modifier, $this->modifiers, true)) { + $this->modifiers[] = $modifier; + } } - protected function getInstance(tao_helpers_form_Form $form, array $options = []): ?core_kernel_classes_Resource + public function modify(tao_helpers_form_Form $form, array $options = []): void { - if (($options[self::OPTION_INSTANCE] ?? null) instanceof core_kernel_classes_Resource) { - return $options[self::OPTION_INSTANCE]; + foreach ($this->modifiers as $modifier) { + $modifier->modify($form, $options); } - - $instanceUri = $form->getValue(self::FORM_INSTANCE_URI); - - return $instanceUri ? $this->ontology->getResource($instanceUri) : null; } -} \ No newline at end of file +} diff --git a/models/classes/form/Modifier/FormModifierInterface.php b/models/classes/form/Modifier/FormModifierInterface.php deleted file mode 100644 index 88ff702d7d..0000000000 --- a/models/classes/form/Modifier/FormModifierInterface.php +++ /dev/null @@ -1,35 +0,0 @@ -extenders)) { - throw new InvalidArgumentException(sprintf('Form extender with id "%s" already exists.', $id)); - } - - $this->extenders[$id] = $extender; - } - - public function modify(Form $form, array $options = []): void - { - $ids = $options[self::OPTIONS_IDS] ?? []; - - $extenders = !empty($ids) - ? array_intersect_key($this->extenders, array_flip($ids)) - : $this->extenders; - - foreach ($extenders as $extender) { - if ($extender->supports($form, $options)) { - $extender->modify($form, $options); - } - } - } -} diff --git a/models/classes/form/Modifier/FormModifierServiceProvider.php b/models/classes/form/Modifier/FormModifierServiceProvider.php deleted file mode 100644 index 8a276b4dc2..0000000000 --- a/models/classes/form/Modifier/FormModifierServiceProvider.php +++ /dev/null @@ -1,38 +0,0 @@ -services(); - - $services - ->set(FormModifierManager::class, FormModifierManager::class) - ->public(); - } -}