xref: /webtrees/app/View.php (revision ac701fbd36864ec7630310142b9b16f78934a505)
1f3281ad6SGreg Roach<?php
23976b470SGreg Roach
3f3281ad6SGreg Roach/**
4f3281ad6SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16f3281ad6SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
20f3281ad6SGreg Roachnamespace Fisharebest\Webtrees;
21f3281ad6SGreg Roach
2250c68a25SGreg Roachuse Exception;
233e4bf26fSGreg Roachuse InvalidArgumentException;
24e50f5fc6SGreg Roachuse LogicException;
253e4bf26fSGreg Roachuse RuntimeException;
2670f31542SGreg Roachuse Throwable;
273976b470SGreg Roach
283e4bf26fSGreg Roachuse function array_key_exists;
293e4bf26fSGreg Roachuse function explode;
30d7952a34SGreg Roachuse function extract;
31d7952a34SGreg Roachuse function implode;
32d7952a34SGreg Roachuse function is_file;
33d7952a34SGreg Roachuse function ob_end_clean;
34d3da353bSGreg Roachuse function ob_get_level;
35d7952a34SGreg Roachuse function ob_start;
36d7952a34SGreg Roachuse function sha1;
37dec352c1SGreg Roachuse function str_contains;
38dec352c1SGreg Roachuse function str_ends_with;
39721424feSGreg Roachuse function strlen;
40721424feSGreg Roachuse function strncmp;
4150c68a25SGreg Roach
42ef5d23f1SGreg Roachuse const EXTR_OVERWRITE;
43ef5d23f1SGreg Roach
44f3281ad6SGreg Roach/**
45f3281ad6SGreg Roach * Simple view/template class.
46f3281ad6SGreg Roach */
47c1010edaSGreg Roachclass View
48c1010edaSGreg Roach{
493e4bf26fSGreg Roach    public const NAMESPACE_SEPARATOR = '::';
50dd6b2bfcSGreg Roach
51dd6b2bfcSGreg Roach    // File extension for our template files.
5216d6367aSGreg Roach    private const TEMPLATE_EXTENSION = '.phtml';
53dd6b2bfcSGreg Roach
54f3281ad6SGreg Roach    /**
55f3281ad6SGreg Roach     * @var string The (file) name of the view.
56f3281ad6SGreg Roach     */
57f3281ad6SGreg Roach    private $name;
58f3281ad6SGreg Roach
59f3281ad6SGreg Roach    /**
60f3281ad6SGreg Roach     * @var mixed[] Data to be inserted into the view.
61f3281ad6SGreg Roach     */
62f3281ad6SGreg Roach    private $data;
63f3281ad6SGreg Roach
64f3281ad6SGreg Roach    /**
653e4bf26fSGreg Roach     * @var string[] Where do the templates live, for each namespace.
663e4bf26fSGreg Roach     */
673e4bf26fSGreg Roach    private static $namespaces = [
68f397d0fdSGreg Roach        '' => Webtrees::ROOT_DIR . 'resources/views/',
693e4bf26fSGreg Roach    ];
703e4bf26fSGreg Roach
713e4bf26fSGreg Roach    /**
723e4bf26fSGreg Roach     * @var string[] Modules can replace core views with their own.
733e4bf26fSGreg Roach     */
743e4bf26fSGreg Roach    private static $replacements = [];
753e4bf26fSGreg Roach
763e4bf26fSGreg Roach    /**
77ecf66805SGreg Roach     * @var string Implementation of Blade "stacks".
78ecf66805SGreg Roach     */
79ecf66805SGreg Roach    private static $stack;
80ecf66805SGreg Roach
81ecf66805SGreg Roach    /**
82ecf66805SGreg Roach     * @var array[] Implementation of Blade "stacks".
83ecf66805SGreg Roach     */
84ecf66805SGreg Roach    private static $stacks = [];
85ecf66805SGreg Roach
86ecf66805SGreg Roach    /**
87f3281ad6SGreg Roach     * Create a view from a template name and optional data.
88f3281ad6SGreg Roach     *
89fa4036e8SGreg Roach     * @param string       $name
905d4b7ec2SGreg Roach     * @param array<mixed> $data
91f3281ad6SGreg Roach     */
92fa4036e8SGreg Roach    public function __construct(string $name, $data = [])
93c1010edaSGreg Roach    {
94f3281ad6SGreg Roach        $this->name = $name;
95f3281ad6SGreg Roach        $this->data = $data;
96f3281ad6SGreg Roach    }
97f3281ad6SGreg Roach
98f3281ad6SGreg Roach    /**
99ecf66805SGreg Roach     * Implementation of Blade "stacks".
100ecf66805SGreg Roach     *
101ecf66805SGreg Roach     * @see https://laravel.com/docs/5.5/blade#stacks
102962e29c9SGreg Roach     *
103962e29c9SGreg Roach     * @param string $stack
104fa4036e8SGreg Roach     *
105fa4036e8SGreg Roach     * @return void
106ecf66805SGreg Roach     */
107e364afe4SGreg Roach    public static function push(string $stack): void
108c1010edaSGreg Roach    {
109ecf66805SGreg Roach        self::$stack = $stack;
110d7952a34SGreg Roach
111ecf66805SGreg Roach        ob_start();
112ecf66805SGreg Roach    }
113ecf66805SGreg Roach
114ecf66805SGreg Roach    /**
115ecf66805SGreg Roach     * Implementation of Blade "stacks".
116fa4036e8SGreg Roach     *
117fa4036e8SGreg Roach     * @return void
118ecf66805SGreg Roach     */
119e364afe4SGreg Roach    public static function endpush(): void
120c1010edaSGreg Roach    {
12188de55fdSRico Sonntag        $content = ob_get_clean();
12288de55fdSRico Sonntag
123e50f5fc6SGreg Roach        if ($content === false) {
124e50f5fc6SGreg Roach            throw new LogicException('found endpush(), but did not find push()');
125e50f5fc6SGreg Roach        }
126e50f5fc6SGreg Roach
127d7952a34SGreg Roach        self::$stacks[self::$stack][] = $content;
128d7952a34SGreg Roach    }
129d7952a34SGreg Roach
130d7952a34SGreg Roach    /**
131d7952a34SGreg Roach     * Variant of push that will only add one copy of each item.
132d7952a34SGreg Roach     *
133d7952a34SGreg Roach     * @param string $stack
134d7952a34SGreg Roach     *
135d7952a34SGreg Roach     * @return void
136d7952a34SGreg Roach     */
137e364afe4SGreg Roach    public static function pushunique(string $stack): void
138d7952a34SGreg Roach    {
139d7952a34SGreg Roach        self::$stack = $stack;
140d7952a34SGreg Roach
141d7952a34SGreg Roach        ob_start();
142d7952a34SGreg Roach    }
143d7952a34SGreg Roach
144d7952a34SGreg Roach    /**
145d7952a34SGreg Roach     * Variant of push that will only add one copy of each item.
146d7952a34SGreg Roach     *
147d7952a34SGreg Roach     * @return void
148d7952a34SGreg Roach     */
149e364afe4SGreg Roach    public static function endpushunique(): void
150d7952a34SGreg Roach    {
151d7952a34SGreg Roach        $content = ob_get_clean();
152d7952a34SGreg Roach
153e50f5fc6SGreg Roach        if ($content === false) {
154e50f5fc6SGreg Roach            throw new LogicException('found endpushunique(), but did not find pushunique()');
155e50f5fc6SGreg Roach        }
156e50f5fc6SGreg Roach
157d7952a34SGreg Roach        self::$stacks[self::$stack][sha1($content)] = $content;
158ecf66805SGreg Roach    }
159ecf66805SGreg Roach
160ecf66805SGreg Roach    /**
161ecf66805SGreg Roach     * Implementation of Blade "stacks".
162ecf66805SGreg Roach     *
163962e29c9SGreg Roach     * @param string $stack
164962e29c9SGreg Roach     *
165ecf66805SGreg Roach     * @return string
166ecf66805SGreg Roach     */
167c1010edaSGreg Roach    public static function stack(string $stack): string
168c1010edaSGreg Roach    {
169ecf66805SGreg Roach        $content = implode('', self::$stacks[$stack] ?? []);
170ecf66805SGreg Roach
171ecf66805SGreg Roach        self::$stacks[$stack] = [];
172ecf66805SGreg Roach
173ecf66805SGreg Roach        return $content;
174ecf66805SGreg Roach    }
175ecf66805SGreg Roach
176ecf66805SGreg Roach    /**
177f3281ad6SGreg Roach     * Render a view.
178f3281ad6SGreg Roach     *
179f3281ad6SGreg Roach     * @return string
18070f31542SGreg Roach     * @throws Throwable
181f3281ad6SGreg Roach     */
1828f53f488SRico Sonntag    public function render(): string
183c1010edaSGreg Roach    {
184ef5d23f1SGreg Roach        extract($this->data, EXTR_OVERWRITE);
185f3281ad6SGreg Roach
18670f31542SGreg Roach        try {
187f3281ad6SGreg Roach            ob_start();
188bc241c54SGreg Roach            // Do not use require, so we can catch errors for missing files
189bc241c54SGreg Roach            include $this->getFilenameForView($this->name);
19075d70144SGreg Roach
191*ac701fbdSGreg Roach            return (string) ob_get_clean();
19270f31542SGreg Roach        } catch (Throwable $ex) {
193d3da353bSGreg Roach            while (ob_get_level() > 0) {
19470f31542SGreg Roach                ob_end_clean();
195d3da353bSGreg Roach            }
19670f31542SGreg Roach            throw $ex;
19770f31542SGreg Roach        }
198f3281ad6SGreg Roach    }
199f3281ad6SGreg Roach
200f3281ad6SGreg Roach    /**
2013e4bf26fSGreg Roach     * @param string $namespace
2023e4bf26fSGreg Roach     * @param string $path
2033e4bf26fSGreg Roach     *
2043e4bf26fSGreg Roach     * @throws InvalidArgumentException
2053e4bf26fSGreg Roach     */
2063e4bf26fSGreg Roach    public static function registerNamespace(string $namespace, string $path): void
2073e4bf26fSGreg Roach    {
2083e4bf26fSGreg Roach        if ($namespace === '') {
2093e4bf26fSGreg Roach            throw new InvalidArgumentException('Cannot register the default namespace');
2103e4bf26fSGreg Roach        }
2113e4bf26fSGreg Roach
212dec352c1SGreg Roach        if (!str_ends_with($path, '/')) {
2133e4bf26fSGreg Roach            throw new InvalidArgumentException('Paths must end with a directory separator');
2143e4bf26fSGreg Roach        }
2153e4bf26fSGreg Roach
2163e4bf26fSGreg Roach        self::$namespaces[$namespace] = $path;
2173e4bf26fSGreg Roach    }
2183e4bf26fSGreg Roach
2193e4bf26fSGreg Roach    /**
2203e4bf26fSGreg Roach     * @param string $old
2213e4bf26fSGreg Roach     * @param string $new
2223e4bf26fSGreg Roach     *
2233e4bf26fSGreg Roach     * @throws InvalidArgumentException
2243e4bf26fSGreg Roach     */
2253e4bf26fSGreg Roach    public static function registerCustomView(string $old, string $new): void
2263e4bf26fSGreg Roach    {
227dec352c1SGreg Roach        if (str_contains($old, self::NAMESPACE_SEPARATOR) && str_contains($new, self::NAMESPACE_SEPARATOR)) {
2283e4bf26fSGreg Roach            self::$replacements[$old] = $new;
2293e4bf26fSGreg Roach        } else {
2303e4bf26fSGreg Roach            throw new InvalidArgumentException();
2313e4bf26fSGreg Roach        }
2323e4bf26fSGreg Roach    }
2333e4bf26fSGreg Roach
2343e4bf26fSGreg Roach    /**
2353e4bf26fSGreg Roach     * Find the file for a view.
236f3281ad6SGreg Roach     *
237f3281ad6SGreg Roach     * @param string $view_name
238f3281ad6SGreg Roach     *
23975d70144SGreg Roach     * @return string
24050c68a25SGreg Roach     * @throws Exception
241f3281ad6SGreg Roach     */
2423e4bf26fSGreg Roach    public function getFilenameForView(string $view_name): string
243c1010edaSGreg Roach    {
244dec352c1SGreg Roach        // If we request "::view", then use it explicitly.  Don't allow replacements.
245721424feSGreg Roach        // NOTE: cannot use str_starts_with() as it wasn't available in 2.0.6, and is called by the upgrade wizard.
246721424feSGreg Roach        $explicit = strncmp($view_name, self::NAMESPACE_SEPARATOR, strlen(self::NAMESPACE_SEPARATOR)) === 0;
247fdbcd0efSGreg Roach
248dec352c1SGreg Roach        if (!str_contains($view_name, self::NAMESPACE_SEPARATOR)) {
2493e4bf26fSGreg Roach            $view_name = self::NAMESPACE_SEPARATOR . $view_name;
2503e4bf26fSGreg Roach        }
25175d70144SGreg Roach
252dec352c1SGreg Roach        // Apply replacements / customizations
253fdbcd0efSGreg Roach        while (!$explicit && array_key_exists($view_name, self::$replacements)) {
2543e4bf26fSGreg Roach            $view_name = self::$replacements[$view_name];
2553e4bf26fSGreg Roach        }
2563e4bf26fSGreg Roach
2573e4bf26fSGreg Roach        [$namespace, $view_name] = explode(self::NAMESPACE_SEPARATOR, $view_name, 2);
2583e4bf26fSGreg Roach
2593e4bf26fSGreg Roach        if ((self::$namespaces[$namespace] ?? null) === null) {
2603e4bf26fSGreg Roach            throw new RuntimeException('Namespace "' . e($namespace) .  '" not found.');
2613e4bf26fSGreg Roach        }
2623e4bf26fSGreg Roach
2633e4bf26fSGreg Roach        $view_file = self::$namespaces[$namespace] . $view_name . self::TEMPLATE_EXTENSION;
2643e4bf26fSGreg Roach
2653e4bf26fSGreg Roach        if (!is_file($view_file)) {
2663e4bf26fSGreg Roach            throw new RuntimeException('View file not found: ' . e($view_file));
2673e4bf26fSGreg Roach        }
2683e4bf26fSGreg Roach
26950c68a25SGreg Roach        return $view_file;
270f21917b2SGreg Roach    }
27150c68a25SGreg Roach
272f3281ad6SGreg Roach    /**
273219fc02dSGreg Roach     * Create and render a view in a single operation.
274f3281ad6SGreg Roach     *
275f3281ad6SGreg Roach     * @param string  $name
276f3281ad6SGreg Roach     * @param mixed[] $data
277f3281ad6SGreg Roach     *
278f3281ad6SGreg Roach     * @return string
279f3281ad6SGreg Roach     */
28024f2a3afSGreg Roach    public static function make(string $name, array $data = []): string
281c1010edaSGreg Roach    {
2821e752ebbSGreg Roach        $view = new self($name, $data);
283f3281ad6SGreg Roach
28444116e73SGreg Roach        DebugBar::addView($name, $data);
28544116e73SGreg Roach
286f3281ad6SGreg Roach        return $view->render();
287f3281ad6SGreg Roach    }
288f3281ad6SGreg Roach}
289