Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 Invoker\InvokerInterface;
20
use Psr\Container\ContainerInterface;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
 
24
/**
25
 * Controller Invoker for the Moodle Router.
26
 *
27
 * This class handles invocation of the route callable, and the conversion of the response into an appropriate format.
28
 *
29
 * @package    core
30
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class controller_invoker implements \Slim\Interfaces\InvocationStrategyInterface {
34
    /**
35
     * Create a new controller invoker.
36
     *
37
     * @param ContainerInterface $container
38
     * @param InvokerInterface $invoker
39
     */
40
    public function __construct(
41
        /** @var ContainerInterface The DI container */
42
        protected ContainerInterface $container,
43
        /** @var InvokerInterface The invoker */
44
        protected InvokerInterface $invoker,
45
    ) {
46
    }
47
 
48
    #[\Override]
49
    public function __invoke(
50
        callable $callable,
51
        ServerRequestInterface $request,
52
        ResponseInterface $response,
53
        array $routeArguments, // phpcs:ignore moodle.NamingConventions.ValidVariableName.VariableNameLowerCase
54
    ): ResponseInterface {
55
        // Inject the request and response by parameter name.
56
        $parameters = [
57
            'request'  => self::inject_route_arguments(
58
                $request,
59
                $routeArguments, // phpcs:ignore moodle.NamingConventions.ValidVariableName.VariableNameLowerCase
60
            ),
61
            'response' => $response,
62
        ];
63
 
64
        // Inject the route arguments by name.
65
        $parameters += $routeArguments; // phpcs:ignore moodle.NamingConventions.ValidVariableName.VariableNameLowerCase
66
 
67
        // Inject the attributes defined on the request.
68
        $parameters += $request->getAttributes();
69
 
70
        $result = $this->invoker->call($callable, $parameters);
71
 
72
        return $this->container->get(response_handler::class)->standardise_response($result);
73
    }
74
 
75
    /**
76
     * Helper to inject route arguments.
77
     *
78
     * This is based on the ControllerInvoker.
79
     *
80
     * @param ServerRequestInterface $request
81
     * @param array $routeargs
82
     * @return ServerRequestInterface
83
     */
84
    private static function inject_route_arguments(
85
        ServerRequestInterface $request,
86
        array $routeargs,
87
    ): ServerRequestInterface {
88
        $args = $request;
89
        foreach ($routeargs as $key => $value) {
90
            // Note: This differs to upstream where route args always override attributes.
91
            // We apply mapped parameters via route attributes and must therefore override the route args.
92
            if (!$args->getAttribute($key)) {
93
                $args = $args->withAttribute($key, $value);
94
            }
95
        }
96
        return $args;
97
    }
98
}