Ejemplo n.º 1
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 boolean $mergeWithExistingClass
  *
  * @return \EBT\ExtensionBuilder\Domain\Model\File
  */
 public function generateControllerClassFileObject($domainObject, $controllerClassTemplatePath, $mergeWithExistingClass)
 {
     $this->classObject = NULL;
     $className = $domainObject->getName() . 'Controller';
     $this->templateFileObject = $this->parserService->parseFile($controllerClassTemplatePath);
     $this->templateClassObject = $this->templateFileObject->getFirstClass();
     if ($mergeWithExistingClass) {
         try {
             $this->classFileObject = $this->roundTripService->getControllerClassFile($domainObject);
             if (!is_null($this->classFileObject)) {
                 $this->classObject = $this->classFileObject->getFirstClass();
             }
         } catch (\Exception $e) {
             \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('Class ' . $className . ' could not be imported: ' . $e->getMessage(), 'extension_builder');
         }
     }
     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;
 }
Ejemplo n.º 2
0
 /**
  * Generates the code for the controller class
  * Either from ectionController template or from class partial
  *
  * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject $domainObject
  * @param bool $mergeWithExistingClass
  * @return string
  */
 public function generateActionControllerCode(\EBT\ExtensionBuilder\Domain\Model\DomainObject $domainObject)
 {
     $controllerTemplateFilePath = $this->codeTemplateRootPath . 'Classes/Controller/Controller.phpt';
     $existingClassFileObject = null;
     if ($this->roundTripEnabled) {
         $existingClassFileObject = $this->roundTripService->getControllerClassFile($domainObject);
     }
     $controllerClassFileObject = $this->classBuilder->generateControllerClassFileObject($domainObject, $controllerTemplateFilePath, $existingClassFileObject);
     // returns a class object if an existing class was found
     if ($controllerClassFileObject) {
         $this->addLicenseHeader($controllerClassFileObject->getFirstClass());
         return $this->printerService->renderFileObject($controllerClassFileObject, true);
     } else {
         throw new \Exception('Class file for controller could not be generated');
     }
 }