catchAdmin/extend/catcher/CatchAdminService.php

163 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2019-12-02 23:04:43 +08:00
<?php
2020-02-20 13:44:54 +08:00
namespace catcher;
2019-12-02 23:04:43 +08:00
2020-06-24 09:11:48 +08:00
use catcher\event\LoadModuleRoutes;
2020-02-02 22:10:52 +08:00
use think\exception\Handle;
2019-12-07 17:31:38 +08:00
use think\facade\Validate;
2019-12-02 23:04:43 +08:00
use think\Service;
class CatchAdminService extends Service
{
/**
*
* @time 2019年11月29日
* @return void
*/
public function boot()
2020-06-24 09:11:48 +08:00
{}
2020-02-01 15:42:13 +08:00
/**
* register
*
* @author JaguarJack
* @email njphper@gmail.com
* @time 2020/1/30
* @return void
*/
public function register()
2019-12-02 23:04:43 +08:00
{
2019-12-13 17:25:22 +08:00
$this->registerCommands();
2019-12-07 17:31:38 +08:00
$this->registerValidates();
2019-12-12 18:53:10 +08:00
$this->registerMiddleWares();
2020-04-17 06:48:14 +08:00
$this->registerEvents();
2020-01-13 16:23:08 +08:00
$this->registerQuery();
2020-02-02 22:10:52 +08:00
$this->registerExceptionHandle();
2020-06-24 09:11:48 +08:00
$this->registerRoutePath();
$this->registerServices();
2019-12-07 17:31:38 +08:00
}
2019-12-13 17:25:22 +08:00
/**
*
* @time 2019年12月13日
* @return void
*/
protected function registerCommands(): void
{
2020-07-21 08:01:20 +08:00
if ($this->app->runningInConsole()) {
$catchConsole = new CatchConsole($this->app);
2020-07-21 08:01:20 +08:00
$this->app->bind('catch\console', $catchConsole);
$this->commands($catchConsole->commands());
}
2019-12-13 17:25:22 +08:00
}
2019-12-07 17:31:38 +08:00
/**
*
* @time 2019年12月07日
* @return void
*/
protected function registerValidates(): void
{
2020-01-14 18:32:30 +08:00
$validates = config('catch.validates');
2019-12-07 17:31:38 +08:00
2020-01-14 18:32:30 +08:00
Validate::maker(function($validate) use ($validates) {
2019-12-07 17:31:38 +08:00
foreach ($validates as $vali) {
2020-01-14 18:31:08 +08:00
$vali = app()->make($vali);
2019-12-07 17:31:38 +08:00
$validate->extend($vali->type(), [$vali, 'verify'], $vali->message());
}
});
2019-12-12 18:53:10 +08:00
}
2019-12-07 17:31:38 +08:00
2019-12-12 18:53:10 +08:00
/**
*
* @time 2019年12月12日
* @return void
*/
protected function registerMiddleWares(): void
{
2020-02-18 14:44:16 +08:00
// todo
2019-12-02 23:04:43 +08:00
}
2019-12-12 22:34:08 +08:00
/**
* 注册监听者
*
* @time 2019年12月12日
* @return void
*/
2020-04-17 06:48:14 +08:00
protected function registerEvents(): void
2019-12-12 22:34:08 +08:00
{
2020-06-24 09:11:48 +08:00
$this->app->event->listenEvents([
'RouteLoaded' => [LoadModuleRoutes::class]
]);
2019-12-12 22:34:08 +08:00
}
2020-01-13 16:23:08 +08:00
2020-02-20 13:44:54 +08:00
/**
* register query
*
* @time 2020年02月20日
* @return void
*/
protected function registerQuery(): void
2020-01-13 16:23:08 +08:00
{
$connections = $this->app->config->get('database.connections');
$connections['mysql']['query'] = CatchQuery::class;
$this->app->config->set([
'connections' => $connections
], 'database');
}
2020-02-02 22:10:52 +08:00
2020-02-20 13:44:54 +08:00
/**
* register exception
*
* @time 2020年02月20日
* @return void
*/
protected function registerExceptionHandle(): void
2020-02-02 22:10:52 +08:00
{
$this->app->bind(Handle::class, CatchExceptionHandle::class);
}
2020-06-24 09:11:48 +08:00
/**
* 注册模块服务
*
* @time 2020年06月23日
* @return void
*/
protected function registerServices()
{
2020-09-22 11:14:51 +08:00
$services = file_exists(CatchAdmin::getCacheServicesFile()) ?
include CatchAdmin::getCacheServicesFile() :
CatchAdmin::getEnabledService();
2020-06-24 09:11:48 +08:00
foreach ($services as $service) {
if (class_exists($service)) {
$this->app->register($service);
}
}
}
/**
* 注册路由地址
*
* @time 2020年06月23日
* @return void
*/
protected function registerRoutePath()
{
$this->app->instance('routePath', new class {
protected $path = [];
public function loadRouterFrom($path)
{
$this->path[] = $path;
return $this;
}
public function get()
{
return $this->path;
}
});
}
2019-12-26 09:03:09 +08:00
}