1 | <?php |
||
40 | class ModuleReflection extends BaseObject |
||
41 | { |
||
42 | /** |
||
43 | * @var \luya\web\Request Request object from DI-Container. |
||
44 | */ |
||
45 | public $request; |
||
46 | |||
47 | /** |
||
48 | * @var \luya\web\UrlManager UrlManager object from DI-Container. |
||
49 | */ |
||
50 | public $urlManager; |
||
51 | |||
52 | /** |
||
53 | * @var \yii\base\Controller|null The controller paramter is null until the [[run()]] method has been applied. |
||
54 | */ |
||
55 | public $controller; |
||
56 | |||
57 | private $_defaultRoute; |
||
58 | |||
59 | /** |
||
60 | * Class constructor in order to consum from DI Container. |
||
61 | * |
||
62 | * @param Request $request |
||
63 | * @param UrlManager $urlManager |
||
64 | * @param array $config |
||
65 | */ |
||
66 | public function __construct(Request $request, UrlManager $urlManager, array $config = []) |
||
67 | { |
||
68 | $this->request = $request; |
||
69 | $this->urlManager = $urlManager; |
||
70 | parent::__construct($config); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @inheritdoc |
||
75 | */ |
||
76 | public function init() |
||
77 | { |
||
78 | if ($this->module === null) { |
||
79 | throw new InvalidConfigException('The module attribute is required and can not be null.'); |
||
80 | } |
||
81 | |||
82 | // add the module specific url rules to the url manager |
||
83 | $this->urlManager->addRules($this->module->urlRules, true); |
||
84 | } |
||
85 | |||
86 | private $_module; |
||
87 | |||
88 | /** |
||
89 | * Setter for the module property. |
||
90 | * |
||
91 | * @param Module $module |
||
92 | */ |
||
93 | public function setModule(Module $module) |
||
97 | |||
98 | /** |
||
99 | * Getter for the module property. |
||
100 | * |
||
101 | * @return \luya\base\Module |
||
102 | */ |
||
103 | public function getModule() |
||
107 | |||
108 | private $_suffix; |
||
109 | |||
110 | /** |
||
111 | * Setter for the suffix property. |
||
112 | * |
||
113 | * @param string $suffix |
||
114 | */ |
||
115 | public function setSuffix($suffix) |
||
116 | { |
||
117 | $this->_suffix = $suffix; |
||
118 | $this->request->setPathInfo(implode('/', [$this->module->id, $suffix])); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Getter for the suffix property. |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | public function getSuffix() |
||
127 | { |
||
128 | return $this->_suffix; |
||
129 | } |
||
130 | |||
131 | private $_requestRoute; |
||
132 | |||
133 | /** |
||
134 | * Determine the default route based on current defaultRoutes or parsedRequested by the UrlManager. |
||
135 | * |
||
136 | * @return array An array with |
||
137 | * + route: The path/route to the controller |
||
138 | * + args: If the url has no params, it returns all params from get request. |
||
139 | * + originalArgs: The arguments (params) parsed from the url trogh url manager |
||
140 | * |
||
141 | * @see Related problems and changes: |
||
142 | * + https://github.com/luyadev/luya/issues/1885 |
||
143 | * + https://github.com/luyadev/luya/issues/1267 |
||
144 | * + https://github.com/luyadev/luya/issues/754 |
||
145 | */ |
||
146 | public function getRequestRoute() |
||
147 | { |
||
148 | if ($this->_requestRoute !== null) { |
||
149 | return $this->_requestRoute; |
||
150 | } |
||
151 | |||
152 | if ($this->_defaultRoute !== null && empty($this->getSuffix())) { |
||
153 | $array = $this->_defaultRoute; |
||
154 | } else { |
||
155 | // parse request against urlManager |
||
156 | $route = $this->urlManager->parseRequest($this->request); |
||
157 | // return human readable array |
||
158 | $array = [ |
||
159 | 'route' => $route[0], |
||
160 | 'args' => $route[1], |
||
161 | 'originalArgs' => $route[1], |
||
162 | ]; |
||
163 | } |
||
164 | // resolve the current route by the module |
||
165 | $array['route'] = $this->module->resolveRoute($array['route']); |
||
166 | |||
167 | // if there are no arguments, all get params are assigned. In order to use the original arguments from parse request use `originalArgs` instead of `args`. |
||
168 | if (empty($array['args'])) { |
||
169 | $array['args'] = $this->request->get(); |
||
170 | } |
||
171 | |||
172 | // @see https://github.com/luyadev/luya/issues/1267 |
||
173 | if ($this->_defaultRoute !== null) { |
||
174 | $array['args'] = array_merge($this->_defaultRoute['args'], $array['args']); |
||
175 | } |
||
176 | |||
177 | $this->_requestRoute = $array; |
||
178 | |||
179 | return $array; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Setter method for the requested route |
||
184 | * @param string $route |
||
185 | * @param array $args |
||
186 | */ |
||
187 | public function setRequestRoute($route, array $args = []) |
||
191 | |||
192 | /** |
||
193 | * Inject a defaultRoute. |
||
194 | * |
||
195 | * @param string $controller |
||
196 | * @param string $action |
||
197 | * @param array $args |
||
198 | */ |
||
199 | public function defaultRoute($controller, $action = null, array $args = []) |
||
207 | |||
208 | /** |
||
209 | * Returns the url rule parameters which are taken from the requested route. |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | public function getUrlRule() |
||
223 | |||
224 | /** |
||
225 | * Run the route based on the values. |
||
226 | * |
||
227 | * @return string|\yii\web\Response The response of the action, can be either a string or an object from response. |
||
228 | * @throws \yii\web\NotFoundHttpException |
||
229 | */ |
||
230 | public function run() |
||
266 | } |
||
267 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.