/**
  * 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;
 }
 /**
  * Adds one (or multiple) properties found in a source code line to the classObject
  *
  * @param array $propertyMatches as returned from preg_match_all
  */
 protected function addProperty(array $propertyMatches, $startLine = NULL)
 {
     $properties = array_combine($propertyMatches['name'], $propertyMatches['value']);
     $isFirstProperty = TRUE;
     foreach ($properties as $propertyName => $propertyValue) {
         try {
             // the property has to exist in the classReflection
             $reflectionProperty = $this->classReflection->getProperty($propertyName);
             if ($reflectionProperty) {
                 $classProperty = new Tx_ExtensionBuilder_Domain_Model_Class_Property($propertyName);
                 $classProperty->mapToReflectionProperty($reflectionProperty);
                 // get the default value from regex matches
                 if (!empty($propertyValue)) {
                     if (strlen($classProperty->getVarType()) < 1) {
                         // try to detect the varType from default value
                         if (strpos($propertyValue, 'array') > -1) {
                             $varType = 'array';
                         } else {
                             eval('$varType = gettype(' . $propertyValue . ');');
                         }
                         if (!empty($varType) && $varType != 'NULL') {
                             $classProperty->setVarType($varType);
                         }
                     }
                     $classProperty->setValue(trim($propertyValue));
                     $classProperty->setDefault(TRUE);
                 }
                 if ($isFirstProperty) {
                     // only the first property will get the preceding block assigned
                     $precedingBlock = $this->concatLinesFromArray($this->lines, $this->lastMatchedLineNumber, $startLine, FALSE);
                     $classProperty->setPrecedingBlock($precedingBlock);
                     $isFirstProperty = FALSE;
                 }
                 $this->classObject->addProperty($classProperty);
                 $this->lastMatchedLineNumber = $this->lineCount;
             } else {
                 throw new Tx_ExtensionBuilder_Exception_ParseError(' Property ' . $propertyName . ' does not exist. Parsed from line ' . $this->lineCount . 'in ' . $this->classReflection->getFileName());
             }
         } catch (ReflectionException $e) {
             // ReflectionClass throws an exception if a property was not found
             t3lib_div::devlog('Exception in line : ' . $e->getMessage() . 'Property ' . $propertyName . ' found in line ' . $this->lineCount, 'extension_builder');
         }
     }
 }
Beispiel #3
0
 /**
  * add a property (returns TRUE if successfull added)
  *
  * @param Tx_ExtensionBuilder_Domain_Model_Class_Property
  * @return boolean success
  */
 public function addProperty(Tx_ExtensionBuilder_Domain_Model_Class_Property $classProperty)
 {
     if (!$this->propertyExists($classProperty->getName())) {
         $this->propertyNames[] = $classProperty->getName();
         $this->properties[$classProperty->getName()] = $classProperty;
     } else {
         return FALSE;
     }
 }