xref: /webtrees/app/Http/ViewResponseTrait.php (revision d35568b467207589ea9059739da0bf1f7e785a0d)
1606471d5SGreg Roach<?php
23976b470SGreg Roach
3606471d5SGreg Roach/**
4606471d5SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6606471d5SGreg Roach * This program is free software: you can redistribute it and/or modify
7606471d5SGreg Roach * it under the terms of the GNU General Public License as published by
8606471d5SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9606471d5SGreg Roach * (at your option) any later version.
10606471d5SGreg Roach * This program is distributed in the hope that it will be useful,
11606471d5SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12606471d5SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13606471d5SGreg Roach * GNU General Public License for more details.
14606471d5SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16606471d5SGreg Roach */
17fcfa147eSGreg Roach
18606471d5SGreg Roachdeclare(strict_types=1);
19606471d5SGreg Roach
20606471d5SGreg Roachnamespace Fisharebest\Webtrees\Http;
21606471d5SGreg Roach
22606471d5SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23*d35568b4SGreg Roachuse Fisharebest\Webtrees\Registry;
24606471d5SGreg Roachuse Psr\Http\Message\ResponseInterface;
25ef5d23f1SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
263976b470SGreg Roach
27606471d5SGreg Roachuse function response;
28606471d5SGreg Roachuse function view;
29606471d5SGreg Roach
30606471d5SGreg Roach/**
31606471d5SGreg Roach * Allows a page fragment to be embedded in a page layout and converted to an HTTP response.
32606471d5SGreg Roach * Used by controllers, request-handlers, modules, etc.
33606471d5SGreg Roach */
34606471d5SGreg Roachtrait ViewResponseTrait
35606471d5SGreg Roach{
36c4943cffSGreg Roach    protected string $layout = 'layouts/default';
371c69478cSGreg Roach
38606471d5SGreg Roach    /**
39606471d5SGreg Roach     * @param string       $view_name
4009482a55SGreg Roach     * @param array<mixed> $view_data
41606471d5SGreg Roach     * @param int          $status
42606471d5SGreg Roach     *
43606471d5SGreg Roach     * @return ResponseInterface
44606471d5SGreg Roach     */
45970b132cSGreg Roach    protected function viewResponse(string $view_name, array $view_data, int $status = StatusCodeInterface::STATUS_OK): ResponseInterface
46606471d5SGreg Roach    {
47606471d5SGreg Roach        // Make the view's data available to the layout.
48606471d5SGreg Roach        $layout_data = $view_data;
49606471d5SGreg Roach
50606471d5SGreg Roach        // Render the view
51606471d5SGreg Roach        $layout_data['content'] = view($view_name, $view_data);
52*d35568b4SGreg Roach        $layout_data['request'] = Registry::container()->get(ServerRequestInterface::class);
53606471d5SGreg Roach
54606471d5SGreg Roach        // Insert the view into the layout
55a612ab9fSGreg Roach        $html = view($this->layout, $layout_data);
56606471d5SGreg Roach
57606471d5SGreg Roach        return response($html, $status);
58606471d5SGreg Roach    }
59606471d5SGreg Roach}
60