/** * Checks if the specified method matches against the method name * expression. * * Returns TRUE if method name, visibility and arguments constraints match and the target * method is not final. * * @param string $className Ignored in this pointcut filter * @param string $methodName Name of the method to match against * @param string $methodDeclaringClassName Name of the class the method was originally declared in * @param mixed $pointcutQueryIdentifier Some identifier for this query - must at least differ from a previous identifier. Used for circular reference detection. * @return boolean TRUE if the class matches, otherwise FALSE * @throws \TYPO3\FLOW3\Aop\Exception */ public function matches($className, $methodName, $methodDeclaringClassName, $pointcutQueryIdentifier) { $matchResult = preg_match('/^' . $this->methodNameFilterExpression . '$/', $methodName); if ($matchResult === FALSE) { throw new \TYPO3\FLOW3\Aop\Exception('Error in regular expression', 1168876915); } elseif ($matchResult !== 1) { return FALSE; } switch ($this->methodVisibility) { case 'public': if (!($methodDeclaringClassName !== NULL && $this->reflectionService->isMethodPublic($methodDeclaringClassName, $methodName))) { return FALSE; } break; case 'protected': if (!($methodDeclaringClassName !== NULL && $this->reflectionService->isMethodProtected($methodDeclaringClassName, $methodName))) { return FALSE; } break; } if ($methodDeclaringClassName !== NULL && $this->reflectionService->isMethodFinal($methodDeclaringClassName, $methodName)) { return FALSE; } $methodArguments = $methodDeclaringClassName === NULL ? array() : $this->reflectionService->getMethodParameters($methodDeclaringClassName, $methodName); foreach (array_keys($this->methodArgumentConstraints) as $argumentName) { $objectAccess = explode('.', $argumentName, 2); $argumentName = $objectAccess[0]; if (!array_key_exists($argumentName, $methodArguments)) { $this->systemLogger->log('The argument "' . $argumentName . '" declared in pointcut does not exist in method ' . $methodDeclaringClassName . '->' . $methodName, LOG_NOTICE); return FALSE; } } return TRUE; }
/** * Takes an array of unparsed command line arguments and options and converts it separated * by named arguments, options and unnamed arguments. * * @param array $rawCommandLineArguments The unparsed command parts (such as "--foo") as an array * @param string $controllerObjectName Object name of the designated command controller * @param string $controllerCommandName Command name of the recognized command (ie. method name without "Command" suffix) * @return array All and exceeding command line arguments * @throws \TYPO3\FLOW3\Mvc\Exception\InvalidArgumentMixingException */ protected function parseRawCommandLineArguments(array $rawCommandLineArguments, $controllerObjectName, $controllerCommandName) { $commandLineArguments = array(); $exceedingArguments = array(); $commandMethodName = $controllerCommandName . 'Command'; $commandMethodParameters = $this->reflectionService->getMethodParameters($controllerObjectName, $commandMethodName); $requiredArguments = array(); $optionalArguments = array(); $argumentNames = array(); foreach ($commandMethodParameters as $parameterName => $parameterInfo) { $argumentNames[] = $parameterName; if ($parameterInfo['optional'] === FALSE) { $requiredArguments[strtolower($parameterName)] = array('parameterName' => $parameterName, 'type' => $parameterInfo['type']); } else { $optionalArguments[strtolower($parameterName)] = array('parameterName' => $parameterName, 'type' => $parameterInfo['type']); } } $decidedToUseNamedArguments = FALSE; $decidedToUseUnnamedArguments = FALSE; $argumentIndex = 0; while (count($rawCommandLineArguments) > 0) { $rawArgument = array_shift($rawCommandLineArguments); if ($rawArgument[0] === '-') { if ($rawArgument[1] === '-') { $rawArgument = substr($rawArgument, 2); } else { $rawArgument = substr($rawArgument, 1); } $argumentName = $this->extractArgumentNameFromCommandLinePart($rawArgument); if (isset($optionalArguments[$argumentName])) { $argumentValue = $this->getValueOfCurrentCommandLineOption($rawArgument, $rawCommandLineArguments, $optionalArguments[$argumentName]['type']); $commandLineArguments[$optionalArguments[$argumentName]['parameterName']] = $argumentValue; } elseif (isset($requiredArguments[$argumentName])) { if ($decidedToUseUnnamedArguments) { throw new \TYPO3\FLOW3\Mvc\Exception\InvalidArgumentMixingException(sprintf('Unexpected named argument "%s". If you use unnamed arguments, all required arguments must be passed without a name.', $argumentName), 1309971821); } $decidedToUseNamedArguments = TRUE; $argumentValue = $this->getValueOfCurrentCommandLineOption($rawArgument, $rawCommandLineArguments, $requiredArguments[$argumentName]['type']); $commandLineArguments[$requiredArguments[$argumentName]['parameterName']] = $argumentValue; unset($requiredArguments[$argumentName]); } } else { if (count($requiredArguments) > 0) { if ($decidedToUseNamedArguments) { throw new \TYPO3\FLOW3\Mvc\Exception\InvalidArgumentMixingException(sprintf('Unexpected unnamed argument "%s". If you use named arguments, all required arguments must be passed named.', $rawArgument), 1309971820); } $argument = array_shift($requiredArguments); $commandLineArguments[$argument['parameterName']] = $rawArgument; $decidedToUseUnnamedArguments = TRUE; } else { $exceedingArguments[] = $rawArgument; } } $argumentIndex++; } return array($commandLineArguments, $exceedingArguments); }
/** * This function tries to find yet unmatched dependencies which need to be injected via "inject*" setter methods. * * @param array &$objectConfigurations * @return void * @throws \TYPO3\FLOW3\Object\Exception if an injected property is private */ protected function autowireProperties(array &$objectConfigurations) { foreach ($objectConfigurations as $objectConfiguration) { $className = $objectConfiguration->getClassName(); $properties = $objectConfiguration->getProperties(); foreach (get_class_methods($className) as $methodName) { if (substr($methodName, 0, 6) === 'inject') { $propertyName = strtolower(substr($methodName, 6, 1)) . substr($methodName, 7); $autowiringAnnotation = $this->reflectionService->getMethodAnnotation($className, $methodName, 'TYPO3\\FLOW3\\Annotations\\Autowiring'); if ($autowiringAnnotation !== NULL && $autowiringAnnotation->enabled === FALSE) { continue; } if ($methodName === 'injectSettings') { $packageKey = $objectConfiguration->getPackageKey(); if ($packageKey !== NULL) { $properties[$propertyName] = new ConfigurationProperty($propertyName, $packageKey, ConfigurationProperty::PROPERTY_TYPES_SETTING); } } else { if (array_key_exists($propertyName, $properties)) { continue; } $methodParameters = $this->reflectionService->getMethodParameters($className, $methodName); if (count($methodParameters) !== 1) { $this->systemLogger->log(sprintf('Could not autowire property %s because %s() expects %s instead of exactly 1 parameter.', "{$className}::{$propertyName}", $methodName, count($methodParameters) ?: 'none'), LOG_DEBUG); continue; } $methodParameter = array_pop($methodParameters); if ($methodParameter['class'] === NULL) { $this->systemLogger->log(sprintf('Could not autowire property %s because the method parameter in %s() contained no class type hint.', "{$className}::{$propertyName}", $methodName), LOG_DEBUG); continue; } $properties[$propertyName] = new ConfigurationProperty($propertyName, $methodParameter['class'], ConfigurationProperty::PROPERTY_TYPES_OBJECT); } } } foreach ($this->reflectionService->getPropertyNamesByAnnotation($className, 'TYPO3\\FLOW3\\Annotations\\Inject') as $propertyName) { if ($this->reflectionService->isPropertyPrivate($className, $propertyName)) { $exceptionMessage = 'The property "' . $propertyName . '" in class "' . $className . '" must not be private when annotated for injection.'; throw new \TYPO3\FLOW3\Object\Exception($exceptionMessage, 1328109641); } if (!array_key_exists($propertyName, $properties)) { $objectName = trim(implode('', $this->reflectionService->getPropertyTagValues($className, $propertyName, 'var')), ' \\'); $properties[$propertyName] = new ConfigurationProperty($propertyName, $objectName, ConfigurationProperty::PROPERTY_TYPES_OBJECT); } } $objectConfiguration->setProperties($properties); } }
/** * Generates the parameters code needed to call the constructor with the saved parameters. * * @param string $className Name of the class the method is declared in * @return string The generated parameters code */ protected function buildSavedConstructorParametersCode($className) { if ($className === NULL) { return ''; } $parametersCode = ''; $methodParameters = $this->reflectionService->getMethodParameters($className, '__construct'); $methodParametersCount = count($methodParameters); if ($methodParametersCount > 0) { foreach ($methodParameters as $methodParameterName => $methodParameterInfo) { $methodParametersCount--; $parametersCode .= '$this->FLOW3_Aop_Proxy_originalConstructorArguments[\'' . $methodParameterName . '\']' . ($methodParametersCount > 0 ? ', ' : ''); } } return $parametersCode; }
/** * Initializes the arguments array of this controller by creating an empty argument object for each of the * method arguments found in the designated command method. * * @return void * @throws \TYPO3\FLOW3\Mvc\Exception\InvalidArgumentTypeException */ protected function initializeCommandMethodArguments() { $methodParameters = $this->reflectionService->getMethodParameters(get_class($this), $this->commandMethodName); foreach ($methodParameters as $parameterName => $parameterInfo) { $dataType = NULL; if (isset($parameterInfo['type'])) { $dataType = $parameterInfo['type']; } elseif ($parameterInfo['array']) { $dataType = 'array'; } if ($dataType === NULL) { throw new \TYPO3\FLOW3\Mvc\Exception\InvalidArgumentTypeException('The argument type for parameter $' . $parameterName . ' of method ' . get_class($this) . '->' . $this->commandMethodName . '() could not be detected.', 1306755296); } $defaultValue = isset($parameterInfo['defaultValue']) ? $parameterInfo['defaultValue'] : NULL; $this->arguments->addNewArgument($parameterName, $dataType, $parameterInfo['optional'] === FALSE, $defaultValue); } }
/** * Detects and registers any validators for arguments: * - by the data type specified in the param annotations * - additional validators specified in the validate annotations of a method * * @param string $className * @param string $methodName * @return array An Array of ValidatorConjunctions for each method parameters. * @throws Exception\InvalidValidationConfigurationException * @throws Exception\NoSuchValidatorException * @throws Exception\InvalidTypeHintException */ public function buildMethodArgumentsValidatorConjunctions($className, $methodName) { $validatorConjunctions = array(); $methodParameters = $this->reflectionService->getMethodParameters($className, $methodName); if (count($methodParameters) === 0) { return $validatorConjunctions; } foreach ($methodParameters as $parameterName => $methodParameter) { $validatorConjunction = $this->createValidator('TYPO3\\FLOW3\\Validation\\Validator\\ConjunctionValidator'); if (!array_key_exists('type', $methodParameter)) { throw new \TYPO3\FLOW3\Validation\Exception\InvalidTypeHintException('Missing type information, probably no @param annotation for parameter "$' . $parameterName . '" in ' . $className . '->' . $methodName . '()', 1281962564); } if (strpos($methodParameter['type'], '\\') === FALSE) { $typeValidator = $this->createValidator($methodParameter['type']); } elseif (strpos($methodParameter['type'], '\\Model\\') !== FALSE) { $possibleValidatorClassName = str_replace('\\Model\\', '\\Validator\\', $methodParameter['type']) . 'Validator'; $typeValidator = $this->createValidator($possibleValidatorClassName); } else { $typeValidator = NULL; } if ($typeValidator !== NULL) { $validatorConjunction->addValidator($typeValidator); } $validatorConjunctions[$parameterName] = $validatorConjunction; } $validateAnnotations = $this->reflectionService->getMethodAnnotations($className, $methodName, 'TYPO3\\FLOW3\\Annotations\\Validate'); foreach ($validateAnnotations as $validateAnnotation) { $newValidator = $this->createValidator($validateAnnotation->type, $validateAnnotation->options); if ($newValidator === NULL) { throw new \TYPO3\FLOW3\Validation\Exception\NoSuchValidatorException('Invalid validate annotation in ' . $className . '->' . $methodName . '(): Could not resolve class name for validator "' . $validateAnnotation->type . '".', 1239853109); } if (isset($validatorConjunctions[$validateAnnotation->argumentName])) { $validatorConjunctions[$validateAnnotation->argumentName]->addValidator($newValidator); } elseif (strpos($validateAnnotation->argumentName, '.') !== FALSE) { $objectPath = explode('.', $validateAnnotation->argumentName); $argumentName = array_shift($objectPath); $validatorConjunctions[$argumentName]->addValidator($this->buildSubObjectValidator($objectPath, $newValidator)); } else { throw new \TYPO3\FLOW3\Validation\Exception\InvalidValidationConfigurationException('Invalid validate annotation in ' . $className . '->' . $methodName . '(): Validator specified for argument name "' . $validateAnnotation->argumentName . '", but this argument does not exist.', 1253172726); } } return $validatorConjunctions; }
/** * Builds the PHP code for the parameters of the specified method to be * used in a method interceptor in the proxy class * * @param string $fullClassName Name of the class the method is declared in * @param string $methodName Name of the method to create the parameters code for * @param boolean $addTypeAndDefaultValue If the type and default value for each parameters should be rendered * @return string A comma speparated list of parameters */ public function buildMethodParametersCode($fullClassName, $methodName, $addTypeAndDefaultValue = TRUE) { $methodParametersCode = ''; $methodParameterTypeName = ''; $defaultValue = ''; $byReferenceSign = ''; if ($fullClassName === NULL || $methodName === NULL) { return ''; } $methodParameters = $this->reflectionService->getMethodParameters($fullClassName, $methodName); if (count($methodParameters) > 0) { $methodParametersCount = 0; foreach ($methodParameters as $methodParameterName => $methodParameterInfo) { if ($addTypeAndDefaultValue) { if ($methodParameterInfo['array'] === TRUE) { $methodParameterTypeName = 'array'; } else { $methodParameterTypeName = $methodParameterInfo['class'] === NULL ? '' : '\\' . $methodParameterInfo['class']; } if ($methodParameterInfo['optional'] === TRUE) { $rawDefaultValue = isset($methodParameterInfo['defaultValue']) ? $methodParameterInfo['defaultValue'] : NULL; if ($rawDefaultValue === NULL) { $defaultValue = ' = NULL'; } elseif (is_bool($rawDefaultValue)) { $defaultValue = $rawDefaultValue ? ' = TRUE' : ' = FALSE'; } elseif (is_numeric($rawDefaultValue)) { $defaultValue = ' = ' . $rawDefaultValue; } elseif (is_string($rawDefaultValue)) { $defaultValue = " = '" . $rawDefaultValue . "'"; } elseif (is_array($rawDefaultValue)) { $defaultValue = " = " . $this->buildArraySetupCode($rawDefaultValue); } } $byReferenceSign = $methodParameterInfo['byReference'] ? '&' : ''; } $methodParametersCode .= ($methodParametersCount > 0 ? ', ' : '') . ($methodParameterTypeName ? $methodParameterTypeName . ' ' : '') . $byReferenceSign . '$' . $methodParameterName . $defaultValue; $methodParametersCount++; } } return $methodParametersCode; }
/** * Returns an array of \TYPO3\FLOW3\Cli\CommandArgumentDefinition that contains * information about required/optional arguments of this command. * If the command does not expect any arguments, an empty array is returned * * @return array<\TYPO3\FLOW3\Cli\CommandArgumentDefinition> */ public function getArgumentDefinitions() { if (!$this->hasArguments()) { return array(); } $commandArgumentDefinitions = array(); $commandMethodReflection = $this->getCommandMethodReflection(); $annotations = $commandMethodReflection->getTagsValues(); $commandParameters = $this->reflectionService->getMethodParameters($this->controllerClassName, $this->controllerCommandName . 'Command'); $i = 0; foreach ($commandParameters as $commandParameterName => $commandParameterDefinition) { $explodedAnnotation = explode(' ', $annotations['param'][$i]); array_shift($explodedAnnotation); array_shift($explodedAnnotation); $description = implode(' ', $explodedAnnotation); $required = $commandParameterDefinition['optional'] !== TRUE; $commandArgumentDefinitions[] = new CommandArgumentDefinition($commandParameterName, $required, $description); $i++; } return $commandArgumentDefinitions; }
/** * Builds a new instance of $objectType with the given $possibleConstructorArgumentValues. * If constructor argument values are missing from the given array the method looks for a * default value in the constructor signature. * * Furthermore, the constructor arguments are removed from $possibleConstructorArgumentValues * * @param array &$possibleConstructorArgumentValues * @param string $objectType * @return object The created instance * @throws \TYPO3\FLOW3\Property\Exception\InvalidTargetException if a required constructor argument is missing */ protected function buildObject(array &$possibleConstructorArgumentValues, $objectType) { $className = $this->objectManager->getClassNameByObjectName($objectType); if ($this->reflectionService->hasMethod($className, '__construct')) { $constructorSignature = $this->reflectionService->getMethodParameters($className, '__construct'); $constructorArguments = array(); foreach ($constructorSignature as $constructorArgumentName => $constructorArgumentInformation) { if (array_key_exists($constructorArgumentName, $possibleConstructorArgumentValues)) { $constructorArguments[] = $possibleConstructorArgumentValues[$constructorArgumentName]; unset($possibleConstructorArgumentValues[$constructorArgumentName]); } elseif ($constructorArgumentInformation['optional'] === TRUE) { $constructorArguments[] = $constructorArgumentInformation['defaultValue']; } else { throw new \TYPO3\FLOW3\Property\Exception\InvalidTargetException('Missing constructor argument "' . $constructorArgumentName . '" for object of type "' . $objectType . '".', 1268734872); } } $classReflection = new \ReflectionClass($className); return $classReflection->newInstanceArgs($constructorArguments); } else { return new $className(); } }
/** * Renders additional code for the __construct() method of the Proxy Class which realizes constructor injection. * * @param \TYPO3\FLOW3\Object\Configuration\Configuration $objectConfiguration * @return string The built code * @throws \TYPO3\FLOW3\Object\Exception\UnknownObjectException */ protected function buildConstructorInjectionCode(\TYPO3\FLOW3\Object\Configuration\Configuration $objectConfiguration) { $assignments = array(); $argumentConfigurations = $objectConfiguration->getArguments(); $constructorParameterInfo = $this->reflectionService->getMethodParameters($objectConfiguration->getClassName(), '__construct'); $argumentNumberToOptionalInfo = array(); foreach ($constructorParameterInfo as $parameterInfo) { $argumentNumberToOptionalInfo[$parameterInfo['position'] + 1] = $parameterInfo['optional']; } foreach ($argumentConfigurations as $argumentNumber => $argumentConfiguration) { if ($argumentConfiguration === NULL) { continue; } $argumentValue = $argumentConfiguration->getValue(); $assignmentPrologue = 'if (!isset($arguments[' . ($argumentNumber - 1) . '])) $arguments[' . ($argumentNumber - 1) . '] = '; if ($argumentValue !== NULL) { switch ($argumentConfiguration->getType()) { case \TYPO3\FLOW3\Object\Configuration\ConfigurationArgument::ARGUMENT_TYPES_OBJECT: if ($argumentValue instanceof \TYPO3\FLOW3\Object\Configuration\Configuration) { $argumentValueObjectName = $argumentValue->getObjectName(); if ($this->objectConfigurations[$argumentValueObjectName]->getScope() === \TYPO3\FLOW3\Object\Configuration\Configuration::SCOPE_PROTOTYPE) { $assignments[] = $assignmentPrologue . 'new \\' . $argumentValueObjectName . '(' . $this->buildMethodParametersCode($argumentValue->getArguments()) . ')'; } else { $assignments[] = $assignmentPrologue . '\\TYPO3\\FLOW3\\Core\\Bootstrap::$staticObjectManager->get(\'' . $argumentValueObjectName . '\')'; } } else { if (strpos($argumentValue, '.') !== FALSE) { $settingPath = explode('.', $argumentValue); $settings = Arrays::getValueByPath($this->configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS), array_shift($settingPath)); $argumentValue = Arrays::getValueByPath($settings, $settingPath); } if (!isset($this->objectConfigurations[$argumentValue])) { throw new \TYPO3\FLOW3\Object\Exception\UnknownObjectException('The object "' . $argumentValue . '" which was specified as an argument in the object configuration of object "' . $objectConfiguration->getObjectName() . '" does not exist.', 1264669967); } $assignments[] = $assignmentPrologue . '\\TYPO3\\FLOW3\\Core\\Bootstrap::$staticObjectManager->get(\'' . $argumentValue . '\')'; } break; case \TYPO3\FLOW3\Object\Configuration\ConfigurationArgument::ARGUMENT_TYPES_STRAIGHTVALUE: $assignments[] = $assignmentPrologue . var_export($argumentValue, TRUE); break; case \TYPO3\FLOW3\Object\Configuration\ConfigurationArgument::ARGUMENT_TYPES_SETTING: $assignments[] = $assignmentPrologue . '\\TYPO3\\FLOW3\\Core\\Bootstrap::$staticObjectManager->getSettingsByPath(explode(\'.\', \'' . $argumentValue . '\'))'; break; } } else { if (isset($argumentNumberToOptionalInfo[$argumentNumber]) && $argumentNumberToOptionalInfo[$argumentNumber] === TRUE) { $assignments[] = $assignmentPrologue . 'NULL'; } } } $code = count($assignments) > 0 ? "\n\t\t" . implode(";\n\t\t", $assignments) . ";\n" : ''; $index = 0; foreach ($constructorParameterInfo as $parameterName => $parameterInfo) { if ($parameterInfo['optional'] === TRUE) { break; } if ($objectConfiguration->getScope() === \TYPO3\FLOW3\Object\Configuration\Configuration::SCOPE_SINGLETON) { $code .= ' if (!isset($arguments[' . $index . '])) throw new \\TYPO3\\FLOW3\\Object\\Exception\\UnresolvedDependenciesException(\'Missing required constructor argument $' . $parameterName . ' in class \' . __CLASS__ . \'. ' . 'Please check your calling code and Dependency Injection configuration.\', 1296143787);' . "\n"; } else { $code .= ' if (!isset($arguments[' . $index . '])) throw new \\TYPO3\\FLOW3\\Object\\Exception\\UnresolvedDependenciesException(\'Missing required constructor argument $' . $parameterName . ' in class \' . __CLASS__ . \'. ' . 'Note that constructor injection is only support for objects of scope singleton (and this is not a singleton) – for other scopes you must pass each required argument to the constructor yourself.\', 1296143788);' . "\n"; } $index++; } return $code; }