Example #1
0
 /**
  * If a domainObject was renamed
  *
  * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject $oldDomainObject
  * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject $currentDomainObject
  * @return void
  */
 protected function mapOldControllerToCurrentClassObject(Model\DomainObject $oldDomainObject, Model\DomainObject $currentDomainObject)
 {
     $extensionDir = $this->previousExtensionDirectory;
     $newClassName = $currentDomainObject->getName() . 'Controller';
     $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(GeneralUtility::lcfirst($oldName) . 'Repository');
         $injectMethodName = 'inject' . $oldName . 'Repository';
         if ($currentDomainObject->isAggregateRoot()) {
             // update the initializeAction method body
             $initializeMethod = $this->classObject->getMethod('initializeAction');
             if ($initializeMethod != null) {
                 $initializeMethodBodyStmts = $initializeMethod->getBodyStmts();
                 $initializeMethodBodyStmts = str_replace(GeneralUtility::lcfirst($oldName) . 'Repository', GeneralUtility::lcfirst($newName) . 'Repository', $initializeMethodBodyStmts);
                 $initializeMethod->setBodyStmts($initializeMethodBodyStmts);
                 $this->classObject->setMethod($initializeMethod);
             }
             $injectMethod = $this->classObject->getMethod($injectMethodName);
             if ($injectMethod != null) {
                 $this->classObject->removeMethod($injectMethodName);
                 $initializeMethodBodyStmts = str_replace(GeneralUtility::lcfirst($oldName), GeneralUtility::lcfirst($newName), $injectMethod->getBodyStmts());
                 $injectMethod->setBodyStmts($initializeMethodBodyStmts);
                 $injectMethod->setTag('param', $currentDomainObject->getFullyQualifiedDomainRepositoryClassName() . ' $' . $newName . 'Repository');
                 $injectMethod->setName('inject' . $newName . 'Repository');
                 $parameter = new Model\ClassObject\MethodParameter(GeneralUtility::lcfirst($newName) . 'Repository');
                 $parameter->setTypeHint($currentDomainObject->getFullyQualifiedDomainRepositoryClassName());
                 $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->getBodyStmts();
                     $newActionMethodBody = str_replace(GeneralUtility::lcfirst($oldName) . 'Repository', GeneralUtility::lcfirst($newName) . 'Repository', $actionMethodBody);
                     $actionMethod->setBodyStmts($newActionMethodBody);
                     $actionMethod->setTag('param', $currentDomainObject->getQualifiedClassName());
                     $parameters = $actionMethod->getParameters();
                     foreach ($parameters as &$parameter) {
                         if (strpos($parameter->getTypeHint(), $oldDomainObject->getFullQualifiedClassName()) > -1) {
                             $parameter->setTypeHint($currentDomainObject->getFullQualifiedClassName());
                             $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->setBodyStmts($this->replaceUpperAndLowerCase($oldName, $newName, $actionMethod->getBodyStmts()));
                     $this->classObject->setMethod($actionMethod);
                 }
             }
         } else {
             $this->classObject->removeMethod('initializeAction');
             $this->classObject->removeMethod($injectMethodName);
             $this->cleanUp(FileGenerator::getFolderForClassFile($extensionDir, 'Repository'), $oldName . 'Repository.php');
         }
     }
     $this->classObject->setFileName($newName . 'Controller.php');
     $this->cleanUp(FileGenerator::getFolderForClassFile($extensionDir, 'Controller'), $oldName . 'Controller.php');
 }
