基于 Laravel Route 的 ThinkSNS+ Component【研发日记三】

2017-05-09 (4062)

亲爱的粉丝,授权客户,企业创业者们,这一次,我们将为你们打造最好用的社交软件系统。在这里你会看到TSer们技术实施相关内容的播报,可评论留言提出您的问题与我们互动。

这里是传送门

《ThinkSNS+ 基于 Laravel master 分支,从 1 到 0,再到 0.1【ThinkSNS+研发日记系列一】》

《基于 Laravel 开发 ThinkSNS+ 中前端的抉择(webpack/Vue)踩坑日记【ThinkSNS+研发日记系列二】》


在前面,我介绍了拓展类型,分别有 plus-compnent 和 plus-plugin 两个,这里重点讲以下如何实现 plus-component 的。

plus-component 是什么

就如同名字一样,plus 代表的是 ThinlSNS+ 程序,用 - 分割 后面的 component 就是「包」或者我们理解成应用。在这里的「应用」指的是通过实现 API 或者 web 的功能。所以产生了这个类型。

但是 plus-component 不只是应用,也可以是简单的功能拓展,例如medz/plus-storage-quniu就是拓展的七牛云储存。

composer 插件的建立

既然涉及到路由,最开始的想法,其实是 /routes 目录下生成文件,包的路由文件复制到这里来。后来,发现了问题不足。
最后想到,plus-component 的实现,不一定是基于路由的应用,也有可能是简单的拓展。我们看下中间插件的接口类:

<?php
namespace Zhiyi\Component\Installer\PlusInstallPlugin;
use Closure;
use Illuminate\Console\Command;
use Illuminate\Console\OutputStyle;
interface InstallerInterface{
    public function setCommand(Command $command, OutputStyle $output);

    /**
     * Get the component info.
     *
     * @return void|\Zhiyi\Component\Installer\PlusInstallPlugin\ComponentInfoInterface
     *
     * @author Seven Du <shiweidu@outlook.com>
     * @homepage http://medz.cn
     */
    public function getComponentInfo();

    /**
     * 应用安装.
     *
     * @param Closure $next
     *
     * @author Seven Du <shiweidu@outlook.com>
     * @homepage http://medz.cn
     */
    public function install(Closure $next);

    /**
     * 应用升级.
     *
     * @param Closure $next
     *
     * @author Seven Du <shiweidu@outlook.com>
     * @homepage http://medz.cn
     */
    public function update(Closure $next);

    /**
     * 应用卸载.
     *
     * @param Closure $next
     *
     * @author Seven Du <shiweidu@outlook.com>
     * @homepage http://medz.cn
     */
    public function uninstall(Closure $next);

    /**
     * 静态资源.
     *
     * @return string 静态资源目录
     *
     * @author Seven Du <shiweidu@outlook.com>
     * @homepage http://medz.cn
     */
    public function resource();

    /**
     * 路由配置.
     *
     * @return string 路由配置文件列表
     *
     * @author Seven Du <shiweidu@outlook.com>
     * @homepage http://medz.cn
     */
    public function router();
    }

其中 router 成了非必需项。

转而,拥有了三个 hook 方法 install、update 和 uninstall 方法,这三个分别对应的是安装,升级,卸载。
而设计中,plus-component 中间插件会在 Laravel 的 /config/component.php 中增加如下配置:

'medz/plus-component-example' => 
  array (
    'installed' => false,
    'installer' => 'Medz\\Component\\ZhiyiPlus\\PlusComponentExample\\Installer\\Installer',
  ),

中间插件的 composer.json 配置

其实很简单,看到上面添加到 /config/component.php 的代码了, installer 项哪里来的呢?看下 包的配置:

{
    "name": "medz/plus-component-example",
    "type": "plus-component",
    "require": {
        "zhiyicx/plus-install-plugin": "^1.1"
    },
    "autoload": {
        "psr-4": {
                    "Medz\\Component\\ZhiyiPlus\\PlusComponentExample\\": "src/"
                }
    },
    "extra": {
        "installer-class": "Medz\\Component\\ZhiyiPlus\\PlusComponentExample\\Installer\\Installer"
    }
}

就是最后的 extra.installer-class 配置的,这里是完整的 class name,这样,在 composer 插件执行的时候读取这个额外的配置,并写入到 Laravel 的配置文件中。

install/update/uninstall

在 ThinkSNS+ 中有 php artisan component [install|update|unstall] vendor/name 这样一个命令,主要是用作 包的安装,升级,卸载。
实际运行如下:

php artisan component install medz/plus-component-example

通过这样的方式安装包,而这个命令会读取 /config/component.php 的配置,从而得到 installer ,这样,在运行不同的参数的时候后,调用 install,uodate,uninstall 等 需求 hook 达到目的。

router

在最上面的接口类中你也看到了,有一个 router 方法,这个方法返回类型有两个 void|string,所以, void 代表没有路由,string 就表示包路由文件的绝对地址。

在 php artisan component 命令执行的时候,对应的添加 /config/component_routes.php 里面的配置。
在 /app/Providers/RouteServiceProvider.php 中如下:

protected function mapVendorRoutes()
    {
        $files = config('component_routes', []);
        foreach ($files as $file) {
            include_once $file;
        }
    }

可能你会误会,为什么只是 include 进来呢?是的,这里的代码其实是参照 Route::group 来的,而在包里面的文件,可以正常的使用 Route::* 进行路由配置。

resource

既然可以基于路由,那就必然会存在静态资源的问题,在接口类中也有这样的规定:

 /**
     * 静态资源.
     *
     * @return string 静态资源目录
     *
     * @author Seven Du <shiweidu@outlook.com>
     * @homepage http://medz.cn
     */
    public function resource();

这里返回在包中静态资源存储的目录,执行安装或者升级命令的时候复制到 /public/vendor/name 目录下来达到静态资源发布的功能。


更高级的封装

这里其实是只模式封装,在 ThinkSNS+ 的 php artisan component 其实还有一个 --link 参数,做什么用的?其实不难理解,就是吧静态资源由原来的复制变成创建软链。这在开发过程中很有用。

下期预告:下一篇文章,会简单的讲以下 ThinkSNS+ 自封装的命令实现。


源码授权购买

现行稳定系统V4系列全端产品(PC / H5 / APP)授权源码购买,产品体验,请致电:18108035545

开源版源码官方正版获取唯一渠道:关注公众号“thinksns”,回复“开源版”即可获得最新源码压缩文件地址。


开源代码仓库:

GitHub:https://github.com/zhiyicx/thinksns-plus(点击星,每日关注开发动态。)

咨询QQ:3298713109

官网:http://www.thinksns.com/

内测申请方式

  提供个人/企业联系方式及认证信息(实名ID /企业营业执照照片或扫描件)及申请说明,发送邮件至lihecong@zhishisoft.com将有机会获得首批内测资格,名额有限,申请从速。

  参与内测请提供以下资料

  1个可接收验证码的手机号

  1个指定邮箱

  1位测试人员姓名

  1个您喜欢的账号昵称

开源不易,为了争取开源,我们团队做了很多努力。把基于Laravel的作品展示在大家面前,之后专栏会持续不断的分享ThinkSNS +开发过程中的技术细节。

能看见这里的,一定是真爱~谢谢你们的爱❤❤❤❤❤

qq咨询
QQ:3515923610

咨询热线

19934627031
微信咨询
微信咨询
首页 免费电话 立即咨询