|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimpleCMS\Region; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Illuminate\Support\ServiceProvider; |
|
7
|
|
|
use Illuminate\Support\Facades\Validator; |
|
8
|
|
|
use SimpleCMS\Region\Services\DistanceService; |
|
9
|
|
|
use SimpleCMS\Framework\Services\SimpleService; |
|
10
|
|
|
use SimpleCMS\Region\Validation\Rule\RegionZipRule; |
|
11
|
|
|
use SimpleCMS\Region\Validation\Rule\RegionAreaRule; |
|
12
|
|
|
use SimpleCMS\Region\Validation\Rule\RegionCodeRule; |
|
13
|
|
|
use SimpleCMS\Region\Validation\Rule\RegionNameRule; |
|
14
|
|
|
use SimpleCMS\Region\Validation\Rule\RegionNumberRule; |
|
15
|
|
|
|
|
16
|
|
|
class RegionServiceProvider extends ServiceProvider |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Bootstrap services. |
|
21
|
|
|
*/ |
|
22
|
|
|
public function boot(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$this->bootConfig(); |
|
25
|
|
|
$this->loadedValidator(); |
|
26
|
|
|
$this->loadedHelpers(); |
|
27
|
|
|
$this->loadFacades(); |
|
28
|
|
|
$this->bindMacroService(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* 加载验证 |
|
33
|
|
|
* |
|
34
|
|
|
* @author Dennis Lui <[email protected]> |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function loadedValidator(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$map = [ |
|
40
|
|
|
'region_area' => RegionAreaRule::class, |
|
41
|
|
|
'region_code' => RegionCodeRule::class, |
|
42
|
|
|
'region_name' => RegionNameRule::class, |
|
43
|
|
|
'region_number' => RegionNumberRule::class, |
|
44
|
|
|
'region_zip' => RegionZipRule::class |
|
45
|
|
|
]; |
|
46
|
|
|
foreach ($map as $name => $class) { |
|
47
|
|
|
Validator::extend($name, $class); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* 修改query |
|
53
|
|
|
* |
|
54
|
|
|
* @author Dennis Lui <[email protected]> |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function bindMacroService(): void |
|
58
|
|
|
{ |
|
59
|
|
|
if (class_exists(SimpleService::class)) { |
|
60
|
|
|
SimpleService::macro('queryDistance', function (SimpleService $service, float $lat, float $lng, float $maxDistance = 50, string $geoColumn) { |
|
61
|
|
|
$distanceService = new DistanceService; |
|
62
|
|
|
return $distanceService->queryDistance($service, $lat, $lng, $maxDistance, $geoColumn); |
|
63
|
|
|
}); |
|
64
|
|
|
SimpleService::macro('selectDistance', function (SimpleService $service, float $lat, float $lng, string $geoColumn, string $alias) { |
|
65
|
|
|
$distanceService = new DistanceService; |
|
66
|
|
|
return $distanceService->selectDistance($service, $lat, $lng, $geoColumn, $alias); |
|
67
|
|
|
}); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* 绑定Facades |
|
73
|
|
|
* |
|
74
|
|
|
* @author Dennis Lui <[email protected]> |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function loadFacades(): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->app->bind('region', fn() => new \SimpleCMS\Region\Packages\Region(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '/data/cities.json')); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* 加载辅助函数 |
|
85
|
|
|
* |
|
86
|
|
|
* @author Dennis Lui <[email protected]> |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function loadedHelpers(): void |
|
90
|
|
|
{ |
|
91
|
|
|
|
|
92
|
|
|
foreach (scandir(__DIR__ . DIRECTORY_SEPARATOR . 'helpers') as $helperFile) { |
|
93
|
|
|
$path = sprintf( |
|
94
|
|
|
'%s%s%s%s%s', |
|
95
|
|
|
__DIR__, |
|
96
|
|
|
DIRECTORY_SEPARATOR, |
|
97
|
|
|
'helpers', |
|
98
|
|
|
DIRECTORY_SEPARATOR, |
|
99
|
|
|
$helperFile |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
if (!is_file($path)) { |
|
103
|
|
|
continue; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$function = Str::before($helperFile, '.php'); |
|
107
|
|
|
|
|
108
|
|
|
if (function_exists($function)) { |
|
109
|
|
|
continue; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
require_once $path; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* 初始化配置文件 |
|
118
|
|
|
* @return void |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function bootConfig(): void |
|
121
|
|
|
{ |
|
122
|
|
|
$this->publishes([ |
|
123
|
|
|
__DIR__ . '/../database/migrations' => database_path('migrations'), |
|
124
|
|
|
], 'simplecms'); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|