Passed
Push — dev ( 4afce3...0792b6 )
by 世昌
02:33
created

ApplicationLoader::loadRouteGroup()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 6
nop 2
dl 0
loc 13
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace suda\application\loader;
4
5
use suda\framework\Config;
6
use suda\application\Module;
7
8
/**
9
 * 应用程序
10
 */
11
class ApplicationLoader extends ApplicationBaseLoader
12
{
13
14
    const CACHE_ROUTE = 'application-route';
15
    const CACHE_ROUTE_RUNNABLE = 'application-route-runnable';
16
17
    /**
18
     * 加载路由
19
     */
20
    public function loadRoute()
21
    {
22
        $name = 'application-route';
23
        $this->application->debug()->time($name);
24
        if (static::isDebug()) {
25
            $this->loadRouteFromModules();
26
            if ($this->application->getRoute()->isContainClosure()) {
27
                $this->application->debug()->warning('route contain closure, route prepare cannot be cacheables');
28
            } else {
29
                $this->application->cache()->set(ApplicationLoader::CACHE_ROUTE, $this->application->getRoute()->getRouteCollection());
30
                $this->application->cache()->set(ApplicationLoader::CACHE_ROUTE_RUNNABLE, $this->application->getRoute()->getRunnable());
31
            }
32
        } elseif ($this->routeCacheAvailable()) {
33
            $route = $this->application->cache()->get(ApplicationLoader::CACHE_ROUTE);
34
            $runnable = $this->application->cache()->get(ApplicationLoader::CACHE_ROUTE_RUNNABLE);
35
            $this->application->getRoute()->setRouteCollection($route);
36
            $this->application->getRoute()->setRunnable($runnable);
37
            $this->application->debug()->info('load route from cache');
38
        } else {
39
            $this->loadRouteFromModules();
40
        }
41
        $this->application->debug()->timeEnd($name);
42
    }
43
44
    /**
45
     * 从模块中加载路由
46
     */
47
    private function loadRouteFromModules()
48
    {
49
        foreach ($this->application->getModules() as $name => $module) {
50
            if ($module->getStatus() === Module::REACHABLE) {
51
                $this->loadModuleRoute($module);
52
                $this->application->debug()->debug('reachable # ' . $module->getFullName());
53
            }
54
        }
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    private function routeCacheAvailable()
61
    {
62
        return $this->application->cache()->has(ApplicationLoader::CACHE_ROUTE)
63
            && $this->application->cache()->has(ApplicationLoader::CACHE_ROUTE_RUNNABLE);
64
    }
65
66
    /**
67
     * 加载路由
68
     * @param Module $module
69
     */
70
    protected function loadModuleRoute(Module $module)
71
    {
72
        foreach ($this->application->getRouteGroup() as $group) {
0 ignored issues
show
Bug introduced by
The method getRouteGroup() does not exist on suda\application\ApplicationModule. Did you maybe mean getRoute()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        foreach ($this->application->/** @scrutinizer ignore-call */ getRouteGroup() as $group) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
            $this->loadRouteGroup($module, $group);
74
        }
75
    }
76
77
    /**
78
     * 加载路由组
79
     *
80
     * @param Module $module
81
     * @param string $groupName
82
     * @return void
83
     */
84
    protected function loadRouteGroup(Module $module, string $groupName)
85
    {
86
        $group = $groupName === 'default' ? '' : '-' . $groupName;
87
        if ($path = $module->getResource()->getConfigResourcePath('config/route' . $group)) {
88
            $routeConfig = Config::loadConfig($path, [
89
                'module' => $module->getName(),
90
                'group' => $groupName,
91
                'property' => $module->getProperty(),
92
                'config' => $module->getConfig(),
93
            ]);
94
            if ($routeConfig !== null) {
95
                $prefix = $module->getConfig('route-prefix.' . $groupName, '');
96
                $this->loadRouteConfig($module, $prefix, $groupName, $routeConfig);
97
            }
98
        }
99
    }
100
101
    /**
102
     * 加载模块路由配置
103
     *
104
     * @param Module $module
105
     * @param string $prefix
106
     * @param string $groupName
107
     * @param array $routeConfig
108
     * @return void
109
     */
110
    protected function loadRouteConfig(Module $module, string $prefix, string $groupName, array $routeConfig)
111
    {
112
        $module = $module->getFullName();
113
        foreach ($routeConfig as $name => $config) {
114
            $exname = $this->application->getRouteName($name, $module, $groupName);
0 ignored issues
show
Bug introduced by
The method getRouteName() does not exist on suda\application\ApplicationModule. Did you maybe mean getRoute()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
            /** @scrutinizer ignore-call */ 
115
            $exname = $this->application->getRouteName($name, $module, $groupName);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
            $method = $config['method'] ?? [];
116
            $attributes = [];
117
            $attributes['module'] = $module;
118
            $attributes['config'] = $config;
119
            $attributes['group'] = $groupName;
120
            $attributes['route'] = $exname;
121
            $uri = $config['uri'] ?? '/';
122
            $anti = array_key_exists('anti-prefix', $config) && $config['anti-prefix'];
123
            if ($anti) {
124
                $uri = '/' . trim($uri, '/');
125
            } else {
126
                $uri = '/' . trim($prefix . $uri, '/');
127
            }
128
            $this->application->request($method, $exname, $uri, $attributes);
0 ignored issues
show
Bug introduced by
The method request() does not exist on suda\application\ApplicationModule. It seems like you code against a sub-type of suda\application\ApplicationModule such as suda\application\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
            $this->application->/** @scrutinizer ignore-call */ 
129
                                request($method, $exname, $uri, $attributes);
Loading history...
129
        }
130
    }
131
}
132