Пример #1
0
 /**
  * Contructor of class
  *
  * @param array $xmlArr
  * @param string $childClassName
  * @param object $parentObj
  * @return void
  */
 public function __construct(&$xmlArr, $childClassName, $parentObj = null)
 {
     $this->parentObj = $parentObj;
     if (!$xmlArr) {
         return;
     }
     if (isset($xmlArr["ATTRIBUTES"])) {
         $className = isset($xmlArr["ATTRIBUTES"]['CLASS']) ? $xmlArr["ATTRIBUTES"]['CLASS'] : $childClassName;
         if ((bool) strpos($className, ".")) {
             $a_package_name = explode(".", $className);
             $className = array_pop($a_package_name);
             $clsLoaded = ClassLoader::loadMetadataClass($className, implode(".", $a_package_name));
         }
         //if (!$clsLoaded) trigger_error("Cannot find the load class $className", E_USER_ERROR);
         $className = Openbizx::objectFactory()->getClassNameFromAlias($className);
         $obj = new $className($xmlArr, $parentObj);
         $this->varValue[$obj->objectName] = $obj;
     } else {
         foreach ($xmlArr as $child) {
             $className = isset($child["ATTRIBUTES"]['CLASS']) ? $child["ATTRIBUTES"]['CLASS'] : $childClassName;
             /**
              * If a '.' is found within className we need to require such class
              * and then get the className after the last dot
              * ex. shared.dataobjs.FieldName, in this case FieldName is the class, shared/dataobjs the path
              *
              * The best solution to this is enable object factory to specify its resulting object constructor parameters
              */
             if ($className) {
                 //bug fixed by jixian for resolve load an empty classname
                 if ((bool) strpos($className, ".")) {
                     $a_package_name = explode(".", $className);
                     $className = array_pop($a_package_name);
                     $clsLoaded = ClassLoader::loadMetadataClass($className, implode(".", $a_package_name));
                 } elseif ($parentObj->package) {
                     $clsLoaded = ClassLoader::loadMetadataClass($className, $parentObj->package);
                 }
                 $className = Openbizx::objectFactory()->getClassNameFromAlias($className);
                 $obj = new $className($child, $parentObj);
                 $this->varValue[$obj->objectName] = $obj;
             }
         }
     }
 }
Пример #2
0
 /**
  * Construct an instance of an object
  *
  * @param string $objName object name
  * @param array $xmlArr xml array
  * @return object the instance of the object
  */
 protected function constructObject($objName, &$xmlArr = null)
 {
     if (!$xmlArr) {
         $xmlFile = ObjectFactoryHelper::getXmlFileWithPath($objName);
         if ($xmlFile) {
             $xmlArr = ObjectFactoryHelper::getXmlArray($xmlFile);
         } else {
             //$this->_createObjectFromClass($objName);
             /* 
              * if not have metadata, create object from class directly
              * metadata is on the class it self.                 * 
              */
             $class = str_replace('.', '\\', $objName);
             /*
             $dotPos = strrpos($objName, ".");
             if ($dotPos > 0) { // if has package/namespace
                 $objectPackage =  substr($objName, 0, $dotPos) ;
                 $class = substr($objName, $dotPos + 1);
             } else {
                 $objectPackage = null;
                 $class = $objName;
             }
             * 
             */
         }
     }
     if ($xmlArr) {
         $keys = array_keys($xmlArr);
         $root = $keys[0];
         // add by mr_a_ton , atrubut name must match with object name
         $dotPos = strrpos($objName, ".");
         $shortObjectName = $dotPos > 0 ? substr($objName, $dotPos + 1) : $objName;
         if ($xmlArr[$root]["ATTRIBUTES"]["NAME"] == "") {
             $xmlArr[$root]["ATTRIBUTES"]["NAME"] = $shortObjectName;
         } else {
             if ($shortObjectName != $xmlArr[$root]["ATTRIBUTES"]["NAME"]) {
                 trigger_error("Metadata file parsing error for object {$objName}. Name attribut [" . $xmlArr[$root]["ATTRIBUTES"]["NAME"] . "] not same with object name. Please double check your metadata xml file again.", E_USER_ERROR);
             }
         }
         $class = $xmlArr[$root]["ATTRIBUTES"]["CLASS"];
         //echo __METHOD__.'-'.__LINE__ . ' class : ' . $class . ' <br />';
         // if class has package name as prefix, change the package to the prefix
         $dotPos = strrpos($class, ".");
         if ($dotPos > 0) {
             $classPrefix = substr($class, 0, $dotPos);
             $classPackage = $classPrefix;
             $shortClass = substr($class, $dotPos + 1);
         } else {
             $classPrefix = null;
             $classPackage = null;
             $shortClass = $class;
         }
         //echo 'shortclass : ' . $shortClass .'<br />' ;
         // set object package
         $dotPos = strrpos($objName, ".");
         $objectPackage = $dotPos > 0 ? substr($objName, 0, $dotPos) : null;
         if (strpos($objectPackage, '@') === 0) {
             $objectPackage = substr($objectPackage, 1);
         }
         if (!$classPackage) {
             $classPackage = $objectPackage;
         }
         $xmlArr[$root]["ATTRIBUTES"]["PACKAGE"] = $objectPackage;
     }
     //$package = $xmlArr[$root]["ATTRIBUTES"]["PACKAGE"];
     //$class = $xmlArr[$root]["ATTRIBUTES"]["CLASS"];
     $class = $shortClass;
     // echo $class . '<br />';
     $class = $this->getClassNameFromAlias($class);
     if (strrpos($class, '\\') !== false) {
         $obj_ref = new $class($xmlArr);
         return $obj_ref;
     }
     //echo $class . '<br />';
     //echo $classPackage . '<br />';
     if (!class_exists($class, false)) {
         //echo 'class not exist<br />';
         $classFile = ClassLoader::getLibFileWithPath($class, $classPackage);
         //echo 'classFile: '.$classFile .'<br />';
         if (!$classFile) {
             if ($objectPackage) {
                 trigger_error("Cannot find the class with name as {$objectPackage}.{$class}", E_USER_ERROR);
             } else {
                 trigger_error("Cannot find the class with name as {$class} of {$objName}", E_USER_ERROR);
             }
             exit;
         }
         include_once $classFile;
     }
     //echo 'class_exists($class, false): '. (class_exists($class, false)? 'true':'false') . '<br />';
     if (class_exists($class, false)) {
         //if ($objName == "collab.calendar.form.EventListForm") { print_r($xmlArr); exit; }
         $obj_ref = new $class($xmlArr);
         if ($obj_ref) {
             return $obj_ref;
         }
     } else {
         trigger_error("Cannot find the class with name as {$class} in {$classFile}", E_USER_ERROR);
     }
     return null;
 }