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\schema\response;
18
 
19
use GuzzleHttp\Psr7\Utils;
20
use Psr\Http\Message\ResponseFactoryInterface;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
 
24
/**
25
 * A response which will render the specified template.
26
 *
27
 * This approach is inspired and based upon slim-routing https://github.com/juliangut/slim-routing
28
 * We only need a fraction of this functionality.
29
 *
30
 * @package    core
31
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class view_response extends abstract_response {
35
    /**
36
     * Create a new view response.
37
     *
38
     * @param string $template The template name
39
     * @param array $parameters The parameters to pass
40
     * @param ServerRequestInterface $request The request
41
     * @param ResponseInterface|null $response The response
42
     */
43
    public function __construct(
44
        /** @var string The template name */
45
        private readonly string $template,
46
        /** @var array The parameters to pass */
47
        private readonly array $parameters,
48
        ServerRequestInterface $request,
49
        ?ResponseInterface $response = null,
50
    ) {
51
        parent::__construct($request, $response);
52
    }
53
 
54
    /**
55
     * Get the template name.
56
     *
57
     * @return string
58
     */
59
    public function get_template_name(): string {
60
        return $this->template;
61
    }
62
 
63
    /**
64
     * Get the parameters.
65
     *
66
     * @return array
67
     */
68
    public function get_parameters(): array {
69
        return $this->parameters;
70
    }
71
 
72
    #[\Override]
73
    public function get_response(
74
        ResponseFactoryInterface $responsefactory,
75
    ): ?ResponseInterface {
76
        global $OUTPUT;
77
 
78
        $response = parent::get_response($responsefactory);
79
        return $response
80
            ->withHeader('Content-Type', 'text/html; charset=utf-8')
81
            ->withBody(Utils::streamFor(
82
                $OUTPUT->render_from_template(
83
                    $this->get_template_name(),
84
                    $this->get_parameters(),
85
                ),
86
            ));
87
    }
88
}