Test Setup Failed
Push — master ( bf3c7e...ad0913 )
by Dennis
04:09
created

RegionServiceProvider::loadFacades()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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->loadedValidator();
25
        $this->loadedHelpers();
26
        $this->loadFacades();
27
        $this->bindMacroService();
28
    }
29
30
    /**
31
     * 加载验证
32
     *
33
     * @author Dennis Lui <[email protected]>
34
     * @return void
35
     */
36
    protected function loadedValidator(): void
37
    {
38
        $map = [
39
            'region_area' => RegionAreaRule::class,
40
            'region_code' => RegionCodeRule::class,
41
            'region_name' => RegionNameRule::class,
42
            'region_number' => RegionNumberRule::class,
43
            'region_zip' => RegionZipRule::class
44
        ];
45
        foreach ($map as $name => $class) {
46
            Validator::extend($name, $class);
47
        }
48
    }
49
50
    /**
51
     * 修改query
52
     *
53
     * @author Dennis Lui <[email protected]>
54
     * @return void
55
     */
56
    protected function bindMacroService(): void
57
    {
58
        if (class_exists(SimpleService::class)) {
59
            SimpleService::macro('queryDistance', function (SimpleService $service, float $lat, float $lng, float $maxDistance = 50, string $geoColumn) {
60
                $distanceService = new DistanceService;
61
                return $distanceService->queryDistance($service, $lat, $lng, $maxDistance, $geoColumn);
62
            });
63
            SimpleService::macro('selectDistance', function (SimpleService $service, float $lat, float $lng, string $geoColumn, string $alias) {
64
                $distanceService = new DistanceService;
65
                return $distanceService->selectDistance($service, $lat, $lng, $geoColumn, $alias);
66
            });
67
        }
68
    }
69
70
    /**
71
     * 绑定Facades
72
     *
73
     * @author Dennis Lui <[email protected]>
74
     * @return void
75
     */
76
    protected function loadFacades(): void
77
    {
78
        $this->app->bind('region', fn() => new \SimpleCMS\Region\Packages\Region(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '/data/cities.json'));
79
    }
80
81
82
    /**
83
     * 加载辅助函数
84
     *
85
     * @author Dennis Lui <[email protected]>
86
     * @return void
87
     */
88
    protected function loadedHelpers(): void
89
    {
90
91
        foreach (scandir(__DIR__ . DIRECTORY_SEPARATOR . 'helpers') as $helperFile) {
92
            $path = sprintf(
93
                '%s%s%s%s%s',
94
                __DIR__,
95
                DIRECTORY_SEPARATOR,
96
                'helpers',
97
                DIRECTORY_SEPARATOR,
98
                $helperFile
99
            );
100
101
            if (!is_file($path)) {
102
                continue;
103
            }
104
105
            $function = Str::before($helperFile, '.php');
106
107
            if (function_exists($function)) {
108
                continue;
109
            }
110
111
            require_once $path;
112
        }
113
    }
114
}
115