eachImport()

in lib/generator.js [615:694]


  eachImport(imports, usedModels, filepath, innerModule, level) {
    this.imports = new Map();
    if (imports.length > 0) {
      const lockPath = path.join(this.config.pkgDir, '.libraries.json');
      const lock = fs.existsSync(lockPath) ? JSON.parse(fs.readFileSync(lockPath, 'utf8')) : {};
      for (let i = 0; i < imports.length; i++) {
        const item = imports[i];
        let comments = DSL.comment.getFrontComments(this.comments, item.tokenRange[0]);
        this.visitComments(comments, level);
        const aliasId = item.lexeme;
        const main = item.mainModule;
        const inner = item.module;
        const moduleDir = main ? this.config.libraries[main] : this.config.libraries[aliasId];
        const innerPath = item.innerPath;
        if (!moduleDir && innerPath) {
          let phpPath = innerPath.replace(/(\.tea)$|(\.spec)$|(\.dara)$/gi, '');
          if (phpPath.startsWith('./') || phpPath.startsWith('../')) {
            phpPath = phpPath.split('/').map(dir => _upperFirst(dir)).join(path.sep);
            phpPath = path.join(path.dirname(filepath), `${phpPath}.php`);
          } else if (phpPath.startsWith('/')) {
            phpPath = phpPath.split('/').map(dir => _upperFirst(dir)).join(path.sep);
            phpPath = `${phpPath}.php`;
          }
          
          const classNamespace = this.getClassNamespace(phpPath);
          const className = this.getInnerClient(aliasId, phpPath);

          innerModule.set(aliasId, path.join(path.dirname(phpPath), `${className}.php`));
          this.moduleClass.set(aliasId, {
            namespace: classNamespace,
            className: className,
            aliasName: this.getAliasName(classNamespace, className, aliasId),
            modelDir: this.modelDir,
            exceptionDir: this.exceptionDir,
          });
          continue;
        }
        let targetPath = '';
        if (moduleDir.startsWith('./') || moduleDir.startsWith('../')) {
          targetPath = path.join(this.config.pkgDir, moduleDir);
        } else if (moduleDir.startsWith('/')) {
          targetPath = moduleDir;
        } else {
          if(!lock[moduleDir]) {
            throw new Error(`The '${aliasId}' is not import in Darafile.`);
          }
          targetPath = path.join(this.config.pkgDir, lock[moduleDir]);
        }
        const pkgPath = fs.existsSync(path.join(targetPath, 'Teafile')) ? path.join(targetPath, 'Teafile') : path.join(targetPath, 'Darafile');
        const pkg = JSON.parse(fs.readFileSync(pkgPath));
        const phpRelease = pkg.releases && pkg.releases.php;
        const phpPkg = pkg.php;
        if (!phpRelease || !phpPkg || !phpPkg.package) {
          throw new Error(`The '${aliasId}' has no PHP supported.`);
        }
        let classNamespace = phpPkg.package.split('.').join('\\');
        let className = phpPkg.clientName || 'Client';
        if (inner && pkg.exports[inner]) {
          let phpPath = path.dirname(pkg.exports[inner]);
          const arr = phpPath.split(path.sep).slice(1);
          arr.map(key => {
            classNamespace += '\\' + _upperFirst(key);
            
          });
          className = phpPkg.exports[inner];
        }
        const [ key ,value ] = phpRelease.split(':');
        this.requires[key] = value;
        this.moduleClass.set(aliasId, {
          namespace: classNamespace,
          className: className,
          aliasName: this.getAliasName(classNamespace, className, aliasId),
          modelDir: phpPkg.modelDirName || 'Models',
          exceptionDir: phpPkg.exceptionDirName || 'Exceptions',
        });
        this.moduleTypedef[aliasId] = phpPkg.typedef;
      }
      this.__externModule = usedModels;
    }
  }