xref: /webtrees/app/View.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
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 */
17*fcfa147eSGreg 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
39f3281ad6SGreg Roach/**
40f3281ad6SGreg Roach * Simple view/template class.
41f3281ad6SGreg Roach */
42c1010edaSGreg Roachclass View
43c1010edaSGreg Roach{
443e4bf26fSGreg Roach    public const NAMESPACE_SEPARATOR = '::';
45dd6b2bfcSGreg Roach
46dd6b2bfcSGreg Roach    // File extension for our template files.
4716d6367aSGreg Roach    private const TEMPLATE_EXTENSION = '.phtml';
48dd6b2bfcSGreg Roach
49f3281ad6SGreg Roach    /**
50f3281ad6SGreg Roach     * @var string The (file) name of the view.
51f3281ad6SGreg Roach     */
52f3281ad6SGreg Roach    private $name;
53f3281ad6SGreg Roach
54f3281ad6SGreg Roach    /**
55f3281ad6SGreg Roach     * @var mixed[] Data to be inserted into the view.
56f3281ad6SGreg Roach     */
57f3281ad6SGreg Roach    private $data;
58f3281ad6SGreg Roach
59f3281ad6SGreg Roach    /**
603e4bf26fSGreg Roach     * @var string[] Where do the templates live, for each namespace.
613e4bf26fSGreg Roach     */
623e4bf26fSGreg Roach    private static $namespaces = [
63f397d0fdSGreg Roach        '' => Webtrees::ROOT_DIR . 'resources/views/',
643e4bf26fSGreg Roach    ];
653e4bf26fSGreg Roach
663e4bf26fSGreg Roach    /**
673e4bf26fSGreg Roach     * @var string[] Modules can replace core views with their own.
683e4bf26fSGreg Roach     */
693e4bf26fSGreg Roach    private static $replacements = [];
703e4bf26fSGreg Roach
713e4bf26fSGreg Roach    /**
7208d24c7aSGreg Roach     * @var mixed[] Data to be inserted into all views.
7308d24c7aSGreg Roach     */
7408d24c7aSGreg Roach    private static $shared_data = [];
7508d24c7aSGreg Roach
7608d24c7aSGreg 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     * Createa view from a template name and optional data.
88f3281ad6SGreg Roach     *
89fa4036e8SGreg Roach     * @param string $name
90f3281ad6SGreg Roach     * @param array  $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    /**
9908d24c7aSGreg Roach     * Shared data that is available to all views.
100962e29c9SGreg Roach     *
101962e29c9SGreg Roach     * @param string $key
102962e29c9SGreg Roach     * @param mixed  $value
103fa4036e8SGreg Roach     *
104fa4036e8SGreg Roach     * @return void
10508d24c7aSGreg Roach     */
106e364afe4SGreg Roach    public static function share(string $key, $value): void
107c1010edaSGreg Roach    {
10808d24c7aSGreg Roach        self::$shared_data[$key] = $value;
10908d24c7aSGreg Roach    }
11008d24c7aSGreg Roach
11108d24c7aSGreg Roach    /**
112ecf66805SGreg Roach     * Implementation of Blade "stacks".
113ecf66805SGreg Roach     *
114ecf66805SGreg Roach     * @see https://laravel.com/docs/5.5/blade#stacks
115962e29c9SGreg Roach     *
116962e29c9SGreg Roach     * @param string $stack
117fa4036e8SGreg Roach     *
118fa4036e8SGreg Roach     * @return void
119ecf66805SGreg Roach     */
120e364afe4SGreg Roach    public static function push(string $stack): void
121c1010edaSGreg Roach    {
122ecf66805SGreg Roach        self::$stack = $stack;
123d7952a34SGreg Roach
124ecf66805SGreg Roach        ob_start();
125ecf66805SGreg Roach    }
126ecf66805SGreg Roach
127ecf66805SGreg Roach    /**
128ecf66805SGreg Roach     * Implementation of Blade "stacks".
129fa4036e8SGreg Roach     *
130fa4036e8SGreg Roach     * @return void
131ecf66805SGreg Roach     */
132e364afe4SGreg Roach    public static function endpush(): void
133c1010edaSGreg Roach    {
13488de55fdSRico Sonntag        $content = ob_get_clean();
13588de55fdSRico Sonntag
136e50f5fc6SGreg Roach        if ($content === false) {
137e50f5fc6SGreg Roach            throw new LogicException('found endpush(), but did not find push()');
138e50f5fc6SGreg Roach        }
139e50f5fc6SGreg Roach
140d7952a34SGreg Roach        self::$stacks[self::$stack][] = $content;
141d7952a34SGreg Roach    }
142d7952a34SGreg Roach
143d7952a34SGreg Roach    /**
144d7952a34SGreg Roach     * Variant of push that will only add one copy of each item.
145d7952a34SGreg Roach     *
146d7952a34SGreg Roach     * @param string $stack
147d7952a34SGreg Roach     *
148d7952a34SGreg Roach     * @return void
149d7952a34SGreg Roach     */
150e364afe4SGreg Roach    public static function pushunique(string $stack): void
151d7952a34SGreg Roach    {
152d7952a34SGreg Roach        self::$stack = $stack;
153d7952a34SGreg Roach
154d7952a34SGreg Roach        ob_start();
155d7952a34SGreg Roach    }
156d7952a34SGreg Roach
157d7952a34SGreg Roach    /**
158d7952a34SGreg Roach     * Variant of push that will only add one copy of each item.
159d7952a34SGreg Roach     *
160d7952a34SGreg Roach     * @return void
161d7952a34SGreg Roach     */
162e364afe4SGreg Roach    public static function endpushunique(): void
163d7952a34SGreg Roach    {
164d7952a34SGreg Roach        $content = ob_get_clean();
165d7952a34SGreg Roach
166e50f5fc6SGreg Roach        if ($content === false) {
167e50f5fc6SGreg Roach            throw new LogicException('found endpushunique(), but did not find pushunique()');
168e50f5fc6SGreg Roach        }
169e50f5fc6SGreg Roach
170d7952a34SGreg Roach        self::$stacks[self::$stack][sha1($content)] = $content;
171ecf66805SGreg Roach    }
172ecf66805SGreg Roach
173ecf66805SGreg Roach    /**
174ecf66805SGreg Roach     * Implementation of Blade "stacks".
175ecf66805SGreg Roach     *
176962e29c9SGreg Roach     * @param string $stack
177962e29c9SGreg Roach     *
178ecf66805SGreg Roach     * @return string
179ecf66805SGreg Roach     */
180c1010edaSGreg Roach    public static function stack(string $stack): string
181c1010edaSGreg Roach    {
182ecf66805SGreg Roach        $content = implode('', self::$stacks[$stack] ?? []);
183ecf66805SGreg Roach
184ecf66805SGreg Roach        self::$stacks[$stack] = [];
185ecf66805SGreg Roach
186ecf66805SGreg Roach        return $content;
187ecf66805SGreg Roach    }
188ecf66805SGreg Roach
189ecf66805SGreg Roach    /**
190f3281ad6SGreg Roach     * Render a view.
191f3281ad6SGreg Roach     *
192f3281ad6SGreg Roach     * @return string
19370f31542SGreg Roach     * @throws Throwable
194f3281ad6SGreg Roach     */
1958f53f488SRico Sonntag    public function render(): string
196c1010edaSGreg Roach    {
1974a86d714SGreg Roach        $variables_for_view = $this->data + self::$shared_data;
198e364afe4SGreg Roach        extract($variables_for_view, EXTR_SKIP);
199f3281ad6SGreg Roach
20070f31542SGreg Roach        try {
201f3281ad6SGreg Roach            ob_start();
202bc241c54SGreg Roach            // Do not use require, so we can catch errors for missing files
203bc241c54SGreg Roach            include $this->getFilenameForView($this->name);
20475d70144SGreg Roach
205f3281ad6SGreg Roach            return ob_get_clean();
20670f31542SGreg Roach        } catch (Throwable $ex) {
207d3da353bSGreg Roach            while (ob_get_level() > 0) {
20870f31542SGreg Roach                ob_end_clean();
209d3da353bSGreg Roach            }
21070f31542SGreg Roach            throw $ex;
21170f31542SGreg Roach        }
212f3281ad6SGreg Roach    }
213f3281ad6SGreg Roach
214f3281ad6SGreg Roach    /**
2153e4bf26fSGreg Roach     * @param string $namespace
2163e4bf26fSGreg Roach     * @param string $path
2173e4bf26fSGreg Roach     *
2183e4bf26fSGreg Roach     * @throws InvalidArgumentException
2193e4bf26fSGreg Roach     */
2203e4bf26fSGreg Roach    public static function registerNamespace(string $namespace, string $path): void
2213e4bf26fSGreg Roach    {
2223e4bf26fSGreg Roach        if ($namespace === '') {
2233e4bf26fSGreg Roach            throw new InvalidArgumentException('Cannot register the default namespace');
2243e4bf26fSGreg Roach        }
2253e4bf26fSGreg Roach
2263e4bf26fSGreg Roach        if (!Str::endsWith($path, '/')) {
2273e4bf26fSGreg Roach            throw new InvalidArgumentException('Paths must end with a directory separator');
2283e4bf26fSGreg Roach        }
2293e4bf26fSGreg Roach
2303e4bf26fSGreg Roach        self::$namespaces[$namespace] = $path;
2313e4bf26fSGreg Roach    }
2323e4bf26fSGreg Roach
2333e4bf26fSGreg Roach    /**
2343e4bf26fSGreg Roach     * @param string $old
2353e4bf26fSGreg Roach     * @param string $new
2363e4bf26fSGreg Roach     *
2373e4bf26fSGreg Roach     * @throws InvalidArgumentException
2383e4bf26fSGreg Roach     */
2393e4bf26fSGreg Roach    public static function registerCustomView(string $old, string $new): void
2403e4bf26fSGreg Roach    {
2413e4bf26fSGreg Roach        if (Str::contains($old, self::NAMESPACE_SEPARATOR) && Str::contains($new, self::NAMESPACE_SEPARATOR)) {
2423e4bf26fSGreg Roach            self::$replacements[$old] = $new;
2433e4bf26fSGreg Roach        } else {
2443e4bf26fSGreg Roach            throw new InvalidArgumentException();
2453e4bf26fSGreg Roach        }
2463e4bf26fSGreg Roach    }
2473e4bf26fSGreg Roach
2483e4bf26fSGreg Roach    /**
2493e4bf26fSGreg Roach     * Find the file for a view.
250f3281ad6SGreg Roach     *
251f3281ad6SGreg Roach     * @param string $view_name
252f3281ad6SGreg Roach     *
25375d70144SGreg Roach     * @return string
25450c68a25SGreg Roach     * @throws Exception
255f3281ad6SGreg Roach     */
2563e4bf26fSGreg Roach    public function getFilenameForView(string $view_name): string
257c1010edaSGreg Roach    {
258fdbcd0efSGreg Roach        // If we request "::view", then use it explicityly.  Don't allow replacements.
259fdbcd0efSGreg Roach        $explicit = Str::startsWith($view_name, self::NAMESPACE_SEPARATOR);
260fdbcd0efSGreg Roach
2613e4bf26fSGreg Roach        if (!Str::contains($view_name, self::NAMESPACE_SEPARATOR)) {
2623e4bf26fSGreg Roach            $view_name = self::NAMESPACE_SEPARATOR . $view_name;
2633e4bf26fSGreg Roach        }
26475d70144SGreg Roach
2653e4bf26fSGreg Roach        // Apply replacements / customisations
266fdbcd0efSGreg Roach        while (!$explicit && array_key_exists($view_name, self::$replacements)) {
2673e4bf26fSGreg Roach            $view_name = self::$replacements[$view_name];
2683e4bf26fSGreg Roach        }
2693e4bf26fSGreg Roach
2703e4bf26fSGreg Roach        [$namespace, $view_name] = explode(self::NAMESPACE_SEPARATOR, $view_name, 2);
2713e4bf26fSGreg Roach
2723e4bf26fSGreg Roach        if ((self::$namespaces[$namespace] ?? null) === null) {
2733e4bf26fSGreg Roach            throw new RuntimeException('Namespace "' . e($namespace) .  '" not found.');
2743e4bf26fSGreg Roach        }
2753e4bf26fSGreg Roach
2763e4bf26fSGreg Roach        $view_file = self::$namespaces[$namespace] . $view_name . self::TEMPLATE_EXTENSION;
2773e4bf26fSGreg Roach
2783e4bf26fSGreg Roach        if (!is_file($view_file)) {
2793e4bf26fSGreg Roach            throw new RuntimeException('View file not found: ' . e($view_file));
2803e4bf26fSGreg Roach        }
2813e4bf26fSGreg Roach
28250c68a25SGreg Roach        return $view_file;
283f21917b2SGreg Roach    }
28450c68a25SGreg Roach
285f3281ad6SGreg Roach    /**
286f3281ad6SGreg Roach     * Cerate and render a view in a single operation.
287f3281ad6SGreg Roach     *
288f3281ad6SGreg Roach     * @param string  $name
289f3281ad6SGreg Roach     * @param mixed[] $data
290f3281ad6SGreg Roach     *
291f3281ad6SGreg Roach     * @return string
292f3281ad6SGreg Roach     */
2938f53f488SRico Sonntag    public static function make($name, $data = []): string
294c1010edaSGreg Roach    {
295f3281ad6SGreg Roach        $view = new static($name, $data);
296f3281ad6SGreg Roach
29744116e73SGreg Roach        DebugBar::addView($name, $data);
29844116e73SGreg Roach
299f3281ad6SGreg Roach        return $view->render();
300f3281ad6SGreg Roach    }
301f3281ad6SGreg Roach}
302