xref: /webtrees/app/View.php (revision dec352c1d7404cdd35c9b1a1b5d97f29e7c4ebb5)
1f3281ad6SGreg Roach<?php
23976b470SGreg 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 */
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;
37*dec352c1SGreg Roachuse function str_contains;
38*dec352c1SGreg Roachuse function str_ends_with;
39*dec352c1SGreg Roachuse function str_starts_with;
4050c68a25SGreg Roach
41ef5d23f1SGreg Roachuse const EXTR_OVERWRITE;
42ef5d23f1SGreg Roach
43f3281ad6SGreg Roach/**
44f3281ad6SGreg Roach * Simple view/template class.
45f3281ad6SGreg Roach */
46c1010edaSGreg Roachclass View
47c1010edaSGreg Roach{
483e4bf26fSGreg Roach    public const NAMESPACE_SEPARATOR = '::';
49dd6b2bfcSGreg Roach
50dd6b2bfcSGreg Roach    // File extension for our template files.
5116d6367aSGreg Roach    private const TEMPLATE_EXTENSION = '.phtml';
52dd6b2bfcSGreg Roach
53f3281ad6SGreg Roach    /**
54f3281ad6SGreg Roach     * @var string The (file) name of the view.
55f3281ad6SGreg Roach     */
56f3281ad6SGreg Roach    private $name;
57f3281ad6SGreg Roach
58f3281ad6SGreg Roach    /**
59f3281ad6SGreg Roach     * @var mixed[] Data to be inserted into the view.
60f3281ad6SGreg Roach     */
61f3281ad6SGreg Roach    private $data;
62f3281ad6SGreg Roach
63f3281ad6SGreg Roach    /**
643e4bf26fSGreg Roach     * @var string[] Where do the templates live, for each namespace.
653e4bf26fSGreg Roach     */
663e4bf26fSGreg Roach    private static $namespaces = [
67f397d0fdSGreg Roach        '' => Webtrees::ROOT_DIR . 'resources/views/',
683e4bf26fSGreg Roach    ];
693e4bf26fSGreg Roach
703e4bf26fSGreg Roach    /**
713e4bf26fSGreg Roach     * @var string[] Modules can replace core views with their own.
723e4bf26fSGreg Roach     */
733e4bf26fSGreg Roach    private static $replacements = [];
743e4bf26fSGreg Roach
753e4bf26fSGreg Roach    /**
76ecf66805SGreg Roach     * @var string Implementation of Blade "stacks".
77ecf66805SGreg Roach     */
78ecf66805SGreg Roach    private static $stack;
79ecf66805SGreg Roach
80ecf66805SGreg Roach    /**
81ecf66805SGreg Roach     * @var array[] Implementation of Blade "stacks".
82ecf66805SGreg Roach     */
83ecf66805SGreg Roach    private static $stacks = [];
84ecf66805SGreg Roach
85ecf66805SGreg Roach    /**
86f3281ad6SGreg Roach     * Createa view from a template name and optional data.
87f3281ad6SGreg Roach     *
88fa4036e8SGreg Roach     * @param string       $name
895d4b7ec2SGreg Roach     * @param array<mixed> $data
90f3281ad6SGreg Roach     */
91fa4036e8SGreg Roach    public function __construct(string $name, $data = [])
92c1010edaSGreg Roach    {
93f3281ad6SGreg Roach        $this->name = $name;
94f3281ad6SGreg Roach        $this->data = $data;
95f3281ad6SGreg Roach    }
96f3281ad6SGreg Roach
97f3281ad6SGreg Roach    /**
98ecf66805SGreg Roach     * Implementation of Blade "stacks".
99ecf66805SGreg Roach     *
100ecf66805SGreg Roach     * @see https://laravel.com/docs/5.5/blade#stacks
101962e29c9SGreg Roach     *
102962e29c9SGreg Roach     * @param string $stack
103fa4036e8SGreg Roach     *
104fa4036e8SGreg Roach     * @return void
105ecf66805SGreg Roach     */
106e364afe4SGreg Roach    public static function push(string $stack): void
107c1010edaSGreg Roach    {
108ecf66805SGreg Roach        self::$stack = $stack;
109d7952a34SGreg Roach
110ecf66805SGreg Roach        ob_start();
111ecf66805SGreg Roach    }
112ecf66805SGreg Roach
113ecf66805SGreg Roach    /**
114ecf66805SGreg Roach     * Implementation of Blade "stacks".
115fa4036e8SGreg Roach     *
116fa4036e8SGreg Roach     * @return void
117ecf66805SGreg Roach     */
118e364afe4SGreg Roach    public static function endpush(): void
119c1010edaSGreg Roach    {
12088de55fdSRico Sonntag        $content = ob_get_clean();
12188de55fdSRico Sonntag
122e50f5fc6SGreg Roach        if ($content === false) {
123e50f5fc6SGreg Roach            throw new LogicException('found endpush(), but did not find push()');
124e50f5fc6SGreg Roach        }
125e50f5fc6SGreg Roach
126d7952a34SGreg Roach        self::$stacks[self::$stack][] = $content;
127d7952a34SGreg Roach    }
128d7952a34SGreg Roach
129d7952a34SGreg Roach    /**
130d7952a34SGreg Roach     * Variant of push that will only add one copy of each item.
131d7952a34SGreg Roach     *
132d7952a34SGreg Roach     * @param string $stack
133d7952a34SGreg Roach     *
134d7952a34SGreg Roach     * @return void
135d7952a34SGreg Roach     */
136e364afe4SGreg Roach    public static function pushunique(string $stack): void
137d7952a34SGreg Roach    {
138d7952a34SGreg Roach        self::$stack = $stack;
139d7952a34SGreg Roach
140d7952a34SGreg Roach        ob_start();
141d7952a34SGreg Roach    }
142d7952a34SGreg Roach
143d7952a34SGreg Roach    /**
144d7952a34SGreg Roach     * Variant of push that will only add one copy of each item.
145d7952a34SGreg Roach     *
146d7952a34SGreg Roach     * @return void
147d7952a34SGreg Roach     */
148e364afe4SGreg Roach    public static function endpushunique(): void
149d7952a34SGreg Roach    {
150d7952a34SGreg Roach        $content = ob_get_clean();
151d7952a34SGreg Roach
152e50f5fc6SGreg Roach        if ($content === false) {
153e50f5fc6SGreg Roach            throw new LogicException('found endpushunique(), but did not find pushunique()');
154e50f5fc6SGreg Roach        }
155e50f5fc6SGreg Roach
156d7952a34SGreg Roach        self::$stacks[self::$stack][sha1($content)] = $content;
157ecf66805SGreg Roach    }
158ecf66805SGreg Roach
159ecf66805SGreg Roach    /**
160ecf66805SGreg Roach     * Implementation of Blade "stacks".
161ecf66805SGreg Roach     *
162962e29c9SGreg Roach     * @param string $stack
163962e29c9SGreg Roach     *
164ecf66805SGreg Roach     * @return string
165ecf66805SGreg Roach     */
166c1010edaSGreg Roach    public static function stack(string $stack): string
167c1010edaSGreg Roach    {
168ecf66805SGreg Roach        $content = implode('', self::$stacks[$stack] ?? []);
169ecf66805SGreg Roach
170ecf66805SGreg Roach        self::$stacks[$stack] = [];
171ecf66805SGreg Roach
172ecf66805SGreg Roach        return $content;
173ecf66805SGreg Roach    }
174ecf66805SGreg Roach
175ecf66805SGreg Roach    /**
176f3281ad6SGreg Roach     * Render a view.
177f3281ad6SGreg Roach     *
178f3281ad6SGreg Roach     * @return string
17970f31542SGreg Roach     * @throws Throwable
180f3281ad6SGreg Roach     */
1818f53f488SRico Sonntag    public function render(): string
182c1010edaSGreg Roach    {
183ef5d23f1SGreg Roach        extract($this->data, EXTR_OVERWRITE);
184f3281ad6SGreg Roach
18570f31542SGreg Roach        try {
186f3281ad6SGreg Roach            ob_start();
187bc241c54SGreg Roach            // Do not use require, so we can catch errors for missing files
188bc241c54SGreg Roach            include $this->getFilenameForView($this->name);
18975d70144SGreg Roach
190f3281ad6SGreg Roach            return ob_get_clean();
19170f31542SGreg Roach        } catch (Throwable $ex) {
192d3da353bSGreg Roach            while (ob_get_level() > 0) {
19370f31542SGreg Roach                ob_end_clean();
194d3da353bSGreg Roach            }
19570f31542SGreg Roach            throw $ex;
19670f31542SGreg Roach        }
197f3281ad6SGreg Roach    }
198f3281ad6SGreg Roach
199f3281ad6SGreg Roach    /**
2003e4bf26fSGreg Roach     * @param string $namespace
2013e4bf26fSGreg Roach     * @param string $path
2023e4bf26fSGreg Roach     *
2033e4bf26fSGreg Roach     * @throws InvalidArgumentException
2043e4bf26fSGreg Roach     */
2053e4bf26fSGreg Roach    public static function registerNamespace(string $namespace, string $path): void
2063e4bf26fSGreg Roach    {
2073e4bf26fSGreg Roach        if ($namespace === '') {
2083e4bf26fSGreg Roach            throw new InvalidArgumentException('Cannot register the default namespace');
2093e4bf26fSGreg Roach        }
2103e4bf26fSGreg Roach
211*dec352c1SGreg Roach        if (!str_ends_with($path, '/')) {
2123e4bf26fSGreg Roach            throw new InvalidArgumentException('Paths must end with a directory separator');
2133e4bf26fSGreg Roach        }
2143e4bf26fSGreg Roach
2153e4bf26fSGreg Roach        self::$namespaces[$namespace] = $path;
2163e4bf26fSGreg Roach    }
2173e4bf26fSGreg Roach
2183e4bf26fSGreg Roach    /**
2193e4bf26fSGreg Roach     * @param string $old
2203e4bf26fSGreg Roach     * @param string $new
2213e4bf26fSGreg Roach     *
2223e4bf26fSGreg Roach     * @throws InvalidArgumentException
2233e4bf26fSGreg Roach     */
2243e4bf26fSGreg Roach    public static function registerCustomView(string $old, string $new): void
2253e4bf26fSGreg Roach    {
226*dec352c1SGreg Roach        if (str_contains($old, self::NAMESPACE_SEPARATOR) && str_contains($new, self::NAMESPACE_SEPARATOR)) {
2273e4bf26fSGreg Roach            self::$replacements[$old] = $new;
2283e4bf26fSGreg Roach        } else {
2293e4bf26fSGreg Roach            throw new InvalidArgumentException();
2303e4bf26fSGreg Roach        }
2313e4bf26fSGreg Roach    }
2323e4bf26fSGreg Roach
2333e4bf26fSGreg Roach    /**
2343e4bf26fSGreg Roach     * Find the file for a view.
235f3281ad6SGreg Roach     *
236f3281ad6SGreg Roach     * @param string $view_name
237f3281ad6SGreg Roach     *
23875d70144SGreg Roach     * @return string
23950c68a25SGreg Roach     * @throws Exception
240f3281ad6SGreg Roach     */
2413e4bf26fSGreg Roach    public function getFilenameForView(string $view_name): string
242c1010edaSGreg Roach    {
243*dec352c1SGreg Roach        // If we request "::view", then use it explicitly.  Don't allow replacements.
244*dec352c1SGreg Roach        $explicit = str_starts_with($view_name, self::NAMESPACE_SEPARATOR);
245fdbcd0efSGreg Roach
246*dec352c1SGreg Roach        if (!str_contains($view_name, self::NAMESPACE_SEPARATOR)) {
2473e4bf26fSGreg Roach            $view_name = self::NAMESPACE_SEPARATOR . $view_name;
2483e4bf26fSGreg Roach        }
24975d70144SGreg Roach
250*dec352c1SGreg Roach        // Apply replacements / customizations
251fdbcd0efSGreg Roach        while (!$explicit && array_key_exists($view_name, self::$replacements)) {
2523e4bf26fSGreg Roach            $view_name = self::$replacements[$view_name];
2533e4bf26fSGreg Roach        }
2543e4bf26fSGreg Roach
2553e4bf26fSGreg Roach        [$namespace, $view_name] = explode(self::NAMESPACE_SEPARATOR, $view_name, 2);
2563e4bf26fSGreg Roach
2573e4bf26fSGreg Roach        if ((self::$namespaces[$namespace] ?? null) === null) {
2583e4bf26fSGreg Roach            throw new RuntimeException('Namespace "' . e($namespace) .  '" not found.');
2593e4bf26fSGreg Roach        }
2603e4bf26fSGreg Roach
2613e4bf26fSGreg Roach        $view_file = self::$namespaces[$namespace] . $view_name . self::TEMPLATE_EXTENSION;
2623e4bf26fSGreg Roach
2633e4bf26fSGreg Roach        if (!is_file($view_file)) {
2643e4bf26fSGreg Roach            throw new RuntimeException('View file not found: ' . e($view_file));
2653e4bf26fSGreg Roach        }
2663e4bf26fSGreg Roach
26750c68a25SGreg Roach        return $view_file;
268f21917b2SGreg Roach    }
26950c68a25SGreg Roach
270f3281ad6SGreg Roach    /**
271f3281ad6SGreg Roach     * Cerate and render a view in a single operation.
272f3281ad6SGreg Roach     *
273f3281ad6SGreg Roach     * @param string  $name
274f3281ad6SGreg Roach     * @param mixed[] $data
275f3281ad6SGreg Roach     *
276f3281ad6SGreg Roach     * @return string
277f3281ad6SGreg Roach     */
2788f53f488SRico Sonntag    public static function make($name, $data = []): string
279c1010edaSGreg Roach    {
2801e752ebbSGreg Roach        $view = new self($name, $data);
281f3281ad6SGreg Roach
28244116e73SGreg Roach        DebugBar::addView($name, $data);
28344116e73SGreg Roach
284f3281ad6SGreg Roach        return $view->render();
285f3281ad6SGreg Roach    }
286f3281ad6SGreg Roach}
287