xref: /webtrees/app/View.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
1f3281ad6SGreg Roach<?php
2*3976b470SGreg Roach
3f3281ad6SGreg Roach/**
4f3281ad6SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6f3281ad6SGreg Roach * This program is free software: you can redistribute it and/or modify
7f3281ad6SGreg Roach * it under the terms of the GNU General Public License as published by
8f3281ad6SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9f3281ad6SGreg Roach * (at your option) any later version.
10f3281ad6SGreg Roach * This program is distributed in the hope that it will be useful,
11f3281ad6SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12f3281ad6SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13f3281ad6SGreg Roach * GNU General Public License for more details.
14f3281ad6SGreg Roach * You should have received a copy of the GNU General Public License
15f3281ad6SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16f3281ad6SGreg Roach */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
19f3281ad6SGreg Roachnamespace Fisharebest\Webtrees;
20f3281ad6SGreg Roach
2150c68a25SGreg Roachuse Exception;
223e4bf26fSGreg Roachuse Illuminate\Support\Str;
233e4bf26fSGreg Roachuse InvalidArgumentException;
243e4bf26fSGreg Roachuse RuntimeException;
2570f31542SGreg Roachuse Throwable;
26*3976b470SGreg Roach
273e4bf26fSGreg Roachuse function array_key_exists;
283e4bf26fSGreg Roachuse function explode;
29d7952a34SGreg Roachuse function extract;
30d7952a34SGreg Roachuse function implode;
31d7952a34SGreg Roachuse function is_file;
32d7952a34SGreg Roachuse function ob_end_clean;
33d3da353bSGreg Roachuse function ob_get_level;
34d7952a34SGreg Roachuse function ob_start;
35d7952a34SGreg Roachuse function sha1;
3650c68a25SGreg Roach
37f3281ad6SGreg Roach/**
38f3281ad6SGreg Roach * Simple view/template class.
39f3281ad6SGreg Roach */
40c1010edaSGreg Roachclass View
41c1010edaSGreg Roach{
423e4bf26fSGreg Roach    public const NAMESPACE_SEPARATOR = '::';
43dd6b2bfcSGreg Roach
44dd6b2bfcSGreg Roach    // File extension for our template files.
4516d6367aSGreg Roach    private const TEMPLATE_EXTENSION = '.phtml';
46dd6b2bfcSGreg Roach
47f3281ad6SGreg Roach    /**
48f3281ad6SGreg Roach     * @var string The (file) name of the view.
49f3281ad6SGreg Roach     */
50f3281ad6SGreg Roach    private $name;
51f3281ad6SGreg Roach
52f3281ad6SGreg Roach    /**
53f3281ad6SGreg Roach     * @var mixed[] Data to be inserted into the view.
54f3281ad6SGreg Roach     */
55f3281ad6SGreg Roach    private $data;
56f3281ad6SGreg Roach
57f3281ad6SGreg Roach    /**
583e4bf26fSGreg Roach     * @var string[] Where do the templates live, for each namespace.
593e4bf26fSGreg Roach     */
603e4bf26fSGreg Roach    private static $namespaces = [
61f397d0fdSGreg Roach        '' => Webtrees::ROOT_DIR . 'resources/views/',
623e4bf26fSGreg Roach    ];
633e4bf26fSGreg Roach
643e4bf26fSGreg Roach    /**
653e4bf26fSGreg Roach     * @var string[] Modules can replace core views with their own.
663e4bf26fSGreg Roach     */
673e4bf26fSGreg Roach    private static $replacements = [];
683e4bf26fSGreg Roach
693e4bf26fSGreg Roach    /**
7008d24c7aSGreg Roach     * @var mixed[] Data to be inserted into all views.
7108d24c7aSGreg Roach     */
7208d24c7aSGreg Roach    private static $shared_data = [];
7308d24c7aSGreg Roach
7408d24c7aSGreg Roach    /**
75ecf66805SGreg Roach     * @var string Implementation of Blade "stacks".
76ecf66805SGreg Roach     */
77ecf66805SGreg Roach    private static $stack;
78ecf66805SGreg Roach
79ecf66805SGreg Roach    /**
80ecf66805SGreg Roach     * @var array[] Implementation of Blade "stacks".
81ecf66805SGreg Roach     */
82ecf66805SGreg Roach    private static $stacks = [];
83ecf66805SGreg Roach
84ecf66805SGreg Roach    /**
85f3281ad6SGreg Roach     * Createa view from a template name and optional data.
86f3281ad6SGreg Roach     *
87fa4036e8SGreg Roach     * @param string $name
88f3281ad6SGreg Roach     * @param array  $data
89f3281ad6SGreg Roach     */
90fa4036e8SGreg Roach    public function __construct(string $name, $data = [])
91c1010edaSGreg Roach    {
92f3281ad6SGreg Roach        $this->name = $name;
93f3281ad6SGreg Roach        $this->data = $data;
94f3281ad6SGreg Roach    }
95f3281ad6SGreg Roach
96f3281ad6SGreg Roach    /**
9708d24c7aSGreg Roach     * Shared data that is available to all views.
98962e29c9SGreg Roach     *
99962e29c9SGreg Roach     * @param string $key
100962e29c9SGreg Roach     * @param mixed  $value
101fa4036e8SGreg Roach     *
102fa4036e8SGreg Roach     * @return void
10308d24c7aSGreg Roach     */
104e364afe4SGreg Roach    public static function share(string $key, $value): void
105c1010edaSGreg Roach    {
10608d24c7aSGreg Roach        self::$shared_data[$key] = $value;
10708d24c7aSGreg Roach    }
10808d24c7aSGreg Roach
10908d24c7aSGreg Roach    /**
110ecf66805SGreg Roach     * Implementation of Blade "stacks".
111ecf66805SGreg Roach     *
112ecf66805SGreg Roach     * @see https://laravel.com/docs/5.5/blade#stacks
113962e29c9SGreg Roach     *
114962e29c9SGreg Roach     * @param string $stack
115fa4036e8SGreg Roach     *
116fa4036e8SGreg Roach     * @return void
117ecf66805SGreg Roach     */
118e364afe4SGreg Roach    public static function push(string $stack): void
119c1010edaSGreg Roach    {
120ecf66805SGreg Roach        self::$stack = $stack;
121d7952a34SGreg Roach
122ecf66805SGreg Roach        ob_start();
123ecf66805SGreg Roach    }
124ecf66805SGreg Roach
125ecf66805SGreg Roach    /**
126ecf66805SGreg Roach     * Implementation of Blade "stacks".
127fa4036e8SGreg Roach     *
128fa4036e8SGreg Roach     * @return void
129ecf66805SGreg Roach     */
130e364afe4SGreg Roach    public static function endpush(): void
131c1010edaSGreg Roach    {
13288de55fdSRico Sonntag        $content = ob_get_clean();
13388de55fdSRico Sonntag
134d7952a34SGreg Roach        self::$stacks[self::$stack][] = $content;
135d7952a34SGreg Roach    }
136d7952a34SGreg Roach
137d7952a34SGreg Roach    /**
138d7952a34SGreg Roach     * Variant of push that will only add one copy of each item.
139d7952a34SGreg Roach     *
140d7952a34SGreg Roach     * @param string $stack
141d7952a34SGreg Roach     *
142d7952a34SGreg Roach     * @return void
143d7952a34SGreg Roach     */
144e364afe4SGreg Roach    public static function pushunique(string $stack): void
145d7952a34SGreg Roach    {
146d7952a34SGreg Roach        self::$stack = $stack;
147d7952a34SGreg Roach
148d7952a34SGreg Roach        ob_start();
149d7952a34SGreg Roach    }
150d7952a34SGreg Roach
151d7952a34SGreg Roach    /**
152d7952a34SGreg Roach     * Variant of push that will only add one copy of each item.
153d7952a34SGreg Roach     *
154d7952a34SGreg Roach     * @return void
155d7952a34SGreg Roach     */
156e364afe4SGreg Roach    public static function endpushunique(): void
157d7952a34SGreg Roach    {
158d7952a34SGreg Roach        $content = ob_get_clean();
159d7952a34SGreg Roach
160d7952a34SGreg Roach        self::$stacks[self::$stack][sha1($content)] = $content;
161ecf66805SGreg Roach    }
162ecf66805SGreg Roach
163ecf66805SGreg Roach    /**
164ecf66805SGreg Roach     * Implementation of Blade "stacks".
165ecf66805SGreg Roach     *
166962e29c9SGreg Roach     * @param string $stack
167962e29c9SGreg Roach     *
168ecf66805SGreg Roach     * @return string
169ecf66805SGreg Roach     */
170c1010edaSGreg Roach    public static function stack(string $stack): string
171c1010edaSGreg Roach    {
172ecf66805SGreg Roach        $content = implode('', self::$stacks[$stack] ?? []);
173ecf66805SGreg Roach
174ecf66805SGreg Roach        self::$stacks[$stack] = [];
175ecf66805SGreg Roach
176ecf66805SGreg Roach        return $content;
177ecf66805SGreg Roach    }
178ecf66805SGreg Roach
179ecf66805SGreg Roach    /**
180f3281ad6SGreg Roach     * Render a view.
181f3281ad6SGreg Roach     *
182f3281ad6SGreg Roach     * @return string
18370f31542SGreg Roach     * @throws Throwable
184f3281ad6SGreg Roach     */
1858f53f488SRico Sonntag    public function render(): string
186c1010edaSGreg Roach    {
1874a86d714SGreg Roach        $variables_for_view = $this->data + self::$shared_data;
188e364afe4SGreg Roach        extract($variables_for_view, EXTR_SKIP);
189f3281ad6SGreg Roach
19070f31542SGreg Roach        try {
191f3281ad6SGreg Roach            ob_start();
192bc241c54SGreg Roach            // Do not use require, so we can catch errors for missing files
193bc241c54SGreg Roach            include $this->getFilenameForView($this->name);
19475d70144SGreg Roach
195f3281ad6SGreg Roach            return ob_get_clean();
19670f31542SGreg Roach        } catch (Throwable $ex) {
197d3da353bSGreg Roach            while (ob_get_level() > 0) {
19870f31542SGreg Roach                ob_end_clean();
199d3da353bSGreg Roach            }
20070f31542SGreg Roach            throw $ex;
20170f31542SGreg Roach        }
202f3281ad6SGreg Roach    }
203f3281ad6SGreg Roach
204f3281ad6SGreg Roach    /**
2053e4bf26fSGreg Roach     * @param string $namespace
2063e4bf26fSGreg Roach     * @param string $path
2073e4bf26fSGreg Roach     *
2083e4bf26fSGreg Roach     * @throws InvalidArgumentException
2093e4bf26fSGreg Roach     */
2103e4bf26fSGreg Roach    public static function registerNamespace(string $namespace, string $path): void
2113e4bf26fSGreg Roach    {
2123e4bf26fSGreg Roach        if ($namespace === '') {
2133e4bf26fSGreg Roach            throw new InvalidArgumentException('Cannot register the default namespace');
2143e4bf26fSGreg Roach        }
2153e4bf26fSGreg Roach
2163e4bf26fSGreg Roach        if (!Str::endsWith($path, '/')) {
2173e4bf26fSGreg Roach            throw new InvalidArgumentException('Paths must end with a directory separator');
2183e4bf26fSGreg Roach        }
2193e4bf26fSGreg Roach
2203e4bf26fSGreg Roach        self::$namespaces[$namespace] = $path;
2213e4bf26fSGreg Roach    }
2223e4bf26fSGreg Roach
2233e4bf26fSGreg Roach    /**
2243e4bf26fSGreg Roach     * @param string $old
2253e4bf26fSGreg Roach     * @param string $new
2263e4bf26fSGreg Roach     *
2273e4bf26fSGreg Roach     * @throws InvalidArgumentException
2283e4bf26fSGreg Roach     */
2293e4bf26fSGreg Roach    public static function registerCustomView(string $old, string $new): void
2303e4bf26fSGreg Roach    {
2313e4bf26fSGreg Roach        if (Str::contains($old, self::NAMESPACE_SEPARATOR) && Str::contains($new, self::NAMESPACE_SEPARATOR)) {
2323e4bf26fSGreg Roach            self::$replacements[$old] = $new;
2333e4bf26fSGreg Roach        } else {
2343e4bf26fSGreg Roach            throw new InvalidArgumentException();
2353e4bf26fSGreg Roach        }
2363e4bf26fSGreg Roach    }
2373e4bf26fSGreg Roach
2383e4bf26fSGreg Roach    /**
2393e4bf26fSGreg Roach     * Find the file for a view.
240f3281ad6SGreg Roach     *
241f3281ad6SGreg Roach     * @param string $view_name
242f3281ad6SGreg Roach     *
24375d70144SGreg Roach     * @return string
24450c68a25SGreg Roach     * @throws Exception
245f3281ad6SGreg Roach     */
2463e4bf26fSGreg Roach    public function getFilenameForView(string $view_name): string
247c1010edaSGreg Roach    {
248fdbcd0efSGreg Roach        // If we request "::view", then use it explicityly.  Don't allow replacements.
249fdbcd0efSGreg Roach        $explicit = Str::startsWith($view_name, self::NAMESPACE_SEPARATOR);
250fdbcd0efSGreg Roach
2513e4bf26fSGreg Roach        if (!Str::contains($view_name, self::NAMESPACE_SEPARATOR)) {
2523e4bf26fSGreg Roach            $view_name = self::NAMESPACE_SEPARATOR . $view_name;
2533e4bf26fSGreg Roach        }
25475d70144SGreg Roach
2553e4bf26fSGreg Roach        // Apply replacements / customisations
256fdbcd0efSGreg Roach        while (!$explicit && array_key_exists($view_name, self::$replacements)) {
2573e4bf26fSGreg Roach            $view_name = self::$replacements[$view_name];
2583e4bf26fSGreg Roach        }
2593e4bf26fSGreg Roach
2603e4bf26fSGreg Roach        [$namespace, $view_name] = explode(self::NAMESPACE_SEPARATOR, $view_name, 2);
2613e4bf26fSGreg Roach
2623e4bf26fSGreg Roach        if ((self::$namespaces[$namespace] ?? null) === null) {
2633e4bf26fSGreg Roach            throw new RuntimeException('Namespace "' . e($namespace) .  '" not found.');
2643e4bf26fSGreg Roach        }
2653e4bf26fSGreg Roach
2663e4bf26fSGreg Roach        $view_file = self::$namespaces[$namespace] . $view_name . self::TEMPLATE_EXTENSION;
2673e4bf26fSGreg Roach
2683e4bf26fSGreg Roach        if (!is_file($view_file)) {
2693e4bf26fSGreg Roach            throw new RuntimeException('View file not found: ' . e($view_file));
2703e4bf26fSGreg Roach        }
2713e4bf26fSGreg Roach
27250c68a25SGreg Roach        return $view_file;
273f21917b2SGreg Roach    }
27450c68a25SGreg Roach
275f3281ad6SGreg Roach    /**
276f3281ad6SGreg Roach     * Cerate and render a view in a single operation.
277f3281ad6SGreg Roach     *
278f3281ad6SGreg Roach     * @param string  $name
279f3281ad6SGreg Roach     * @param mixed[] $data
280f3281ad6SGreg Roach     *
281f3281ad6SGreg Roach     * @return string
282f3281ad6SGreg Roach     */
2838f53f488SRico Sonntag    public static function make($name, $data = []): string
284c1010edaSGreg Roach    {
285f3281ad6SGreg Roach        $view = new static($name, $data);
286f3281ad6SGreg Roach
28744116e73SGreg Roach        DebugBar::addView($name, $data);
28844116e73SGreg Roach
289f3281ad6SGreg Roach        return $view->render();
290f3281ad6SGreg Roach    }
291f3281ad6SGreg Roach}
292