[TASK] Avoid ObjectManager for ext:form ProcessingRule
authorChristian Kuhn <lolli@schwarzbu.ch>
Thu, 10 Jun 2021 18:31:36 +0000 (20:31 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Thu, 10 Jun 2021 19:41:17 +0000 (21:41 +0200)
Inject a singleton service, makeInstance() prototypes.

Resolves: #94304
Related: #90803
Releases: master
Change-Id: I0645c06a5f5ae2b10f5e14aee08ed01f16720bac
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/69455
Tested-by: core-ci <typo3@b13.com>
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: Jochen <rothjochen@gmail.com>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Jochen <rothjochen@gmail.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/form/Classes/Domain/Model/FormDefinition.php
typo3/sysext/form/Classes/Domain/Model/Renderable/AbstractRenderable.php
typo3/sysext/form/Classes/Mvc/ProcessingRule.php
typo3/sysext/form/Configuration/Services.yaml
typo3/sysext/form/Tests/Unit/Mvc/ProcessingRuleTest.php

index fb411f8..48214ae 100644 (file)
@@ -23,6 +23,7 @@ namespace TYPO3\CMS\Form\Domain\Model;
 
 use Psr\Http\Message\ResponseInterface;
 use TYPO3\CMS\Core\Utility\ArrayUtility;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Mvc\Request;
 use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
 use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
@@ -666,7 +667,7 @@ class FormDefinition extends AbstractCompositeRenderable implements VariableRend
     public function getProcessingRule(string $propertyPath): ProcessingRule
     {
         if (!isset($this->processingRules[$propertyPath])) {
-            $this->processingRules[$propertyPath] = $this->objectManager->get(ProcessingRule::class);
+            $this->processingRules[$propertyPath] = GeneralUtility::makeInstance(ProcessingRule::class);
         }
         return $this->processingRules[$propertyPath];
     }
index f299eb0..aa3ec41 100644 (file)
@@ -467,8 +467,7 @@ abstract class AbstractRenderable implements RenderableInterface, VariableRender
         $identifier = $options['identifier'] ?? '';
         unset($options['identifier']);
 
-        $variant = GeneralUtility::makeInstance(ObjectManager::class)
-            ->get(RenderableVariant::class, $identifier, $options, $this);
+        $variant = GeneralUtility::makeInstance(RenderableVariant::class, $identifier, $options, $this);
 
         $this->addVariant($variant);
         return $variant;
index 2b2f7a0..b32aa3b 100644 (file)
@@ -41,59 +41,20 @@ class ProcessingRule
      */
     protected $dataType;
 
-    /**
-     * @var \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
-     */
-    protected $propertyMappingConfiguration;
-
-    /**
-     * @var \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator
-     */
-    protected $validator;
-
-    /**
-     * @var \TYPO3\CMS\Extbase\Error\Result
-     */
-    protected $processingMessages;
-
-    /**
-     * @var \TYPO3\CMS\Extbase\Property\PropertyMapper
-     */
-    protected $propertyMapper;
-
-    /**
-     * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration $propertyMappingConfiguration
-     * @internal
-     */
-    public function injectPropertyMappingConfiguration(PropertyMappingConfiguration $propertyMappingConfiguration)
-    {
-        $this->propertyMappingConfiguration = $propertyMappingConfiguration;
-    }
-
-    /**
-     * @param \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator $validator
-     * @internal
-     */
-    public function injectConjunctionValidator(ConjunctionValidator $validator)
-    {
-        $this->validator = $validator;
-    }
-
-    /**
-     * @param \TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper
-     * @internal
-     */
-    public function injectPropertyMapper(PropertyMapper $propertyMapper)
-    {
-        $this->propertyMapper = $propertyMapper;
-    }
+    protected PropertyMappingConfiguration $propertyMappingConfiguration;
+    protected ConjunctionValidator $validator;
+    protected Result $processingMessages;
+    protected PropertyMapper $propertyMapper;
 
     /**
      * Constructs this processing rule
      * @internal
      */
-    public function __construct()
+    public function __construct(PropertyMapper $propertyMapper)
     {
+        $this->propertyMapper = $propertyMapper;
+        $this->propertyMappingConfiguration = GeneralUtility::makeInstance(PropertyMappingConfiguration::class);
+        $this->validator = GeneralUtility::makeInstance(ConjunctionValidator::class);
         $this->processingMessages = GeneralUtility::makeInstance(Result::class);
     }
 
index fc35b0b..2c36abb 100644 (file)
@@ -19,6 +19,10 @@ services:
     alias: TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManager
     public: true
 
+  TYPO3\CMS\Form\Mvc\ProcessingRule:
+    public: true
+    shared: false
+
   TYPO3\CMS\Form\Slot\ResourcePublicationSlot:
     tags:
       - name: event.listener
index 3c5918b..1d6e8ba 100644 (file)
@@ -16,8 +16,7 @@
 namespace TYPO3\CMS\Form\Tests\Unit\Mvc;
 
 use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Extbase\Error\Result;
-use TYPO3\CMS\Extbase\Object\ObjectManager;
+use TYPO3\CMS\Extbase\Property\PropertyMapper;
 use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
 use TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator;
 use TYPO3\CMS\Form\Mvc\ProcessingRule;
@@ -29,28 +28,15 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
  */
 class ProcessingRuleTest extends UnitTestCase
 {
-    /**
-     * @var bool Reset singletons created by subject
-     */
-    protected $resetSingletonInstances = true;
-
-    protected function setUp(): void
-    {
-        parent::setUp();
-        $objectManagerProphecy = $this->prophesize(ObjectManager::class);
-        $objectManagerProphecy->get(Result::class)->willReturn(new Result());
-        GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManagerProphecy->reveal());
-    }
-
     /**
      * @test
      */
     public function addValidatorAddsValidator(): void
     {
-        $mockProcessingRule = new ProcessingRule();
-        $mockProcessingRule->injectConjunctionValidator(new ConjunctionValidator([]));
-        $mockProcessingRule->addValidator(new TestValidator());
-        $validators = $mockProcessingRule->getValidators();
+        GeneralUtility::addInstance(ConjunctionValidator::class, new ConjunctionValidator([]));
+        $subject = new ProcessingRule($this->prophesize(PropertyMapper::class)->reveal());
+        $subject->addValidator(new TestValidator());
+        $validators = $subject->getValidators();
         $validators->rewind();
         self::assertInstanceOf(AbstractValidator::class, $validators->current());
     }
@@ -60,9 +46,8 @@ class ProcessingRuleTest extends UnitTestCase
      */
     public function processNoPropertyMappingReturnsNotModifiedValue(): void
     {
-        $processingRule = new ProcessingRule();
-        $processingRule->injectConjunctionValidator(new ConjunctionValidator([]));
-
+        GeneralUtility::addInstance(ConjunctionValidator::class, new ConjunctionValidator([]));
+        $processingRule = new ProcessingRule($this->prophesize(PropertyMapper::class)->reveal());
         $input = 'someValue';
         self::assertSame($input, $processingRule->process($input));
     }
@@ -72,13 +57,11 @@ class ProcessingRuleTest extends UnitTestCase
      */
     public function processNoPropertyMappingAndHasErrorsIfValidatorContainsErrors(): void
     {
-        $processingRule = new ProcessingRule();
-        $processingRule->injectConjunctionValidator(new ConjunctionValidator([]));
+        GeneralUtility::addInstance(ConjunctionValidator::class, new ConjunctionValidator([]));
+        $processingRule = new ProcessingRule($this->prophesize(PropertyMapper::class)->reveal());
         $processingRule->addValidator(new TestValidator());
-
         $input = 'addError';
         $processingRule->process($input);
-
         self::assertTrue($processingRule->getProcessingMessages()->hasErrors());
     }
 }