Passed
Push — master ( acba12...b2f89b )
by 世昌
04:15
created

ApplicationRoute   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
c 0
b 0
f 0
dl 0
loc 129
rs 10
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setRouteGroup() 0 3 1
A getRouteGroupPrefix() 0 3 3
A getUriBase() 0 4 2
A getUrlIndex() 0 11 3
A getRouteName() 0 12 4
A getRouteGroup() 0 3 1
A getUrl() 0 12 1
1
<?php
2
3
namespace suda\application;
4
5
use suda\framework\loader\Loader;
6
use function explode;
7
use function in_array;
8
use function strlen;
9
use function strrpos;
10
use suda\framework\Request;
11
12
/**
13
 * 应用源处理
14
 */
15
class ApplicationRoute extends ApplicationModule
16
{
17
18
    /**
19
     * 路由组
20
     *
21
     * @var array
22
     */
23
    protected $routeGroup;
24
25
    /**
26
     * ApplicationSource constructor.
27
     * @param string $path
28
     * @param array $manifest
29
     * @param Loader $loader
30
     * @param string|null $dataPath
31
     */
32
    public function __construct(string $path, array $manifest, Loader $loader, ?string $dataPath = null)
33
    {
34
        parent::__construct($path, $manifest, $loader, $dataPath);
35
        $this->setRouteGroup($manifest['route-group'] ?? ['default']);
36
    }
37
38
    /**
39
     * 获取URL
40
     *
41
     * @param Request $request
42
     * @param string $name
43
     * @param array $parameter
44
     * @param bool $allowQuery
45
     * @param string|null $default
46
     * @param string|null $group
47
     * @return string|null
48
     */
49
    public function getUrl(
50
        Request $request,
51
        string $name,
52
        array $parameter = [],
53
        bool $allowQuery = true,
54
        ?string $default = null,
55
        ?string $group = null
56
    ): ?string {
57
        $group = $group ?? $request->getAttribute('group');
58
        $default = $default ?? $request->getAttribute('module');
59
        $url = $this->route->create($this->getRouteName($name, $default, $group), $parameter, $allowQuery);
60
        return $this->getUrlIndex($request) . ltrim($url, '/');
61
    }
62
63
    /**
64
     * 获取URL索引
65
     *
66
     * @param Request $request
67
     * @return string
68
     */
69
    protected function getUrlIndex(Request $request): string
70
    {
71
        $indexArray = $this->conf('index') ?? ['index.php'];
72
        $rewrite = $this->conf('url_rewrite', false);
73
        $base = $request->getIndex();
74
        $index = ltrim($base, '/');
75
        // 根目录重写开启
76
        if (in_array($index, $indexArray) && $rewrite) {
77
            $base = '';
78
        }
79
        return $base.'/';
80
    }
81
82
    /**
83
     * 获取基础URI
84
     *
85
     * @param Request $request
86
     * @param boolean $beautify
87
     * @return string
88
     */
89
    public function getUriBase(Request $request, bool $beautify = true): string
90
    {
91
        $index = $beautify ? $this->getUrlIndex($request) : $request->getIndex();
92
        return $request->getUriBase() . $index;
93
    }
94
95
    /**
96
     * 获取分组前缀
97
     *
98
     * @param string|null $group
99
     * @return string
100
     */
101
    protected function getRouteGroupPrefix(?string $group): string
102
    {
103
        return $group === null || $group === 'default' ? '' : '@' . $group;
104
    }
105
106
    /**
107
     * @param array $routeGroup
108
     */
109
    public function setRouteGroup(array $routeGroup): void
110
    {
111
        $this->routeGroup = $routeGroup;
112
    }
113
114
    /**
115
     * Get 路由组
116
     *
117
     * @return  array
118
     */
119
    public function getRouteGroup()
120
    {
121
        return $this->routeGroup;
122
    }
123
124
    /**
125
     * 获取路由全名
126
     *
127
     * @param string $name
128
     * @param string|null $default
129
     * @param string|null $group
130
     * @return string
131
     */
132
    public function getRouteName(string $name, ?string $default = null, ?string $group = null): string
133
    {
134
        if (strpos($name, ':') !== false) {
135
            list($module, $group, $name) = $this->parseSourceName($name, $default, $group);
136
        } else {
137
            $module = $default;
138
        }
139
        $prefixGroup = $this->getRouteGroupPrefix($group);
140
        if ($module !== null && ($moduleObj = $this->find($module))) {
141
            return $moduleObj->getFullName() . $prefixGroup . ':' . $name;
142
        }
143
        return $name;
144
    }
145
}
146