xref: /webtrees/app/Http/ViewResponseTrait.php (revision a612ab9f45ad6c797765d82d1da45558735bf42e)
1606471d5SGreg Roach<?php
23976b470SGreg Roach
3606471d5SGreg Roach/**
4606471d5SGreg Roach * webtrees: online genealogy
5*a612ab9fSGreg Roach * Copyright (C) 2021 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
15606471d5SGreg Roach * along with this program. If not, see <http://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;
23606471d5SGreg Roachuse Psr\Http\Message\ResponseInterface;
24ef5d23f1SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
253976b470SGreg Roach
26606471d5SGreg Roachuse function response;
27606471d5SGreg Roachuse function view;
28606471d5SGreg Roach
29606471d5SGreg Roach/**
30606471d5SGreg Roach * Allows a page fragment to be embedded in a page layout and converted to an HTTP response.
31606471d5SGreg Roach * Used by controllers, request-handlers, modules, etc.
32606471d5SGreg Roach */
33606471d5SGreg Roachtrait ViewResponseTrait
34606471d5SGreg Roach{
35982e6c55SGreg Roach    /** @var string */
361c69478cSGreg Roach    protected $layout = 'layouts/default';
371c69478cSGreg Roach
38606471d5SGreg Roach    /**
39606471d5SGreg Roach     * @param string  $view_name
40606471d5SGreg Roach     * @param 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);
52ef5d23f1SGreg Roach        $layout_data['request'] = app(ServerRequestInterface::class);
53606471d5SGreg Roach
54606471d5SGreg Roach        // Insert the view into the layout
55*a612ab9fSGreg Roach        $html = view($this->layout, $layout_data);
56606471d5SGreg Roach
57606471d5SGreg Roach        return response($html, $status);
58606471d5SGreg Roach    }
59606471d5SGreg Roach}
60