Passed
Push — master ( 6740b1...356763 )
by 世昌
02:25
created

MatchResult::getMatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\application\route;
3
4
use nebula\Context;
5
use ReflectionClass;
6
use nebula\application\Loader;
7
use nebula\application\Response;
8
use nebula\component\runnable\Runnable;
9
use nebula\component\route\RouteMatcher;
10
11
/**
12
 * 匹配结果
13
 */
14
class MatchResult
15
{
16
    /**
17
     * 匹配对象
18
     *
19
     * @var RouteMatcher
20
     */
21
    protected $matcher;
22
23
24
    /**
25
     * 匹配的参数
26
     *
27
     * @var array
28
     */
29
    protected $parameter;
30
31
    /**
32
     * 匹配的模块
33
     *
34
     * @var string
35
     */
36
    protected $module;
37
38
    /**
39
     * 匹配的名字
40
     *
41
     * @var string
42
     */
43
    protected $name;
44
45
    public function __construct(string $module, string $name, RouteMatcher $matcher, array $parameter)
46
    {
47
        $this->matcher = $matcher;
48
        $this->parameter = $parameter;
49
        $this->module = $module;
50
        $this->name = $name;
51
    }
52
53
    /**
54
     * Get 匹配对象
55
     *
56
     * @return  RouteMatcher
57
     */
58
    public function getMatcher()
59
    {
60
        return $this->matcher;
61
    }
62
63
    /**
64
     * Set 匹配对象
65
     *
66
     * @param  RouteMatcher  $matcher  匹配对象
67
     *
68
     * @return  self
69
     */
70
    public function setMatcher(RouteMatcher $matcher)
71
    {
72
        $this->matcher = $matcher;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Get 匹配的参数
79
     *
80
     * @return  array
81
     */
82
    public function getParameter()
83
    {
84
        return $this->parameter;
85
    }
86
87
    /**
88
     * Set 匹配的参数
89
     *
90
     * @param  array  $parameter  匹配的参数
91
     *
92
     * @return  self
93
     */
94
    public function setParameter(array $parameter)
95
    {
96
        $this->parameter = $parameter;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Get 匹配的模块
103
     *
104
     * @return  string
105
     */
106
    public function getModule()
107
    {
108
        return $this->module;
109
    }
110
111
    /**
112
     * Set 匹配的模块
113
     *
114
     * @param  string  $module  匹配的模块
115
     *
116
     * @return  self
117
     */
118
    public function setModule(string $module)
119
    {
120
        $this->module = $module;
121
122
        return $this;
123
    }
124
125
    /**
126
     * Get 匹配的名字
127
     *
128
     * @return  string
129
     */
130
    public function getName()
131
    {
132
        return $this->name;
133
    }
134
135
    /**
136
     * Set 匹配的名字
137
     *
138
     * @param  string  $name  匹配的名字
139
     *
140
     * @return  self
141
     */
142
    public function setName(string $name)
143
    {
144
        $this->name = $name;
145
146
        return $this;
147
    }
148
149
    public function run(Context $context)
150
    {
151
        $context->getApplication()->getManager()->registerPrivateClassLoader($this->module, $context->getApplication());
152
        $className = $this->matcher->getAttribute('class');
153
        if ($className !== null) {
154
            $this->runWithResponseClass($context, $className);
155
        }
156
    }
157
158
    protected function runWithResponseClass(Context $context, string $className)
159
    {
160
        $className = Loader::realName($className);
161
        $class = new ReflectionClass($className);
162
        if ($class->isSubclassOf(Response::class)) {
163
            $object = $class->getMethod('instance')->invoke(null);
164
            $context->setResponse($object);
165
            $class->getMethod('proccess')->invokeArgs($object, [
166
                new Runnable([$object,'onRequest']),
167
                $context->getRequest(),
168
                $this->matcher->getAttribute('buffer', true),
169
            ]);
170
        } else {
171
            throw new \Exception(sprintf('class route must be instance of %s in route %s:%s', Response::class, $this->module, $this->name));
172
        }
173
    }
174
}
175