/** * adder for parameters * @param array $parameters of type Tx_ExtensionBuilder_Reflection_ParameterReflection * @return void */ public function setParameters($parameters) { foreach ($parameters as $parameter) { $methodParameter = new Tx_ExtensionBuilder_Domain_Model_Class_MethodParameter($parameter->getName(), $parameter); $this->parameters[$methodParameter->getPosition()] = $methodParameter; } }
/** * If a domainObject was renamed * * @param Tx_ExtensionBuilder_Domain_Model_DomainObject $oldDomainObject * @param Tx_ExtensionBuilder_Domain_Model_DomainObject $currentDomainObject * @return void */ protected function mapOldControllerToCurrentClassObject(Tx_ExtensionBuilder_Domain_Model_DomainObject $oldDomainObject, Tx_ExtensionBuilder_Domain_Model_DomainObject $currentDomainObject) { $extensionDir = $this->previousExtensionDirectory; $newClassName = $currentDomainObject->getControllerName(); $newName = $currentDomainObject->getName(); $oldName = $oldDomainObject->getName(); $this->classObject->setName($newClassName); $this->classObject->setDescription($this->replaceUpperAndLowerCase($oldName, $newName, $this->classObject->getDescription())); if ($oldDomainObject->isAggregateRoot()) { // should we keep the old properties comments and tags? $this->classObject->removeProperty(t3lib_div::lcfirst($oldName) . 'Repository'); $injectMethodName = 'inject' . $oldName . 'Repository'; if ($currentDomainObject->isAggregateRoot()) { // update the initializeAction method body $initializeMethod = $this->classObject->getMethod('initializeAction'); if ($initializeMethod != NULL) { $initializeMethodBody = $initializeMethod->getBody(); $newInitializeMethodBody = str_replace($oldDomainObject->getDomainRepositoryClassName(), $currentDomainObject->getDomainRepositoryClassName(), $initializeMethodBody); $newInitializeMethodBody = str_replace(t3lib_div::lcfirst($oldName) . 'Repository', t3lib_div::lcfirst($newName) . 'Repository', $newInitializeMethodBody); $initializeMethod->setBody($newInitializeMethodBody); $this->classObject->setMethod($initializeMethod); } $injectMethod = $this->classObject->getMethod($injectMethodName); if ($injectMethod != NULL) { $this->classObject->removeMethod($injectMethodName); $newInjectMethodBody = str_replace(t3lib_div::lcfirst($oldName), t3lib_div::lcfirst($newName), $injectMethod->getBody()); $injectMethod->setBody($newInjectMethodBody); $injectMethod->setTag('param', $currentDomainObject->getDomainRepositoryClassName() . ' $' . $newName . 'Repository'); $injectMethod->setName('inject' . $newName . 'Repository'); $parameter = new Tx_ExtensionBuilder_Domain_Model_Class_MethodParameter(t3lib_div::lcfirst($newName) . 'Repository'); $parameter->setTypeHint($currentDomainObject->getDomainRepositoryClassName()); $parameter->setPosition(0); $injectMethod->replaceParameter($parameter); $this->classObject->setMethod($injectMethod); } foreach ($oldDomainObject->getActions() as $action) { // here we have to update all the occurences of domain object names in action methods $actionMethod = $this->classObject->getMethod($action->getName() . 'Action'); if ($actionMethod != NULL) { $actionMethodBody = $actionMethod->getBody(); $newActionMethodBody = str_replace(t3lib_div::lcfirst($oldName) . 'Repository', t3lib_div::lcfirst($newName) . 'Repository', $actionMethodBody); $actionMethod->setBody($newActionMethodBody); $actionMethod->setTag('param', $currentDomainObject->getClassName()); $parameters = $actionMethod->getParameters(); foreach ($parameters as &$parameter) { if (strpos($parameter->getTypeHint(), $oldDomainObject->getClassName()) > -1) { $parameter->setTypeHint($currentDomainObject->getClassName()); $parameter->setName($this->replaceUpperAndLowerCase($oldName, $newName, $parameter->getName())); $actionMethod->replaceParameter($parameter); } } $tags = $actionMethod->getTags(); foreach ($tags as $tagName => $tagValue) { $tags[$tagName] = $this->replaceUpperAndLowerCase($oldName, $newName, $tagValue); } $actionMethod->setTags($tags); $actionMethod->setDescription($this->replaceUpperAndLowerCase($oldName, $newName, $actionMethod->getDescription())); //TODO: this is not safe. Could rename unwanted variables $actionMethod->setBody($this->replaceUpperAndLowerCase($oldName, $newName, $actionMethod->getBody())); $this->classObject->setMethod($actionMethod); } } } else { $this->classObject->removeMethod('initializeAction'); $this->classObject->removeMethod($injectMethodName); $this->cleanUp(Tx_ExtensionBuilder_Service_CodeGenerator::getFolderForClassFile($extensionDir, 'Repository'), $oldName . 'Repository.php'); } } $this->classObject->setFileName($newName . 'Controller.php'); $this->cleanUp(Tx_ExtensionBuilder_Service_CodeGenerator::getFolderForClassFile($extensionDir, 'Controller'), $oldName . 'Controller.php'); }
/** * This method generates the class object, which is passed to the template * it keeps all methods and properties including user modified method bodies and * comments that are required to create a controller class file * * @param Tx_ExtensionBuilder_Domain_Model_DomainObject $domainObject * @param boolean $mergeWithExistingClass * * @return Tx_ExtensionBuilder_Domain_Model_Class_Class */ public function generateControllerClassObject($domainObject, $mergeWithExistingClass) { t3lib_div::devlog('------------------------------------- generateControllerClassObject(' . $domainObject->getName() . ') ---------------------------------', 'extension_builder', 1); $this->classObject = NULL; $className = $domainObject->getControllerName(); if ($mergeWithExistingClass) { try { $this->classObject = $this->roundTripService->getControllerClass($domainObject); } catch (Exception $e) { t3lib_div::devLog('Class ' . $className . ' could not be imported: ' . $e->getMessage(), 'extension_builder'); } } if ($this->classObject == NULL) { $this->classObject = new Tx_ExtensionBuilder_Domain_Model_Class_Class($className); if (isset($this->settings['Controller']['parentClass'])) { $parentClass = $this->settings['Controller']['parentClass']; } else { $parentClass = 'Tx_Extbase_MVC_Controller_ActionController'; } $this->classObject->setParentClass($parentClass); } if ($domainObject->isAggregateRoot()) { $propertyName = t3lib_div::lcfirst($domainObject->getName()) . 'Repository'; // now add the property to class Object (or update an existing class Object property) if (!$this->classObject->propertyExists($propertyName)) { $classProperty = new Tx_ExtensionBuilder_Domain_Model_Class_Property($propertyName); $classProperty->setTag('var', $domainObject->getDomainRepositoryClassName()); $classProperty->addModifier('protected'); $this->classObject->setProperty($classProperty); } $injectMethodName = 'inject' . $domainObject->getName() . 'Repository'; if (!$this->classObject->methodExists($injectMethodName)) { $repositoryVarName = t3lib_div::lcfirst($domainObject->getName()) . 'Repository'; $injectMethod = new Tx_ExtensionBuilder_Domain_Model_Class_Method($injectMethodName); $injectMethod->setBody('$this->' . $repositoryVarName . ' = $' . $repositoryVarName . ';'); $injectMethod->setTag('param', $domainObject->getDomainRepositoryClassName() . ' $' . $repositoryVarName); $injectMethod->setTag('return', 'void'); $injectMethod->addModifier('public'); $parameter = new Tx_ExtensionBuilder_Domain_Model_Class_MethodParameter($repositoryVarName); $parameter->setVarType($domainObject->getDomainRepositoryClassName()); $parameter->setTypeHint($domainObject->getDomainRepositoryClassName()); $parameter->setPosition(0); $injectMethod->setParameter($parameter); $this->classObject->addMethod($injectMethod); } } foreach ($domainObject->getActions() as $action) { $actionMethodName = $action->getName() . 'Action'; if (!$this->classObject->methodExists($actionMethodName)) { $actionMethod = $this->buildActionMethod($action, $domainObject); $this->classObject->addMethod($actionMethod); } } return $this->classObject; }