种族平等 发表于 2023-1-2 16:06:52

PHP Composer 虚拟依赖包 - 实现按需载入钉钉对应功能模块的 php sdk

难得钉钉官方提供了完整的 api sdk,各种语言的版本的都有,而且 api 覆盖面非常完整。但是,composer 安装下来有大几十兆,这个不符合个人的品味,我想要按需加载。
于是在构思了一两次以后,今天下午开始动手实现。
首要原则,不做手工劳动,人工分包,然后提交到 packagist 仓库,那样后续会有巨大的维护工作量。
基本思路:


[*]按固定规则在 package.json 中指定所需依赖的钉钉 sdk 的功能模块名称和版本名称,当然,它们在 packagist 仓库中肯定是不存在的;
[*]然后利用 composer 包管理工具的 hook 机制,在恰当的时机比如 install 或 update 实现拦截,匹配这种命名规则的包名,动态虚构相应的自定义“仓库”完成下载动作。
实现以后,可以用来解决很多类似场景的问题。
比如,虚构的依赖包名是这样的:
"require-dev": {
    "x3d/dingtalk-contact": "1.0"
}, x3d/dingtalk- 是前缀标识;contact 是钉钉的 api sdk 模块名;1.0 是这个模块的版本号。
这里涉及几个知识点:
1、钉钉sdk中的模块命名规则;
2、composer 的 hook 机制;
3、找到能从 github git 仓库中按目录检出代码的方式
4、虚构 composer 能支持的“仓库”类型
钉钉的 sdk 在 https://github.com/alibabacloud-sdk-php/dingtalk/tree/master/src 这个目录下,按功能模块+相应接口的版本号拼接的目录,目标是每次只按需下载某个模块的代码,并能在项目本地 被 composer 正常管理。

composer 的 hook 机制,在 官网 找到了简单的说明,但是具体是什么效果不清楚,整个下午花时间最多的就是在一个个的 hook 试,找感觉,看哪些能满足此次的需求。同时,要通过代码熟悉相应的知识点,文档是指望不上的。

最后确定了两种事件是可以的,pre-install-cmd、pre-update-cmd,其它细节可能要跟进使用情况进一步挖掘。
实现的代码比较简单,分两步,先分析出来 require 的虚构包列表,然后为他们添加虚构的代码仓库。
$devReqs = $this->composer->getPackage()->getDevRequires();
      foreach ($devReqs as $pkgName => $devReq) {
            if (substr($pkgName, 0, strlen($this->id)) != $this->id) {
                continue;
            }
            $dtk_mods[$pkgName] = $devReq->getPrettyConstraint();
      }        $packages = [];
      foreach ($dtk_mods as $module => $version) {
            $packages[] = $this->configByModule($module, $version);
      }

      $sdkMirror = [
            "type" => "package",
            "package" => $packages,
      ];

      $repoName = $this->repoName;
      $this->composer->getConfig()->merge([
            'repositories' => [
                $repoName => $sdkMirror,
            ],
      ]);

      $repoMgr = $this->composer->getRepositoryManager();
      $mirrorRepo = $repoMgr->createRepository('package', $sdkMirror, $repoName);
      $repoMgr->addRepository($mirrorRepo);
   

    /**
   * @paramstring$module 格式 x3d/dingtalk-service_group
   * @paramstring$version 格式 1.0
   * @return array
   */
    protected function configByModule(string $module, string $version)
    {
      // 下载对应的 dingtalk 结构代码,放到对应结构;
      $mod = substr($module, strlen($this->id) + 1);
      $dir = "{$mod}_" . str_replace('.', '_', $version);
      $package = [
            "name"=> $module,
            "version" => $version,
            "source" => [
                "url" => $this->mirrorUrl,
                "type" => "svn",
                "reference" => "trunk/src/$dir/"
            ],
            // "target-dir" => "src/",
            "autoload" => [
                "psr-4" => [
                  $this->nsPrefix . ucfirst($dir) . '\\' => "src/"
                ]
            ]
      ];

      return $package;
    } 由于国内 Gitee 的努力,很早就支持 svn 协议的兼容方案,而我们这种古早程序员从 cvs 和 svn 时代过来的,自然就联想到 转成 svn 就可以满足需求;因此,在Gitee 上建立一个 镜像仓库就,启用 svn 即可。
出现以下下面提示。其中第一个认证领域是用户的密码,这个可以留空。而用户名是用户在 Gitee 登陆时使用邮箱地址。密码则是用户登陆 Gitee 所使用的密码。

一般而言,svn 会加密缓存用户的用户名密码,所以,对仓库的操作只需要第一次输入用户邮箱和密码。 然后是看 composer 如何支持这种非标准的没有 package.json composer 协议配置文件的仓库,所幸,确实是支持的。
它支持一种 type 为 package 的仓库形式,里面指定协议为 svn 就可以进一步指定代码目录,这正是我们需要的。
{
    "repositories": [
      {
            "type": "package",
            "package": {
                "name": "smarty/smarty",
                "version": "3.1.7",
                "dist": {
                  "url": "https://www.smarty.net/files/Smarty-3.1.7.zip",
                  "type": "zip"
                },
                "source": {
                  "url": "http://smarty-php.googlecode.com/svn/",
                  "type": "svn",
                  "reference": "tags/Smarty_3_1_7/distribution/"
                },
                "autoload": {
                  "classmap": ["libs/"]
                }
            }
      }
    ],
    "require": {
      "smarty/smarty": "3.1.*"
    }
} 最后,在实操触发 svn 检出时,发现 svn 会报错,
could not be downloaded, Your configuration does not allow connections to svn://gitee.com/web3d/dingtalk-sdk-php/trunk/src/contact_1_0/.

See (https://getcomposer.org/doc/06-config.md#secure-svn-domains) for details. 乖乖的去这个页面上去找,果然有答案。
Defaults to [].
Lists domains which should be trusted/marked as using a secure Subversion/SVN transport.
By default svn:// protocol is seen as insecure and will throw, but you can set this config option to ["example.org"] to allow using svn URLs on that hostname.
This is a better/safer alternative to disabling secure-http altogether.
在 package.json 中加一条规则:
"config": {
    "secure-svn-domains": ["gitee.com"]
}, 再试,就可以了。

来源:https://www.cnblogs.com/x3d/p/php-composer-srcipts-repos-custom-for-dingtalk-php-sdk.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: PHP Composer 虚拟依赖包 - 实现按需载入钉钉对应功能模块的 php sdk