xref: /webtrees/app/Helpers/functions.php (revision 8b67c11a1199191915b4af08a3841e7ce9d528b6)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program 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 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17declare(strict_types=1);
18
19use Fisharebest\Webtrees\Application;
20
21/**
22 * Get the IoC container, or fetch something from it.
23 *
24 * @param string|null $abstract
25 *
26 * @return Application|mixed
27 */
28function app(string $abstract = null)
29{
30    if ($abstract === null) {
31        return Application::getInstance();
32    } else {
33        return Application::getInstance()->make($abstract);
34    }
35}
36
37/**
38 * Generate a CSRF token form field.
39 *
40 * @return string
41 */
42function csrf_field()
43{
44    return '<input type="hidden" name="csrf" value="' . e(\Fisharebest\Webtrees\Session::getCsrfToken()) . '">';
45}
46
47/**
48 * Get the CSRF token value.
49 *
50 * @return string
51 */
52function csrf_token()
53{
54    return \Fisharebest\Webtrees\Session::getCsrfToken();
55}
56
57/**
58 * Generate a URL for a named route.
59 *
60 * @param string $route
61 * @param array  $parameters
62 * @param bool   $absolute
63 *
64 * @return string
65 */
66function route(string $route, array $parameters = [], bool $absolute = true): string
67{
68    $parameters = ['route' => $route] + $parameters;
69
70    if ($absolute) {
71        return \Fisharebest\Webtrees\Html::url(WT_BASE_URL . 'index.php', $parameters);
72    }
73
74    return \Fisharebest\Webtrees\Html::url('index.php', $parameters);
75}
76
77/**
78 * Cerate and render a view in a single operation.
79 *
80 * @param string  $name
81 * @param mixed[] $data
82 *
83 * @return string
84 */
85function view(string $name, array $data = [])
86{
87    return \Fisharebest\Webtrees\View::make($name, $data);
88}
89