xref: /webtrees/app/View.php (revision 1e752ebb8c1d6981a24de4cf91c5c290dbd906aa)
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 Illuminate\Support\Str;
243e4bf26fSGreg Roachuse InvalidArgumentException;
25e50f5fc6SGreg Roachuse LogicException;
263e4bf26fSGreg Roachuse RuntimeException;
2770f31542SGreg Roachuse Throwable;
283976b470SGreg Roach
293e4bf26fSGreg Roachuse function array_key_exists;
303e4bf26fSGreg Roachuse function explode;
31d7952a34SGreg Roachuse function extract;
32d7952a34SGreg Roachuse function implode;
33d7952a34SGreg Roachuse function is_file;
34d7952a34SGreg Roachuse function ob_end_clean;
35d3da353bSGreg Roachuse function ob_get_level;
36d7952a34SGreg Roachuse function ob_start;
37d7952a34SGreg Roachuse function sha1;
3850c68a25SGreg Roach
39ef5d23f1SGreg Roachuse const EXTR_OVERWRITE;
40ef5d23f1SGreg Roach
41f3281ad6SGreg Roach/**
42f3281ad6SGreg Roach * Simple view/template class.
43f3281ad6SGreg Roach */
44c1010edaSGreg Roachclass View
45c1010edaSGreg Roach{
463e4bf26fSGreg Roach    public const NAMESPACE_SEPARATOR = '::';
47dd6b2bfcSGreg Roach
48dd6b2bfcSGreg Roach    // File extension for our template files.
4916d6367aSGreg Roach    private const TEMPLATE_EXTENSION = '.phtml';
50dd6b2bfcSGreg Roach
51f3281ad6SGreg Roach    /**
52f3281ad6SGreg Roach     * @var string The (file) name of the view.
53f3281ad6SGreg Roach     */
54f3281ad6SGreg Roach    private $name;
55f3281ad6SGreg Roach
56f3281ad6SGreg Roach    /**
57f3281ad6SGreg Roach     * @var mixed[] Data to be inserted into the view.
58f3281ad6SGreg Roach     */
59f3281ad6SGreg Roach    private $data;
60f3281ad6SGreg Roach
61f3281ad6SGreg Roach    /**
623e4bf26fSGreg Roach     * @var string[] Where do the templates live, for each namespace.
633e4bf26fSGreg Roach     */
643e4bf26fSGreg Roach    private static $namespaces = [
65f397d0fdSGreg Roach        '' => Webtrees::ROOT_DIR . 'resources/views/',
663e4bf26fSGreg Roach    ];
673e4bf26fSGreg Roach
683e4bf26fSGreg Roach    /**
693e4bf26fSGreg Roach     * @var string[] Modules can replace core views with their own.
703e4bf26fSGreg Roach     */
713e4bf26fSGreg Roach    private static $replacements = [];
723e4bf26fSGreg Roach
733e4bf26fSGreg Roach    /**
74ecf66805SGreg Roach     * @var string Implementation of Blade "stacks".
75ecf66805SGreg Roach     */
76ecf66805SGreg Roach    private static $stack;
77ecf66805SGreg Roach
78ecf66805SGreg Roach    /**
79ecf66805SGreg Roach     * @var array[] Implementation of Blade "stacks".
80ecf66805SGreg Roach     */
81ecf66805SGreg Roach    private static $stacks = [];
82ecf66805SGreg Roach
83ecf66805SGreg Roach    /**
84f3281ad6SGreg Roach     * Createa view from a template name and optional data.
85f3281ad6SGreg Roach     *
86fa4036e8SGreg Roach     * @param string $name
87f3281ad6SGreg Roach     * @param array  $data
88f3281ad6SGreg Roach     */
89fa4036e8SGreg Roach    public function __construct(string $name, $data = [])
90c1010edaSGreg Roach    {
91f3281ad6SGreg Roach        $this->name = $name;
92f3281ad6SGreg Roach        $this->data = $data;
93f3281ad6SGreg Roach    }
94f3281ad6SGreg Roach
95f3281ad6SGreg Roach    /**
96ecf66805SGreg Roach     * Implementation of Blade "stacks".
97ecf66805SGreg Roach     *
98ecf66805SGreg Roach     * @see https://laravel.com/docs/5.5/blade#stacks
99962e29c9SGreg Roach     *
100962e29c9SGreg Roach     * @param string $stack
101fa4036e8SGreg Roach     *
102fa4036e8SGreg Roach     * @return void
103ecf66805SGreg Roach     */
104e364afe4SGreg Roach    public static function push(string $stack): void
105c1010edaSGreg Roach    {
106ecf66805SGreg Roach        self::$stack = $stack;
107d7952a34SGreg Roach
108ecf66805SGreg Roach        ob_start();
109ecf66805SGreg Roach    }
110ecf66805SGreg Roach
111ecf66805SGreg Roach    /**
112ecf66805SGreg Roach     * Implementation of Blade "stacks".
113fa4036e8SGreg Roach     *
114fa4036e8SGreg Roach     * @return void
115ecf66805SGreg Roach     */
116e364afe4SGreg Roach    public static function endpush(): void
117c1010edaSGreg Roach    {
11888de55fdSRico Sonntag        $content = ob_get_clean();
11988de55fdSRico Sonntag
120e50f5fc6SGreg Roach        if ($content === false) {
121e50f5fc6SGreg Roach            throw new LogicException('found endpush(), but did not find push()');
122e50f5fc6SGreg Roach        }
123e50f5fc6SGreg Roach
124d7952a34SGreg Roach        self::$stacks[self::$stack][] = $content;
125d7952a34SGreg Roach    }
126d7952a34SGreg Roach
127d7952a34SGreg Roach    /**
128d7952a34SGreg Roach     * Variant of push that will only add one copy of each item.
129d7952a34SGreg Roach     *
130d7952a34SGreg Roach     * @param string $stack
131d7952a34SGreg Roach     *
132d7952a34SGreg Roach     * @return void
133d7952a34SGreg Roach     */
134e364afe4SGreg Roach    public static function pushunique(string $stack): void
135d7952a34SGreg Roach    {
136d7952a34SGreg Roach        self::$stack = $stack;
137d7952a34SGreg Roach
138d7952a34SGreg Roach        ob_start();
139d7952a34SGreg Roach    }
140d7952a34SGreg Roach
141d7952a34SGreg Roach    /**
142d7952a34SGreg Roach     * Variant of push that will only add one copy of each item.
143d7952a34SGreg Roach     *
144d7952a34SGreg Roach     * @return void
145d7952a34SGreg Roach     */
146e364afe4SGreg Roach    public static function endpushunique(): void
147d7952a34SGreg Roach    {
148d7952a34SGreg Roach        $content = ob_get_clean();
149d7952a34SGreg Roach
150e50f5fc6SGreg Roach        if ($content === false) {
151e50f5fc6SGreg Roach            throw new LogicException('found endpushunique(), but did not find pushunique()');
152e50f5fc6SGreg Roach        }
153e50f5fc6SGreg Roach
154d7952a34SGreg Roach        self::$stacks[self::$stack][sha1($content)] = $content;
155ecf66805SGreg Roach    }
156ecf66805SGreg Roach
157ecf66805SGreg Roach    /**
158ecf66805SGreg Roach     * Implementation of Blade "stacks".
159ecf66805SGreg Roach     *
160962e29c9SGreg Roach     * @param string $stack
161962e29c9SGreg Roach     *
162ecf66805SGreg Roach     * @return string
163ecf66805SGreg Roach     */
164c1010edaSGreg Roach    public static function stack(string $stack): string
165c1010edaSGreg Roach    {
166ecf66805SGreg Roach        $content = implode('', self::$stacks[$stack] ?? []);
167ecf66805SGreg Roach
168ecf66805SGreg Roach        self::$stacks[$stack] = [];
169ecf66805SGreg Roach
170ecf66805SGreg Roach        return $content;
171ecf66805SGreg Roach    }
172ecf66805SGreg Roach
173ecf66805SGreg Roach    /**
174f3281ad6SGreg Roach     * Render a view.
175f3281ad6SGreg Roach     *
176f3281ad6SGreg Roach     * @return string
17770f31542SGreg Roach     * @throws Throwable
178f3281ad6SGreg Roach     */
1798f53f488SRico Sonntag    public function render(): string
180c1010edaSGreg Roach    {
181ef5d23f1SGreg Roach        extract($this->data, EXTR_OVERWRITE);
182f3281ad6SGreg Roach
18370f31542SGreg Roach        try {
184f3281ad6SGreg Roach            ob_start();
185bc241c54SGreg Roach            // Do not use require, so we can catch errors for missing files
186bc241c54SGreg Roach            include $this->getFilenameForView($this->name);
18775d70144SGreg Roach
188f3281ad6SGreg Roach            return ob_get_clean();
18970f31542SGreg Roach        } catch (Throwable $ex) {
190d3da353bSGreg Roach            while (ob_get_level() > 0) {
19170f31542SGreg Roach                ob_end_clean();
192d3da353bSGreg Roach            }
19370f31542SGreg Roach            throw $ex;
19470f31542SGreg Roach        }
195f3281ad6SGreg Roach    }
196f3281ad6SGreg Roach
197f3281ad6SGreg Roach    /**
1983e4bf26fSGreg Roach     * @param string $namespace
1993e4bf26fSGreg Roach     * @param string $path
2003e4bf26fSGreg Roach     *
2013e4bf26fSGreg Roach     * @throws InvalidArgumentException
2023e4bf26fSGreg Roach     */
2033e4bf26fSGreg Roach    public static function registerNamespace(string $namespace, string $path): void
2043e4bf26fSGreg Roach    {
2053e4bf26fSGreg Roach        if ($namespace === '') {
2063e4bf26fSGreg Roach            throw new InvalidArgumentException('Cannot register the default namespace');
2073e4bf26fSGreg Roach        }
2083e4bf26fSGreg Roach
2093e4bf26fSGreg Roach        if (!Str::endsWith($path, '/')) {
2103e4bf26fSGreg Roach            throw new InvalidArgumentException('Paths must end with a directory separator');
2113e4bf26fSGreg Roach        }
2123e4bf26fSGreg Roach
2133e4bf26fSGreg Roach        self::$namespaces[$namespace] = $path;
2143e4bf26fSGreg Roach    }
2153e4bf26fSGreg Roach
2163e4bf26fSGreg Roach    /**
2173e4bf26fSGreg Roach     * @param string $old
2183e4bf26fSGreg Roach     * @param string $new
2193e4bf26fSGreg Roach     *
2203e4bf26fSGreg Roach     * @throws InvalidArgumentException
2213e4bf26fSGreg Roach     */
2223e4bf26fSGreg Roach    public static function registerCustomView(string $old, string $new): void
2233e4bf26fSGreg Roach    {
2243e4bf26fSGreg Roach        if (Str::contains($old, self::NAMESPACE_SEPARATOR) && Str::contains($new, self::NAMESPACE_SEPARATOR)) {
2253e4bf26fSGreg Roach            self::$replacements[$old] = $new;
2263e4bf26fSGreg Roach        } else {
2273e4bf26fSGreg Roach            throw new InvalidArgumentException();
2283e4bf26fSGreg Roach        }
2293e4bf26fSGreg Roach    }
2303e4bf26fSGreg Roach
2313e4bf26fSGreg Roach    /**
2323e4bf26fSGreg Roach     * Find the file for a view.
233f3281ad6SGreg Roach     *
234f3281ad6SGreg Roach     * @param string $view_name
235f3281ad6SGreg Roach     *
23675d70144SGreg Roach     * @return string
23750c68a25SGreg Roach     * @throws Exception
238f3281ad6SGreg Roach     */
2393e4bf26fSGreg Roach    public function getFilenameForView(string $view_name): string
240c1010edaSGreg Roach    {
241fdbcd0efSGreg Roach        // If we request "::view", then use it explicityly.  Don't allow replacements.
242fdbcd0efSGreg Roach        $explicit = Str::startsWith($view_name, self::NAMESPACE_SEPARATOR);
243fdbcd0efSGreg Roach
2443e4bf26fSGreg Roach        if (!Str::contains($view_name, self::NAMESPACE_SEPARATOR)) {
2453e4bf26fSGreg Roach            $view_name = self::NAMESPACE_SEPARATOR . $view_name;
2463e4bf26fSGreg Roach        }
24775d70144SGreg Roach
2483e4bf26fSGreg Roach        // Apply replacements / customisations
249fdbcd0efSGreg Roach        while (!$explicit && array_key_exists($view_name, self::$replacements)) {
2503e4bf26fSGreg Roach            $view_name = self::$replacements[$view_name];
2513e4bf26fSGreg Roach        }
2523e4bf26fSGreg Roach
2533e4bf26fSGreg Roach        [$namespace, $view_name] = explode(self::NAMESPACE_SEPARATOR, $view_name, 2);
2543e4bf26fSGreg Roach
2553e4bf26fSGreg Roach        if ((self::$namespaces[$namespace] ?? null) === null) {
2563e4bf26fSGreg Roach            throw new RuntimeException('Namespace "' . e($namespace) .  '" not found.');
2573e4bf26fSGreg Roach        }
2583e4bf26fSGreg Roach
2593e4bf26fSGreg Roach        $view_file = self::$namespaces[$namespace] . $view_name . self::TEMPLATE_EXTENSION;
2603e4bf26fSGreg Roach
2613e4bf26fSGreg Roach        if (!is_file($view_file)) {
2623e4bf26fSGreg Roach            throw new RuntimeException('View file not found: ' . e($view_file));
2633e4bf26fSGreg Roach        }
2643e4bf26fSGreg Roach
26550c68a25SGreg Roach        return $view_file;
266f21917b2SGreg Roach    }
26750c68a25SGreg Roach
268f3281ad6SGreg Roach    /**
269f3281ad6SGreg Roach     * Cerate and render a view in a single operation.
270f3281ad6SGreg Roach     *
271f3281ad6SGreg Roach     * @param string  $name
272f3281ad6SGreg Roach     * @param mixed[] $data
273f3281ad6SGreg Roach     *
274f3281ad6SGreg Roach     * @return string
275f3281ad6SGreg Roach     */
2768f53f488SRico Sonntag    public static function make($name, $data = []): string
277c1010edaSGreg Roach    {
278*1e752ebbSGreg Roach        $view = new self($name, $data);
279f3281ad6SGreg Roach
28044116e73SGreg Roach        DebugBar::addView($name, $data);
28144116e73SGreg Roach
282f3281ad6SGreg Roach        return $view->render();
283f3281ad6SGreg Roach    }
284f3281ad6SGreg Roach}
285