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\middleware;
18
 
19
use core\router\request_validator_interface;
20
use core\router\response_handler;
21
use core\router\response_validator_interface;
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\ServerRequestInterface;
24
use Psr\Http\Server\MiddlewareInterface;
25
use Psr\Http\Server\RequestHandlerInterface;
26
 
27
/**
28
 * Middleware to handle validation of request and response based on the route data.
29
 *
30
 * @package    core
31
 * @copyright  2024 Andrew Lyons <andrew@nicols.co.uk>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class validation_middleware implements MiddlewareInterface {
35
    /**
36
     * Create a new instance of the validation middleware.
37
     *
38
     * @param response_handler $responsehandler A handler to standardise a response
39
     * @param request_validator_interface $requestvalidator A request validator
40
     * @param response_validator_interface $responsevalidator A response validator
41
     */
42
    public function __construct(
43
        /** @var response_handler A handler to standardise a response */
44
        protected response_handler $responsehandler,
45
 
46
        /** @var request_validator_interface The request validator used to validate incoming data */
47
        protected request_validator_interface $requestvalidator,
48
 
49
        /** @var response_validator_interface The response validator used to validate incoming data */
50
        protected response_validator_interface $responsevalidator,
51
    ) {
52
    }
53
 
54
    #[\Override]
55
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
56
        try {
57
            $request = $this->requestvalidator->validate_request($request);
58
        } catch (\Exception $e) {
59
            return $this->responsehandler->get_response_from_exception($request, $e);
60
        }
61
 
62
        $response = $handler->handle($request);
63
 
64
        try {
65
            $this->responsevalidator->validate_response($request, $response);
66
        } catch (\Exception $e) {
67
            return $this->responsehandler->get_response_from_exception($request, $e);
68
        }
69
 
70
        return $response;
71
    }
72
}