forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontroller_invoker.php
109 lines (100 loc) · 3.75 KB
/
controller_invoker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\router;
use Invoker\InvokerInterface;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* Controller Invoker for the Moodle Router.
*
* This class handles invocation of the route callable, and the conversion of the response into an appropriate format.
*
* @package core
* @copyright 2023 Andrew Lyons <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class controller_invoker extends \DI\Bridge\Slim\ControllerInvoker {
/**
* Create a new controller invoker.
*
* @param ContainerInterface $container
* @param InvokerInterface $invoker
*/
public function __construct(
/** @var ContainerInterface The DI container */
private ContainerInterface $container,
/** @var InvokerInterface The invoker */
private InvokerInterface $invoker,
) {
}
/**
* Invoke a route callable.
*
* Note: Much of this is copied from the parent class, but we need to handle the response differently.
*
* @param callable $callable The callable to invoke using the strategy.
* @param ServerRequestInterface $request The request object.
* @param ResponseInterface $response The response object.
* @param array $routeargs The route's placeholder arguments
*
* @return ResponseInterface|string The response from the callable.
*/
public function __invoke(
callable $callable,
ServerRequestInterface $request,
ResponseInterface $response,
array $routeargs,
): ResponseInterface {
// Inject the request and response by parameter name.
$parameters = [
'request' => self::inject_route_arguments($request, $routeargs),
'response' => $response,
];
// Inject the route arguments by name.
$parameters += $routeargs;
// Inject the attributes defined on the request.
$parameters += $request->getAttributes();
$result = $this->invoker->call($callable, $parameters);
return $this
->container
->get(response_handler::class)
->standardise_response($result);
}
/**
* Helper to inject route arguments.
*
* This is based on the ControllerInvoker.
*
* @param ServerRequestInterface $request
* @param array $routeargs
* @return ServerRequestInterface
*/
private static function inject_route_arguments(
ServerRequestInterface $request,
array $routeargs,
): ServerRequestInterface {
$args = $request;
foreach ($routeargs as $key => $value) {
// Note: This differs to upstream where route args always override attributes.
// We apply mapped parameters via route attributes and must therefore override the route args.
if (!$args->getAttribute($key)) {
$args = $args->withAttribute($key, $value);
}
}
return $args;
}
}