/** * @test */ public function passingAnEmptyArrayToSetPropertiesRemovesAllExistingproperties() { $someProperties = array('prop1' => new \TYPO3\FLOW3\Object\Configuration\ConfigurationProperty('prop1', 'simple string'), 'prop2' => new \TYPO3\FLOW3\Object\Configuration\ConfigurationProperty('prop2', 'another string')); $this->objectConfiguration->setProperties($someProperties); $this->assertEquals($someProperties, $this->objectConfiguration->getProperties(), 'The set properties could not be retrieved again.'); $this->objectConfiguration->setProperties(array()); $this->assertEquals(array(), $this->objectConfiguration->getProperties(), 'The properties have not been cleared.'); }
/** * Builds the code necessary to inject setter based dependencies. * * @param \TYPO3\FLOW3\Object\Configuration\Configuration $objectConfiguration (needed to produce helpful exception message) * @return string The built code * @throws \TYPO3\FLOW3\Object\Exception\UnknownObjectException */ protected function buildPropertyInjectionCode(\TYPO3\FLOW3\Object\Configuration\Configuration $objectConfiguration) { $commands = array(); $className = $objectConfiguration->getClassName(); $objectName = $objectConfiguration->getObjectName(); foreach ($objectConfiguration->getProperties() as $propertyName => $propertyConfiguration) { if ($propertyConfiguration->getAutowiring() === \TYPO3\FLOW3\Object\Configuration\Configuration::AUTOWIRING_MODE_OFF) { continue; } $propertyValue = $propertyConfiguration->getValue(); switch ($propertyConfiguration->getType()) { case \TYPO3\FLOW3\Object\Configuration\ConfigurationProperty::PROPERTY_TYPES_OBJECT: if ($propertyValue instanceof \TYPO3\FLOW3\Object\Configuration\Configuration) { $propertyClassName = $propertyValue->getClassName(); if ($propertyClassName === NULL) { $preparedSetterArgument = $this->buildCustomFactoryCall($propertyValue->getFactoryObjectName(), $propertyValue->getFactoryMethodName(), $propertyValue->getArguments()); } else { if (!is_string($propertyClassName) || !isset($this->objectConfigurations[$propertyClassName])) { $configurationSource = $objectConfiguration->getConfigurationSourceHint(); throw new \TYPO3\FLOW3\Object\Exception\UnknownObjectException('Unknown class "' . $propertyClassName . '", specified as property "' . $propertyName . '" in the object configuration of object "' . $objectName . '" (' . $configurationSource . ').', 1296130876); } if ($this->objectConfigurations[$propertyClassName]->getScope() === \TYPO3\FLOW3\Object\Configuration\Configuration::SCOPE_PROTOTYPE) { $preparedSetterArgument = 'new \\' . $propertyClassName . '(' . $this->buildMethodParametersCode($propertyValue->getArguments()) . ')'; } else { $preparedSetterArgument = '\\TYPO3\\FLOW3\\Core\\Bootstrap::$staticObjectManager->get(\'' . $propertyClassName . '\')'; } } } else { if (strpos($propertyValue, '.') !== FALSE) { $settingPath = explode('.', $propertyValue); $settings = Arrays::getValueByPath($this->configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS), array_shift($settingPath)); $propertyValue = Arrays::getValueByPath($settings, $settingPath); } if (!isset($this->objectConfigurations[$propertyValue])) { $configurationSource = $objectConfiguration->getConfigurationSourceHint(); if ($propertyValue[0] === '\\') { throw new \TYPO3\FLOW3\Object\Exception\UnknownObjectException('The object name "' . $propertyValue . '" which was specified as a property in the object configuration of object "' . $objectName . '" (' . $configurationSource . ') starts with a leading backslash.', 1277827579); } else { throw new \TYPO3\FLOW3\Object\Exception\UnknownObjectException('The object "' . $propertyValue . '" which was specified as a property in the object configuration of object "' . $objectName . '" (' . $configurationSource . ') does not exist. Check for spelling mistakes and if that dependency is correctly configured.', 1265213849); } } $propertyClassName = $this->objectConfigurations[$propertyValue]->getClassName(); if ($this->objectConfigurations[$propertyValue]->getScope() === \TYPO3\FLOW3\Object\Configuration\Configuration::SCOPE_PROTOTYPE) { $preparedSetterArgument = 'new \\' . $propertyClassName . '(' . $this->buildMethodParametersCode($this->objectConfigurations[$propertyValue]->getArguments()) . ')'; } else { $preparedSetterArgument = '\\TYPO3\\FLOW3\\Core\\Bootstrap::$staticObjectManager->get(\'' . $propertyValue . '\')'; } } break; case \TYPO3\FLOW3\Object\Configuration\ConfigurationProperty::PROPERTY_TYPES_STRAIGHTVALUE: if (is_string($propertyValue)) { $preparedSetterArgument = '\'' . str_replace('\'', '\\\'', $propertyValue) . '\''; } else { $preparedSetterArgument = $propertyValue; } break; case \TYPO3\FLOW3\Object\Configuration\ConfigurationProperty::PROPERTY_TYPES_SETTING: $preparedSetterArgument = '\\TYPO3\\FLOW3\\Core\\Bootstrap::$staticObjectManager->get(\'TYPO3\\FLOW3\\Configuration\\ConfigurationManager\')->getConfiguration(\\TYPO3\\FLOW3\\Configuration\\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, \'' . $propertyValue . '\')'; break; } $setterMethodName = 'inject' . ucfirst($propertyName); if ($this->reflectionService->hasMethod($className, $setterMethodName)) { $commands[] = "\$this->{$setterMethodName}({$preparedSetterArgument});"; continue; } $setterMethodName = 'set' . ucfirst($propertyName); if ($this->reflectionService->hasMethod($className, $setterMethodName)) { $commands[] = "\$this->{$setterMethodName}({$preparedSetterArgument});"; continue; } if (property_exists($className, $propertyName)) { $commands[] = "\$this->{$propertyName} = {$preparedSetterArgument};"; } } return count($commands) > 0 ? "\t\t" . implode("\n\t\t", $commands) . "\n" : ''; }