Example #1
0
 /**
  * Allways get object of current version by className.Ignore namespace!
  *
  * @param mixed $types
  * @param string $className
  * @param array $args
  * @throws Exception
  */
 function getObject($types, $className, $args = array())
 {
     if (!is_array($types)) {
         $types = array($types);
     }
     $typeList = array(self::TYPE_ACTION, self::TYPE_FIX);
     $version = ucfirst(str_replace('.', 'd', $this->version));
     $prefixNamespace = '\\Api';
     if (isset($this->module)) {
         $prefixNamespace .= '\\' . $this->module;
     }
     $dir = APP_PATH . '/models' . str_replace('\\', '/', $prefixNamespace);
     $versionList = File::scandirr($dir, 'dir', 1, array('\\.*'));
     // check types
     $typeDiff = array_diff($types, $typeList);
     if (!empty($typeDiff)) {
         $errorMsg = 'api types is invalid,types=' . implode(',', $typeDiff);
         throw new ApiException($errorMsg, ApiException::ERROR_METHOD);
     }
     $versionNeeded = true;
     if (empty($this->getVersion())) {
         $typeDiff = array_diff($versionList, $typeList);
         if (empty($typeDiff)) {
             $versionNeeded = false;
         }
     }
     if ($versionNeeded) {
         $dir .= '/' . $version;
         if (!is_dir($dir)) {
             $errorMsg = 'version not found, version=' . $this->version;
             throw new ApiException($errorMsg, ApiException::ERROR_VERSION);
         }
     }
     $fileName = str_replace('\\', '/', $className) . '.php';
     foreach ($types as $v) {
         $file = $dir . '/' . $v . '/' . $fileName;
         if (is_file($file)) {
             $apiName = $prefixNamespace . '\\' . $v;
             $apiName .= '\\' . $className . 'Model';
             $obj = $apiName::getInstanceOri($args);
             $obj->setParamAll($this->param);
             return $obj;
         }
     }
 }