Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 56 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
HttpFlowTrait | |
0.00% |
0 / 56 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
getPsrRequest | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getServiceLocator | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
redirect | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
forwardUrl | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
30 | |||
forward | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2019 (original work) Open Assessment Technologies SA; |
19 | * |
20 | */ |
21 | |
22 | namespace oat\tao\model\http; |
23 | |
24 | use Context; |
25 | use GuzzleHttp\Psr7\Uri; |
26 | use HTTPToolkit; |
27 | use InterruptedActionException; |
28 | use oat\tao\model\routing\ActionEnforcer; |
29 | use oat\tao\model\routing\Resolver; |
30 | use Psr\Http\Message\ServerRequestInterface; |
31 | use Zend\ServiceManager\ServiceLocatorInterface; |
32 | use oat\oatbox\extension\exception\ManifestNotFoundException; |
33 | |
34 | /** |
35 | * Trait HttpFlowTrait |
36 | * @package oat\tao\model\http |
37 | * @author Moyon Camille |
38 | */ |
39 | trait HttpFlowTrait |
40 | { |
41 | /** |
42 | * @return ServerRequestInterface |
43 | */ |
44 | abstract public function getPsrRequest(); |
45 | |
46 | /** |
47 | * @return ServiceLocatorInterface |
48 | */ |
49 | abstract public function getServiceLocator(); |
50 | |
51 | /** |
52 | * Redirect using the TAO FlowController implementation |
53 | * |
54 | * @see {@link oat\model\routing\FlowController} |
55 | * @param string $url |
56 | * @param int $statusCode |
57 | * @throws InterruptedActionException |
58 | */ |
59 | public function redirect($url, $statusCode = 302) |
60 | { |
61 | $context = Context::getInstance(); |
62 | |
63 | header(HTTPToolkit::statusCodeHeader($statusCode)); |
64 | header(HTTPToolkit::locationHeader($url)); |
65 | |
66 | throw new InterruptedActionException( |
67 | 'Interrupted action after a redirection', |
68 | $context->getModuleName(), |
69 | $context->getActionName() |
70 | ); |
71 | } |
72 | |
73 | /** |
74 | * Forward the action to execute regarding a URL |
75 | * The forward runs into tha same HTTP request unlike redirect. |
76 | * |
77 | * @param string $url the url to forward to |
78 | * @throws InterruptedActionException |
79 | * @throws \ResolverException |
80 | * @throws \common_exception_InconsistentData |
81 | * @throws \common_exception_InvalidArgumentType |
82 | * @throws ManifestNotFoundException |
83 | */ |
84 | public function forwardUrl($url) |
85 | { |
86 | $uri = new Uri($url); |
87 | $query = $uri->getQuery(); |
88 | $queryParams = []; |
89 | if (strlen($query) > 0) { |
90 | parse_str($query, $queryParams); |
91 | } |
92 | |
93 | switch ($this->getPsrRequest()->getMethod()) { |
94 | case 'GET': |
95 | $params = $this->getPsrRequest()->getQueryParams(); |
96 | break; |
97 | case 'POST': |
98 | $params = $this->getPsrRequest()->getParsedBody(); |
99 | break; |
100 | default: |
101 | $params = []; |
102 | } |
103 | $request = $this->getPsrRequest() |
104 | ->withUri($uri) |
105 | ->withQueryParams((array) $queryParams); |
106 | |
107 | //resolve the given URL for routing |
108 | $resolver = new Resolver($request); |
109 | $resolver->setServiceLocator($this->getServiceLocator()); |
110 | |
111 | //update the context to the new route |
112 | $context = \Context::getInstance(); |
113 | $context->setExtensionName($resolver->getExtensionId()); |
114 | $context->setModuleName($resolver->getControllerShortName()); |
115 | $context->setActionName($resolver->getMethodName()); |
116 | |
117 | $context->getRequest()->addParameters($queryParams); |
118 | |
119 | $request = $request |
120 | ->withAttribute('extension', $resolver->getExtensionId()) |
121 | ->withAttribute('controller', $resolver->getControllerShortName()) |
122 | ->withAttribute('method', $resolver->getMethodName()); |
123 | |
124 | //execute the new action |
125 | $enforcer = new ActionEnforcer( |
126 | $resolver->getExtensionId(), |
127 | $resolver->getControllerClass(), |
128 | $resolver->getMethodName(), |
129 | $params |
130 | ); |
131 | $enforcer->setServiceLocator($this->getServiceLocator()); |
132 | |
133 | $enforcer( |
134 | $request, |
135 | $this->response->withHeader( |
136 | 'X-Tao-Forward', |
137 | $resolver->getExtensionId() . '/' . $resolver->getControllerShortName() . '/' |
138 | . $resolver->getMethodName() |
139 | ) |
140 | ); |
141 | |
142 | throw new InterruptedActionException( |
143 | 'Interrupted action after a forwardUrl', |
144 | $context->getModuleName(), |
145 | $context->getActionName() |
146 | ); |
147 | } |
148 | |
149 | /** |
150 | * Forward routing. |
151 | * |
152 | * @param string $action the name of the new action |
153 | * @param string $controller the name of the new controller/module |
154 | * @param string $extension the name of the new extension |
155 | * @param array $params additional parameters |
156 | * @throws InterruptedActionException |
157 | * @throws \ActionEnforcingException |
158 | * @throws \common_exception_Error |
159 | */ |
160 | public function forward($action, $controller = null, $extension = null, $params = []) |
161 | { |
162 | //as we use a route resolver, it's easier to rebuild the URL to resolve it |
163 | $this->forwardUrl(\tao_helpers_Uri::url($action, $controller, $extension, $params)); |
164 | } |
165 | } |