[BUGFIX] Extbase validator should not ask class loader for simple types 41/36241/3
authorBenjamin Mack <benni@typo3.org>
Sat, 24 Jan 2015 22:27:57 +0000 (23:27 +0100)
committerAlexander Opitz <opitz.alexander@googlemail.com>
Mon, 26 Jan 2015 13:56:13 +0000 (14:56 +0100)
Extbase calls class_exists multiple times to
check for valid classes during set up of Validator
classes. It does these checks even for types
like "string" and "integer" for each
This leads to unnecessary calls to
the TYPO3 class loader and can be avoided.

Change-Id: I33f39b2902c1b04ca3c014ba44741976960cbf45
Resolves: #64379
Releases: master, 6.2
Reviewed-on: http://review.typo3.org/36241
Reviewed-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
Tested-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Alexander Opitz <opitz.alexander@googlemail.com>
Tested-by: Alexander Opitz <opitz.alexander@googlemail.com>
typo3/sysext/extbase/Classes/Validation/ValidatorResolver.php

index 39bdfd2..2d72236 100644 (file)
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Extbase\Validation;
 
 use TYPO3\CMS\Core\Utility\ClassNamingUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Utility\TypeHandlingUtility;
 use TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException;
 use TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator;
 
@@ -248,7 +249,9 @@ class ValidatorResolver implements \TYPO3\CMS\Core\SingletonInterface {
        protected function buildBaseValidatorConjunction($indexKey, $targetClassName, array $validationGroups = array()) {
                $conjunctionValidator = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator();
                $this->baseValidatorConjunctions[$indexKey] = $conjunctionValidator;
-               if (class_exists($targetClassName)) {
+
+               // note: the simpleType check reduces lookups to the class loader
+               if (!TypeHandlingUtility::isSimpleType($targetClassName) && class_exists($targetClassName)) {
                                // Model based validator
                        /** @var \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator $objectValidator */
                        $objectValidator = $this->objectManager->get('TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator', array());
@@ -259,18 +262,21 @@ class ValidatorResolver implements \TYPO3\CMS\Core\SingletonInterface {
                                        throw new \InvalidArgumentException(sprintf('There is no @var annotation for property "%s" in class "%s".', $classPropertyName, $targetClassName), 1363778104);
                                }
                                try {
-                                       $parsedType = \TYPO3\CMS\Extbase\Utility\TypeHandlingUtility::parseType(trim(implode('', $classPropertyTagsValues['var']), ' \\'));
+                                       $parsedType = TypeHandlingUtility::parseType(trim(implode('', $classPropertyTagsValues['var']), ' \\'));
                                } catch (\TYPO3\CMS\Extbase\Utility\Exception\InvalidTypeException $exception) {
                                        throw new \InvalidArgumentException(sprintf(' @var annotation of ' . $exception->getMessage(), 'class "' . $targetClassName . '", property "' . $classPropertyName . '"'), 1315564744, $exception);
                                }
                                $propertyTargetClassName = $parsedType['type'];
-                               if (\TYPO3\CMS\Extbase\Utility\TypeHandlingUtility::isCollectionType($propertyTargetClassName) === TRUE) {
-                                       $collectionValidator = $this->createValidator('TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator', array('elementType' => $parsedType['elementType'], 'validationGroups' => $validationGroups));
-                                       $objectValidator->addPropertyValidator($classPropertyName, $collectionValidator);
-                               } elseif (class_exists($propertyTargetClassName) && !\TYPO3\CMS\Extbase\Utility\TypeHandlingUtility::isCoreType($propertyTargetClassName) && $this->objectManager->isRegistered($propertyTargetClassName) && $this->objectManager->getScope($propertyTargetClassName) === \TYPO3\CMS\Extbase\Object\Container\Container::SCOPE_PROTOTYPE) {
-                                       $validatorForProperty = $this->getBaseValidatorConjunction($propertyTargetClassName, $validationGroups);
-                                       if (count($validatorForProperty) > 0) {
-                                               $objectValidator->addPropertyValidator($classPropertyName, $validatorForProperty);
+                               // note: the outer simpleType check reduces lookups to the class loader
+                               if (!TypeHandlingUtility::isSimpleType($propertyTargetClassName)) {
+                                       if (TypeHandlingUtility::isCollectionType($propertyTargetClassName)) {
+                                               $collectionValidator = $this->createValidator('TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator', array('elementType' => $parsedType['elementType'], 'validationGroups' => $validationGroups));
+                                               $objectValidator->addPropertyValidator($classPropertyName, $collectionValidator);
+                                       } elseif (class_exists($propertyTargetClassName) && !TypeHandlingUtility::isCoreType($propertyTargetClassName) && $this->objectManager->isRegistered($propertyTargetClassName) && $this->objectManager->getScope($propertyTargetClassName) === \TYPO3\CMS\Extbase\Object\Container\Container::SCOPE_PROTOTYPE) {
+                                               $validatorForProperty = $this->getBaseValidatorConjunction($propertyTargetClassName, $validationGroups);
+                                               if (count($validatorForProperty) > 0) {
+                                                       $objectValidator->addPropertyValidator($classPropertyName, $validatorForProperty);
+                                               }
                                        }
                                }