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 core\exception\invalid_parameter_exception;
20
use core\exception\response_aware_exception;
21
use core\router;
22
use core\router\response\exception_response;
23
use core\router\response\invalid_parameter_response;
24
use core\router\schema\response\response_type;
25
use Psr\Container\ContainerInterface;
26
use Psr\Http\Message\ResponseInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
 
29
/**
30
 * Controller Invoker for the Moodle Router.
31
 *
32
 * This class handles invocation of the route callable, and the conversion of the response into an appropriate format.
33
 *
34
 * @package    core
35
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class response_handler {
39
    /**
40
     * Create a new response handler.
41
     *
42
     * @param ContainerInterface $container
43
     */
44
    public function __construct(
45
        /** @var ContainerInterface */
46
        private readonly ContainerInterface $container,
47
    ) {
48
    }
49
 
50
    /**
51
     * Invoke a route callable.
52
     *
53
     * Note: Much of this is copied from the parent class, but we need to handle the response differently.
54
     *
55
     * @param ResponseInterface|response_type $response The response object.
56
     * @return ResponseInterface The response from the callable.
57
     */
58
    public function standardise_response(
59
        ResponseInterface | response_type $response,
60
    ): ResponseInterface {
61
        if ($response instanceof ResponseInterface) {
62
            // An object implementing ResponseInterface is returned, so we can just return it.
63
            return $response;
64
        }
65
 
66
        $responsefactory = $this->container->get(router::class)->get_response_factory();
67
 
68
        // This must be a response\response_type.
69
        return $response->get_response($responsefactory);
70
    }
71
 
72
    /**
73
     * Get the response from an exception.
74
     *
75
     * @param ServerRequestInterface $request
76
     * @param \Exception $exception
77
     *
78
     * @return ResponseInterface
79
     */
80
    public function get_response_from_exception(
81
        ServerRequestInterface $request,
82
        \Exception $exception,
83
    ): ResponseInterface {
84
        $response = match (true) {
85
            // Newer exceptions may be response-aware, so we can use the response class they specify.
86
            (is_a($exception, response_aware_exception::class)) => $exception->get_response_classname()::get_response(
87
                $request,
88
                $exception,
89
            ),
90
 
91
            // Some legacy expressions are here for the moment.
92
            is_a($exception, invalid_parameter_exception::class) => invalid_parameter_response::get_response(
93
                $request,
94
                $exception,
95
            ),
96
 
97
            // Otherwise use the default.
98
            default => exception_response::get_response($request, $exception),
99
        };
100
 
101
        return $this->standardise_response($response);
102
    }
103
}