xref: /webtrees/app/Helpers/functions.php (revision 8121b9bec19818120092699199161a1357bb8f3f)
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;
20use Illuminate\Cache\Repository;
21
22/**
23 * Get the IoC container, or fetch something from it.
24 *
25 * @param string|null $abstract
26 *
27 * @return Application|Repository|mixed
28 */
29function app(string $abstract = null)
30{
31    if ($abstract === null) {
32        return Application::getInstance();
33    } else {
34        return Application::getInstance()->make($abstract);
35    }
36}
37
38/**
39 * Generate a CSRF token form field.
40 *
41 * @return string
42 */
43function csrf_field()
44{
45    return '<input type="hidden" name="csrf" value="' . e(\Fisharebest\Webtrees\Session::getCsrfToken()) . '">';
46}
47
48/**
49 * Get the CSRF token value.
50 *
51 * @return string
52 */
53function csrf_token()
54{
55    return \Fisharebest\Webtrees\Session::getCsrfToken();
56}
57
58/**
59 * Generate a URL for a named route.
60 *
61 * @param string $route
62 * @param array  $parameters
63 * @param bool   $absolute
64 *
65 * @return string
66 */
67function route(string $route, array $parameters = [], bool $absolute = true): string
68{
69    $parameters = ['route' => $route] + $parameters;
70
71    if ($absolute) {
72        return \Fisharebest\Webtrees\Html::url(WT_BASE_URL . 'index.php', $parameters);
73    }
74
75    return \Fisharebest\Webtrees\Html::url('index.php', $parameters);
76}
77
78/**
79 * Cerate and render a view in a single operation.
80 *
81 * @param string  $name
82 * @param mixed[] $data
83 *
84 * @return string
85 */
86function view(string $name, array $data = [])
87{
88    return \Fisharebest\Webtrees\View::make($name, $data);
89}
90