예제 #1
0
 /**
  * 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');
         }
     }
 }
예제 #2
0
 /**
  * This method generates the class schema object, which is passed to the template
  * it keeps all methods and properties including user modified method bodies and comments
  * needed to create a domain object class file
  *
  * @param Tx_ExtensionBuilder_Domain_Model_DomainObject $domainObject
  * @param boolean mergeWithExistingClass
  *
  * @return Tx_ExtensionBuilder_Domain_Model_Class_Class
  */
 public function generateModelClassObject($domainObject, $mergeWithExistingClass)
 {
     t3lib_div::devlog('------------------------------------- generateModelClassObject(' . $domainObject->getName() . ') ---------------------------------', 'extension_builder', 0);
     $this->classObject = NULL;
     // reference to the resulting class file,
     $className = $domainObject->getClassName();
     if ($mergeWithExistingClass) {
         try {
             $this->classObject = $this->roundTripService->getDomainModelClass($domainObject);
         } catch (Exception $e) {
             t3lib_div::devLog('Class ' . $className . ' could not be imported: ' . $e->getMessage(), 'extension_builder', 2);
         }
     }
     if ($this->classObject == NULL) {
         $this->classObject = new Tx_ExtensionBuilder_Domain_Model_Class_Class($className);
         if ($domainObject->isEntity()) {
             $parentClass = $domainObject->getParentClass();
             if (empty($parentClass)) {
                 $parentClass = $this->configurationManager->getParentClassForEntityObject($this->extension->getExtensionKey());
             }
         } else {
             $parentClass = $this->configurationManager->getParentClassForValueObject($this->extension->getExtensionKey());
         }
         $this->classObject->setParentClass($parentClass);
     }
     if (!$this->classObject->hasDescription()) {
         $this->classObject->setDescription($domainObject->getDescription());
     }
     $this->addInitStorageObjectCalls($domainObject);
     //TODO the following part still needs some enhancement:
     //what should be obligatory in existing properties and methods
     foreach ($domainObject->getProperties() as $domainProperty) {
         $propertyName = $domainProperty->getName();
         // add the property to class Object (or update an existing class Object property)
         if ($this->classObject->propertyExists($propertyName)) {
             $classProperty = $this->classObject->getProperty($propertyName);
             //$classPropertyTags = $classProperty->getTags();
             //t3lib_div::devLog('Property found: ' . $propertyName . ':' . $domainProperty->getTypeForComment(), 'extension_builder', 1, (array)$classProperty);
         } else {
             $classProperty = new Tx_ExtensionBuilder_Domain_Model_Class_Property($propertyName);
             $classProperty->setTag('var', $domainProperty->getTypeForComment());
             $classProperty->addModifier('protected');
             //t3lib_div::devLog('New property: ' . $propertyName . ':' . $domainProperty->getTypeForComment(), 'extension_builder', 1);
         }
         $classProperty->setAssociatedDomainObjectProperty($domainProperty);
         if ($domainProperty->getRequired()) {
             if (!$classProperty->isTaggedWith('validate')) {
                 $validateTag = explode(' ', trim($domainProperty->getValidateAnnotation()));
                 $classProperty->setTag('validate', $validateTag[1]);
             }
         }
         if ($domainProperty->isRelation() && $domainProperty->getLazyLoading()) {
             if (!$classProperty->isTaggedWith('lazy')) {
                 $classProperty->setTag('lazy', '');
             }
         }
         if ($domainProperty->getHasDefaultValue()) {
             $classProperty->setDefault($domainProperty->getDefaultValue());
         }
         $this->classObject->setProperty($classProperty);
         if ($domainProperty->isNew()) {
             $this->setPropertyRelatedMethods($domainProperty);
         }
     }
     //t3lib_div::devlog('Methods before sorting','extension_builder',0,array_keys($this->classObject->getMethods()));
     //$this->sortMethods($domainObject);
     return $this->classObject;
 }