🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 19. 第三方类库 ## 引用第三方类库 将你的第三方类库直接放到插件根目录或者新建文件夹放好。 例如`demo`插件下新建`libaray`文件夹,之后把类库放入`library`文件夹。 ## 修改类库的命名空间 通过上面,类库放进来并不能调用到,我们需要增加或修改命名空间。 如下图,放入了两个类库文件,我们只需把命名空间改成:namespace addons\\插件名称\\library;,library里面还有文件夹就继续按目录层次写下去即可。 ![](https://img.kancloud.cn/9c/44/9c446dcbd8260b8801c637c8a2da6993_367x241.png) 根据插件命名空间调整,如下所示 ``` <pre class="calibre16">``` <span class="token"><</span><span class="token">?</span>php <span class="token5">// addons\插件名称\目录</span> namespace addons\demo\library<span class="token1">;</span> class <span class="token6">World</span> <span class="token1">{</span> public <span class="token2">function</span> <span class="token6">show</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> echo <span class="token3">1</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` ``` <pre class="calibre16">``` <span class="token"><</span><span class="token">?</span>php namespace addons\demo\library\oat<span class="token1">;</span> class <span class="token6">Lumoto</span> <span class="token1">{</span> <span class="token1">}</span> ``` ``` 改好后调用 ``` <pre class="calibre16">``` $w <span class="token">=</span> <span class="token2">new</span> <span class="token6"><span class="token1">\</span>addons<span class="token1">\</span>demo<span class="token1">\</span>library<span class="token1">\</span>World</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> $w<span class="token">-</span><span class="token">></span><span class="token6">show</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> ``` ``` ## 通过初始化事件自动注册 如果类库文件过多或者是一个composer包,修改起来麻烦,可参考如下代码,在插件名称、插件初始化时执行 > 2.2.1.20220603版本起支持 如下:在`addonsInitHook`事件中调用了`addNamespace`自动注册第三方类库方法 ``` <pre class="calibre16">``` <span class="token"><</span><span class="token">?</span>php declare <span class="token1">(</span>strict_types<span class="token">=</span><span class="token3">1</span><span class="token1">)</span><span class="token1">;</span> namespace addons\demo<span class="token1">;</span> use think\Addons<span class="token1">;</span> class <span class="token6">Demo</span> extends <span class="token6">Addons</span> <span class="token1">{</span> public <span class="token2">function</span> <span class="token6">install</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token3">true</span><span class="token1">;</span> <span class="token1">}</span> public <span class="token2">function</span> <span class="token6">uninstall</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token3">true</span><span class="token1">;</span> <span class="token1">}</span> public <span class="token2">function</span> <span class="token6">addonsInitHook</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $this<span class="token">-</span><span class="token">></span><span class="token6">addNamespace</span><span class="token1">(</span><span class="token4">'命名空间'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` 例如:以PhpWord类库为例,我们下载后的压缩包目录结构如下: ![](https://img.kancloud.cn/cb/6d/cb6d3551b578e8eb314ef433a1825496_303x296.png) 去到src目录,有如下文件夹 ![](https://img.kancloud.cn/cf/54/cf540228bf2bf50f431f4a66663a1712_199x57.png) 把PhpWord文件夹放入到addons\\demo\\library\\PhpOffice目录下,之所以还要创建PhpOffice目录因为他的命名空间是 ``` <pre class="calibre16">``` PhpOffice\PhpWord ``` ``` 所以我们需要在创建`PhpOffice`文件夹,完成这一步后在`addonsInitHook`事件中添加命名空间 ``` <pre class="calibre16">``` $this<span class="token">-</span><span class="token">></span><span class="token6">addNamespace</span><span class="token1">(</span><span class="token4">'PhpOffice\PhpWord'</span><span class="token1">)</span><span class="token1">;</span> ``` ``` 在插件里面或者其他地方使用类似于PhpOffice\\PhpWord空间的类库时都会在这加载。 ## composer 方式安装第三方类库 > 2.2.1.20220603版本起支持 使用composer方式之前你需要先了解什么是composer,链接地址:https://pkg.xyz/#how-to-install-composer 1. 分别建立vendor文件夹与composer.json文件 ![](https://img.kancloud.cn/e5/73/e5732dac0eb47d6a22062b80c8fd6a42_556x255.png) 2. composer.json内容 ``` <pre class="calibre16">``` <span class="token1">{</span> <span class="token4">"name"</span><span class="token1">:</span> <span class="token4">"hkcms/demo"</span><span class="token1">,</span> <span class="token4">"description"</span><span class="token1">:</span> <span class="token4">"demo plugin"</span><span class="token1">,</span> <span class="token4">"type"</span><span class="token1">:</span> <span class="token4">"hkcms-addon"</span><span class="token1">,</span> <span class="token4">"license"</span><span class="token1">:</span> <span class="token4">"apache2.0"</span><span class="token1">,</span> <span class="token4">"authors"</span><span class="token1">:</span> <span class="token1">[</span> <span class="token1">{</span> <span class="token4">"name"</span><span class="token1">:</span> <span class="token4">"demo"</span><span class="token1">,</span> <span class="token4">"email"</span><span class="token1">:</span> <span class="token4">"demo@hkcms.cn"</span> <span class="token1">}</span> <span class="token1">]</span><span class="token1">,</span> <span class="token4">"config"</span><span class="token1">:</span> <span class="token1">{</span> <span class="token1">}</span><span class="token1">,</span> <span class="token4">"require"</span><span class="token1">:</span> <span class="token1">{</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` `name`composer包的名字,格式为“vendor名称/包名” `description`描述信息,简单描述下你的插件 `type`固定为**hkcms-addon** `license`固定`apache2.0` `authors`作者,按上面的格式改掉即可 `` 3\. 跟composer一样在当前插件目录安装、更新、删除第三方库包。 例如安装第三方微信SDK ``` <pre class="calibre16">``` composer require overtrue<span class="token">/</span>wechat<span class="token1">:</span><span class="token">~</span><span class="token3">4.0</span> <span class="token">-</span>vvv ``` ``` 安装成功后即可使用。 > 注意,插件必须是安装后才能生效