Example #1
0
 /**
  * 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;
 }