| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core\router;
|
|
|
18 |
|
|
|
19 |
use moodle_url;
|
|
|
20 |
use Psr\Http\Message\ResponseInterface;
|
|
|
21 |
use Psr\Http\Message\ServerRequestInterface;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* A controller to make it easier to implement a route.
|
|
|
25 |
*
|
|
|
26 |
* This controller adds the Container to the constructor which allows controllers to support DI.
|
|
|
27 |
*
|
|
|
28 |
* This trait is entirely optional.
|
|
|
29 |
* @package core
|
|
|
30 |
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
*/
|
|
|
33 |
trait route_controller {
|
|
|
34 |
/**
|
|
|
35 |
* Generate a Page Not Found result.
|
|
|
36 |
*
|
|
|
37 |
* @param ServerRequestInterface $request
|
|
|
38 |
* @param ResponseInterface $response
|
|
|
39 |
* @return ResponseInterface
|
|
|
40 |
* @throws \Slim\Exception\HttpNotFoundException
|
|
|
41 |
*/
|
|
|
42 |
public static function page_not_found(
|
|
|
43 |
ServerRequestInterface $request,
|
|
|
44 |
ResponseInterface $response,
|
|
|
45 |
): ResponseInterface {
|
|
|
46 |
return util::throw_page_not_found($request, $response);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Redirect to a URL.
|
|
|
51 |
*
|
|
|
52 |
* @param ResponseInterface $response
|
|
|
53 |
* @param string|moodle_url $url
|
|
|
54 |
* @return ResponseInterface
|
|
|
55 |
*/
|
|
|
56 |
public static function redirect(
|
|
|
57 |
ResponseInterface $response,
|
|
|
58 |
string|moodle_url $url,
|
|
|
59 |
): ResponseInterface {
|
|
|
60 |
return util::redirect($response, $url);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Redirect to the requested callable.
|
|
|
65 |
*
|
|
|
66 |
* @param ServerRequestInterface $request
|
|
|
67 |
* @param ResponseInterface $response
|
|
|
68 |
* @param array|callable|string $callable
|
|
|
69 |
* @param null|array $pathparams
|
|
|
70 |
* @param null|array $queryparams
|
|
|
71 |
* @param null|array $excludeparams A list of any parameters to remove the URI during the redirect
|
|
|
72 |
* @return ResponseInterface
|
|
|
73 |
*/
|
|
|
74 |
public static function redirect_to_callable(
|
|
|
75 |
ServerRequestInterface $request,
|
|
|
76 |
ResponseInterface $response,
|
|
|
77 |
array|callable|string $callable,
|
|
|
78 |
?array $pathparams = null,
|
|
|
79 |
?array $queryparams = null,
|
|
|
80 |
?array $excludeparams = null,
|
|
|
81 |
): ResponseInterface {
|
|
|
82 |
return util::redirect_to_callable(
|
|
|
83 |
$request,
|
|
|
84 |
$response,
|
|
|
85 |
$callable,
|
|
|
86 |
$pathparams,
|
|
|
87 |
$queryparams,
|
|
|
88 |
$excludeparams,
|
|
|
89 |
);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Get a parameter from the query params after validation.
|
|
|
94 |
*
|
|
|
95 |
* @param ServerRequestInterface $request
|
|
|
96 |
* @param string $key
|
|
|
97 |
* @param mixed $default
|
|
|
98 |
* @return mixed
|
|
|
99 |
*/
|
|
|
100 |
protected function get_param(
|
|
|
101 |
ServerRequestInterface $request,
|
|
|
102 |
string $key,
|
|
|
103 |
mixed $default = null,
|
|
|
104 |
): mixed {
|
|
|
105 |
$params = $request->getQueryParams();
|
|
|
106 |
if (array_key_exists($key, $params)) {
|
|
|
107 |
return $params[$key];
|
|
|
108 |
} else {
|
|
|
109 |
debugging("Missing parameter: $key");
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
return $default;
|
|
|
113 |
}
|
|
|
114 |
}
|