Exemplo n.º 1
0
 /**
  * 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');
 }
 private function validateDomainObjectActions(Tx_ExtensionBuilder_Domain_Model_DomainObject $domainObject)
 {
     $actionNames = array();
     $actions = $domainObject->getActions();
     foreach ($actions as $action) {
         if (in_array($action->getName(), $actionNames)) {
             $this->validationResult['errors'][] = new Tx_ExtensionBuilder_Domain_Exception_ExtensionException('Duplicate action name "' . $action->getName() . '" of ' . $domainObject->getName() . '. Action names have to be unique for each model', self::ERROR_ACTIONNAME_DUPLICATE);
         }
         /**
          * Character test
          * Allowed characters are: a-z (lowercase), A-Z (uppercase) and 0-9
          */
         if (!preg_match("/^[a-zA-Z0-9]*\$/", $action->getName())) {
             $this->validationResult['errors'][] = new Tx_ExtensionBuilder_Domain_Exception_ExtensionException('Illegal action name "' . $action->getName() . '" of ' . $domainObject->getName() . '. Please use lowerCamelCase, no spaces or underscores.', self::ERROR_ACTIONNAME_ILLEGAL_CHARACTER);
         }
         $actionNames[] = $action->getName();
     }
     $this->validateDependentActions($actionNames, 'Domain object ' . $domainObject->getName());
     $firstAction = reset($actionNames);
     if ($firstAction == 'show' || $firstAction == 'edit' || $firstAction == 'delete') {
         $this->validationResult['warnings'][] = new Tx_ExtensionBuilder_Domain_Exception_ExtensionException('Potential misconfiguration in Domain object ' . $domainObject->getName() . ':<br />First action could not be default action since "' . $firstAction . '" action needs a parameter', self::ERROR_MISCONFIGURATION);
     }
 }