Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| FlowController | |
0.00% |
0 / 36 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
| forwardUrl | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
30 | |||
| forward | |
0.00% |
0 / 2 |
|
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) 2006-2009 (original work) Public Research Centre Henri Tudor (under the project FP6-IST-PALETTE); |
| 19 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 20 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 21 | * 2014 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 22 | */ |
| 23 | |
| 24 | namespace oat\tao\model\routing; |
| 25 | |
| 26 | use common_http_Request; |
| 27 | use common_ext_ExtensionsManager; |
| 28 | use InterruptedActionException; |
| 29 | use Context; |
| 30 | use FlowController as ClearFwFlowController; |
| 31 | use oat\oatbox\service\ServiceManagerAwareInterface; |
| 32 | use oat\oatbox\service\ServiceManagerAwareTrait; |
| 33 | |
| 34 | /** |
| 35 | * The FlowController helps you to navigate through MVC actions. |
| 36 | * |
| 37 | * @author Jérôme Bogaerts <jerome.bogaerts@tudor.lu> <jerome.bogaerts@gmail.com> |
| 38 | * @author Bertrand Chevrier <Bertrand@taotestin.com> |
| 39 | */ |
| 40 | class FlowController extends ClearFwFlowController implements ServiceManagerAwareInterface |
| 41 | { |
| 42 | use ServiceManagerAwareTrait; |
| 43 | |
| 44 | /** |
| 45 | * This header is added to the response to inform the client a forward occurs |
| 46 | */ |
| 47 | public const FORWARD_HEADER = 'X-Tao-Forward'; |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * Forward the action to execute reqarding a URL |
| 52 | * The forward runs into tha same HTTP request unlike redirect. |
| 53 | * @param string $url the url to forward to |
| 54 | */ |
| 55 | public function forwardUrl($url) |
| 56 | { |
| 57 | |
| 58 | //get the current request |
| 59 | $request = common_http_Request::currentRequest(); |
| 60 | $params = $request->getParams(); |
| 61 | |
| 62 | //parse the given URL |
| 63 | $parsedUrl = parse_url($url); |
| 64 | |
| 65 | //if new parameters are given, then merge them |
| 66 | if (isset($parsedUrl['query']) && strlen($parsedUrl['query']) > 0) { |
| 67 | $newParams = []; |
| 68 | parse_str($parsedUrl['query'], $newParams); |
| 69 | if (count($newParams) > 0) { |
| 70 | $params = array_merge($params, $newParams); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | //resolve the given URL for routing |
| 75 | $resolver = new Resolver(new common_http_Request($parsedUrl['path'], $request->getMethod(), $params)); |
| 76 | $this->propagate($resolver); |
| 77 | |
| 78 | $context = Context::getInstance(); |
| 79 | |
| 80 | // load the responsible extension |
| 81 | common_ext_ExtensionsManager::singleton()->getExtensionById($resolver->getExtensionId()); |
| 82 | |
| 83 | //update the context to the new route |
| 84 | $context->setExtensionName($resolver->getExtensionId()); |
| 85 | $context->setModuleName($resolver->getControllerShortName()); |
| 86 | $context->setActionName($resolver->getMethodName()); |
| 87 | if (count($params) > 0) { |
| 88 | $context->getRequest()->addParameters($params); |
| 89 | } |
| 90 | |
| 91 | //add a custom header so the client knows where the route ends |
| 92 | header( |
| 93 | self::FORWARD_HEADER . ': ' . $resolver->getExtensionId() . '/' |
| 94 | . $resolver->getControllerShortName() . '/' . $resolver->getMethodName() |
| 95 | ); |
| 96 | |
| 97 | //execite the new action |
| 98 | $enforcer = new ActionEnforcer( |
| 99 | $resolver->getExtensionId(), |
| 100 | $resolver->getControllerClass(), |
| 101 | $resolver->getMethodName(), |
| 102 | $params |
| 103 | ); |
| 104 | $this->propagate($enforcer); |
| 105 | $enforcer->execute(); |
| 106 | |
| 107 | //should not be reached |
| 108 | throw new InterruptedActionException( |
| 109 | 'Interrupted action after a forward', |
| 110 | $context->getModuleName(), |
| 111 | $context->getActionName() |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Forward routing. |
| 117 | |
| 118 | * @param string $action the name of the new action |
| 119 | * @param string $controller the name of the new controller/module |
| 120 | * @param string $extension the name of the new extension |
| 121 | * @param array $params additional parameters |
| 122 | */ |
| 123 | public function forward($action, $controller = null, $extension = null, $params = []) |
| 124 | { |
| 125 | //as we use a route resolver, it's easier to rebuild the URL to resolve it |
| 126 | $this->forwardUrl(\tao_helpers_Uri::url($action, $controller, $extension, $params)); |
| 127 | } |
| 128 | } |