Example #2
0
 /**
  * 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 \EBT\ExtensionBuilder\Domain\Model\DomainObject $domainObject
  * @param \EBT\ExtensionBuilder\Domain\Model\File $existingClassFileObject
  *
  * @return \EBT\ExtensionBuilder\Domain\Model\File
  */
 public function generateControllerClassFileObject($domainObject, $controllerClassTemplatePath, $existingClassFileObject = null)
 {
     $this->classObject = null;
     $className = $domainObject->getName() . 'Controller';
     $this->templateFileObject = $this->parserService->parseFile($controllerClassTemplatePath);
     $this->templateClassObject = $this->templateFileObject->getFirstClass();
     if ($existingClassFileObject) {
         $this->classFileObject = $existingClassFileObject;
         $this->classObject = $existingClassFileObject->getFirstClass();
         if ($this->classFileObject->getNamespace() === false) {
             $nameSpace = new \EBT\ExtensionBuilder\Domain\Model\NamespaceObject('dummy');
             $this->classFileObject->addNamespace($nameSpace);
         }
     }
     if ($this->classObject == null) {
         $this->classFileObject = clone $this->templateFileObject;
         $this->classObject = clone $this->templateFileObject->getFirstClass();
         $this->classObject->resetAll();
         $this->classObject->setName($className);
         $this->classObject->setDescription($className);
         if (isset($this->settings['Controller']['parentClass'])) {
             $parentClass = $this->settings['Controller']['parentClass'];
         } else {
             $parentClass = '\\TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController';
         }
         $this->classObject->setParentClassName($parentClass);
     }
     if ($domainObject->isAggregateRoot()) {
         $repositoryName = \TYPO3\CMS\Core\Utility\GeneralUtility::lcfirst($domainObject->getName() . 'Repository');
         // now add the property to class Object (or update an existing class Object property)
         if (!$this->classObject->propertyExists($repositoryName)) {
             $classProperty = $this->templateClassObject->getProperty('domainObjectRepository');
             $classProperty->setName($repositoryName);
             $classProperty->setDescription($repositoryName);
             $classProperty->setTag('var', $domainObject->getFullyQualifiedDomainRepositoryClassName(), true);
             $this->classObject->setProperty($classProperty);
         }
         if (!$this->classObject->getProperty($repositoryName)->isTaggedWith('inject') && !$this->classObject->methodExists('inject' . ucfirst($repositoryName))) {
             $this->classObject->getProperty($repositoryName)->setTag('inject');
         }
     }
     foreach ($domainObject->getActions() as $action) {
         $actionMethodName = $action->getName() . 'Action';
         if (!$this->classObject->methodExists($actionMethodName)) {
             $actionMethod = $this->buildActionMethod($action, $domainObject);
             $this->classObject->addMethod($actionMethod);
         }
     }
     $this->classFileObject->getNamespace()->setName($this->extension->getNamespaceName() . '\\Controller')->setClasses(array($this->classObject));
     return $this->classFileObject;
 }
 /**
  *
  * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject $currentDomainObject
  *
  * @return \EBT\ExtensionBuilder\Domain\Model\ClassObject\ClassObject|NULL
  */
 public function getRepositoryClassFile(Model\DomainObject $currentDomainObject)
 {
     $extensionDir = $this->previousExtensionDirectory;
     if (isset($this->previousDomainObjects[$currentDomainObject->getUniqueIdentifier()])) {
         $oldDomainObject = $this->previousDomainObjects[$currentDomainObject->getUniqueIdentifier()];
         $fileName = FileGenerator::getFolderForClassFile($extensionDir, 'Repository', FALSE);
         $fileName .= $oldDomainObject->getName() . 'Repository.php';
         if (file_exists($fileName)) {
             $this->classFileObject = $this->parserService->parseFile($fileName);
             $this->classObject = $this->classFileObject->getFirstClass();
             $this->classObject->setName($currentDomainObject->getName() . 'Repository');
             if ($oldDomainObject->getName() != $currentDomainObject->getName() || $this->extensionRenamed) {
                 $newClassName = $currentDomainObject->getFullyQualifiedDomainRepositoryClassName();
                 $this->classObject->setName($newClassName);
                 $this->cleanUp(FileGenerator::getFolderForClassFile($extensionDir, 'Repository'), $oldDomainObject->getName() . 'Repository.php');
             }
             return $this->classFileObject;
         } else {
             GeneralUtility::devLog('class file didn\'t exist:' . $fileName, 'extension_builder', 2);
         }
     } else {
         $fileName = FileGenerator::getFolderForClassFile($extensionDir, 'Repository', FALSE);
         $fileName .= $currentDomainObject->getName() . 'Repository.php';
         if (file_exists($fileName)) {
             $this->classFileObject = $this->parserService->parseFile($fileName);
             $this->classObject = $this->classFileObject->getFirstClass();
             $this->classObject->setFileName($fileName);
             $this->classObject->setFileName($fileName);
             $this->log('existing Repository class:' . $fileName, 0, (array) $this->classObject);
             return $this->classFileObject;
         }
     }
     $this->log('No existing Repository class:' . $currentDomainObject->getName(), 2);
     return NULL;
 }