xref: /webtrees/app/View.php (revision e50f5fc6ed598659bfe073c841165158ede9ddd2)
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 */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
19f3281ad6SGreg Roachnamespace Fisharebest\Webtrees;
20f3281ad6SGreg Roach
2150c68a25SGreg Roachuse Exception;
223e4bf26fSGreg Roachuse Illuminate\Support\Str;
233e4bf26fSGreg Roachuse InvalidArgumentException;
24*e50f5fc6SGreg 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;
3750c68a25SGreg Roach
38f3281ad6SGreg Roach/**
39f3281ad6SGreg Roach * Simple view/template class.
40f3281ad6SGreg Roach */
41c1010edaSGreg Roachclass View
42c1010edaSGreg Roach{
433e4bf26fSGreg Roach    public const NAMESPACE_SEPARATOR = '::';
44dd6b2bfcSGreg Roach
45dd6b2bfcSGreg Roach    // File extension for our template files.
4616d6367aSGreg Roach    private const TEMPLATE_EXTENSION = '.phtml';
47dd6b2bfcSGreg Roach
48f3281ad6SGreg Roach    /**
49f3281ad6SGreg Roach     * @var string The (file) name of the view.
50f3281ad6SGreg Roach     */
51f3281ad6SGreg Roach    private $name;
52f3281ad6SGreg Roach
53f3281ad6SGreg Roach    /**
54f3281ad6SGreg Roach     * @var mixed[] Data to be inserted into the view.
55f3281ad6SGreg Roach     */
56f3281ad6SGreg Roach    private $data;
57f3281ad6SGreg Roach
58f3281ad6SGreg Roach    /**
593e4bf26fSGreg Roach     * @var string[] Where do the templates live, for each namespace.
603e4bf26fSGreg Roach     */
613e4bf26fSGreg Roach    private static $namespaces = [
62f397d0fdSGreg Roach        '' => Webtrees::ROOT_DIR . 'resources/views/',
633e4bf26fSGreg Roach    ];
643e4bf26fSGreg Roach
653e4bf26fSGreg Roach    /**
663e4bf26fSGreg Roach     * @var string[] Modules can replace core views with their own.
673e4bf26fSGreg Roach     */
683e4bf26fSGreg Roach    private static $replacements = [];
693e4bf26fSGreg Roach
703e4bf26fSGreg Roach    /**
7108d24c7aSGreg Roach     * @var mixed[] Data to be inserted into all views.
7208d24c7aSGreg Roach     */
7308d24c7aSGreg Roach    private static $shared_data = [];
7408d24c7aSGreg Roach
7508d24c7aSGreg 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
89f3281ad6SGreg Roach     * @param array  $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    /**
9808d24c7aSGreg Roach     * Shared data that is available to all views.
99962e29c9SGreg Roach     *
100962e29c9SGreg Roach     * @param string $key
101962e29c9SGreg Roach     * @param mixed  $value
102fa4036e8SGreg Roach     *
103fa4036e8SGreg Roach     * @return void
10408d24c7aSGreg Roach     */
105e364afe4SGreg Roach    public static function share(string $key, $value): void
106c1010edaSGreg Roach    {
10708d24c7aSGreg Roach        self::$shared_data[$key] = $value;
10808d24c7aSGreg Roach    }
10908d24c7aSGreg Roach
11008d24c7aSGreg Roach    /**
111ecf66805SGreg Roach     * Implementation of Blade "stacks".
112ecf66805SGreg Roach     *
113ecf66805SGreg Roach     * @see https://laravel.com/docs/5.5/blade#stacks
114962e29c9SGreg Roach     *
115962e29c9SGreg Roach     * @param string $stack
116fa4036e8SGreg Roach     *
117fa4036e8SGreg Roach     * @return void
118ecf66805SGreg Roach     */
119e364afe4SGreg Roach    public static function push(string $stack): void
120c1010edaSGreg Roach    {
121ecf66805SGreg Roach        self::$stack = $stack;
122d7952a34SGreg Roach
123ecf66805SGreg Roach        ob_start();
124ecf66805SGreg Roach    }
125ecf66805SGreg Roach
126ecf66805SGreg Roach    /**
127ecf66805SGreg Roach     * Implementation of Blade "stacks".
128fa4036e8SGreg Roach     *
129fa4036e8SGreg Roach     * @return void
130ecf66805SGreg Roach     */
131e364afe4SGreg Roach    public static function endpush(): void
132c1010edaSGreg Roach    {
13388de55fdSRico Sonntag        $content = ob_get_clean();
13488de55fdSRico Sonntag
135*e50f5fc6SGreg Roach        if ($content === false) {
136*e50f5fc6SGreg Roach            throw new LogicException('found endpush(), but did not find push()');
137*e50f5fc6SGreg Roach        }
138*e50f5fc6SGreg Roach
139d7952a34SGreg Roach        self::$stacks[self::$stack][] = $content;
140d7952a34SGreg Roach    }
141d7952a34SGreg Roach
142d7952a34SGreg Roach    /**
143d7952a34SGreg Roach     * Variant of push that will only add one copy of each item.
144d7952a34SGreg Roach     *
145d7952a34SGreg Roach     * @param string $stack
146d7952a34SGreg Roach     *
147d7952a34SGreg Roach     * @return void
148d7952a34SGreg Roach     */
149e364afe4SGreg Roach    public static function pushunique(string $stack): void
150d7952a34SGreg Roach    {
151d7952a34SGreg Roach        self::$stack = $stack;
152d7952a34SGreg Roach
153d7952a34SGreg Roach        ob_start();
154d7952a34SGreg Roach    }
155d7952a34SGreg Roach
156d7952a34SGreg Roach    /**
157d7952a34SGreg Roach     * Variant of push that will only add one copy of each item.
158d7952a34SGreg Roach     *
159d7952a34SGreg Roach     * @return void
160d7952a34SGreg Roach     */
161e364afe4SGreg Roach    public static function endpushunique(): void
162d7952a34SGreg Roach    {
163d7952a34SGreg Roach        $content = ob_get_clean();
164d7952a34SGreg Roach
165*e50f5fc6SGreg Roach        if ($content === false) {
166*e50f5fc6SGreg Roach            throw new LogicException('found endpushunique(), but did not find pushunique()');
167*e50f5fc6SGreg Roach        }
168*e50f5fc6SGreg Roach
169d7952a34SGreg Roach        self::$stacks[self::$stack][sha1($content)] = $content;
170ecf66805SGreg Roach    }
171ecf66805SGreg Roach
172ecf66805SGreg Roach    /**
173ecf66805SGreg Roach     * Implementation of Blade "stacks".
174ecf66805SGreg Roach     *
175962e29c9SGreg Roach     * @param string $stack
176962e29c9SGreg Roach     *
177ecf66805SGreg Roach     * @return string
178ecf66805SGreg Roach     */
179c1010edaSGreg Roach    public static function stack(string $stack): string
180c1010edaSGreg Roach    {
181ecf66805SGreg Roach        $content = implode('', self::$stacks[$stack] ?? []);
182ecf66805SGreg Roach
183ecf66805SGreg Roach        self::$stacks[$stack] = [];
184ecf66805SGreg Roach
185ecf66805SGreg Roach        return $content;
186ecf66805SGreg Roach    }
187ecf66805SGreg Roach
188ecf66805SGreg Roach    /**
189f3281ad6SGreg Roach     * Render a view.
190f3281ad6SGreg Roach     *
191f3281ad6SGreg Roach     * @return string
19270f31542SGreg Roach     * @throws Throwable
193f3281ad6SGreg Roach     */
1948f53f488SRico Sonntag    public function render(): string
195c1010edaSGreg Roach    {
1964a86d714SGreg Roach        $variables_for_view = $this->data + self::$shared_data;
197e364afe4SGreg Roach        extract($variables_for_view, EXTR_SKIP);
198f3281ad6SGreg Roach
19970f31542SGreg Roach        try {
200f3281ad6SGreg Roach            ob_start();
201bc241c54SGreg Roach            // Do not use require, so we can catch errors for missing files
202bc241c54SGreg Roach            include $this->getFilenameForView($this->name);
20375d70144SGreg Roach
204f3281ad6SGreg Roach            return ob_get_clean();
20570f31542SGreg Roach        } catch (Throwable $ex) {
206d3da353bSGreg Roach            while (ob_get_level() > 0) {
20770f31542SGreg Roach                ob_end_clean();
208d3da353bSGreg Roach            }
20970f31542SGreg Roach            throw $ex;
21070f31542SGreg Roach        }
211f3281ad6SGreg Roach    }
212f3281ad6SGreg Roach
213f3281ad6SGreg Roach    /**
2143e4bf26fSGreg Roach     * @param string $namespace
2153e4bf26fSGreg Roach     * @param string $path
2163e4bf26fSGreg Roach     *
2173e4bf26fSGreg Roach     * @throws InvalidArgumentException
2183e4bf26fSGreg Roach     */
2193e4bf26fSGreg Roach    public static function registerNamespace(string $namespace, string $path): void
2203e4bf26fSGreg Roach    {
2213e4bf26fSGreg Roach        if ($namespace === '') {
2223e4bf26fSGreg Roach            throw new InvalidArgumentException('Cannot register the default namespace');
2233e4bf26fSGreg Roach        }
2243e4bf26fSGreg Roach
2253e4bf26fSGreg Roach        if (!Str::endsWith($path, '/')) {
2263e4bf26fSGreg Roach            throw new InvalidArgumentException('Paths must end with a directory separator');
2273e4bf26fSGreg Roach        }
2283e4bf26fSGreg Roach
2293e4bf26fSGreg Roach        self::$namespaces[$namespace] = $path;
2303e4bf26fSGreg Roach    }
2313e4bf26fSGreg Roach
2323e4bf26fSGreg Roach    /**
2333e4bf26fSGreg Roach     * @param string $old
2343e4bf26fSGreg Roach     * @param string $new
2353e4bf26fSGreg Roach     *
2363e4bf26fSGreg Roach     * @throws InvalidArgumentException
2373e4bf26fSGreg Roach     */
2383e4bf26fSGreg Roach    public static function registerCustomView(string $old, string $new): void
2393e4bf26fSGreg Roach    {
2403e4bf26fSGreg Roach        if (Str::contains($old, self::NAMESPACE_SEPARATOR) && Str::contains($new, self::NAMESPACE_SEPARATOR)) {
2413e4bf26fSGreg Roach            self::$replacements[$old] = $new;
2423e4bf26fSGreg Roach        } else {
2433e4bf26fSGreg Roach            throw new InvalidArgumentException();
2443e4bf26fSGreg Roach        }
2453e4bf26fSGreg Roach    }
2463e4bf26fSGreg Roach
2473e4bf26fSGreg Roach    /**
2483e4bf26fSGreg Roach     * Find the file for a view.
249f3281ad6SGreg Roach     *
250f3281ad6SGreg Roach     * @param string $view_name
251f3281ad6SGreg Roach     *
25275d70144SGreg Roach     * @return string
25350c68a25SGreg Roach     * @throws Exception
254f3281ad6SGreg Roach     */
2553e4bf26fSGreg Roach    public function getFilenameForView(string $view_name): string
256c1010edaSGreg Roach    {
257fdbcd0efSGreg Roach        // If we request "::view", then use it explicityly.  Don't allow replacements.
258fdbcd0efSGreg Roach        $explicit = Str::startsWith($view_name, self::NAMESPACE_SEPARATOR);
259fdbcd0efSGreg Roach
2603e4bf26fSGreg Roach        if (!Str::contains($view_name, self::NAMESPACE_SEPARATOR)) {
2613e4bf26fSGreg Roach            $view_name = self::NAMESPACE_SEPARATOR . $view_name;
2623e4bf26fSGreg Roach        }
26375d70144SGreg Roach
2643e4bf26fSGreg Roach        // Apply replacements / customisations
265fdbcd0efSGreg Roach        while (!$explicit && array_key_exists($view_name, self::$replacements)) {
2663e4bf26fSGreg Roach            $view_name = self::$replacements[$view_name];
2673e4bf26fSGreg Roach        }
2683e4bf26fSGreg Roach
2693e4bf26fSGreg Roach        [$namespace, $view_name] = explode(self::NAMESPACE_SEPARATOR, $view_name, 2);
2703e4bf26fSGreg Roach
2713e4bf26fSGreg Roach        if ((self::$namespaces[$namespace] ?? null) === null) {
2723e4bf26fSGreg Roach            throw new RuntimeException('Namespace "' . e($namespace) .  '" not found.');
2733e4bf26fSGreg Roach        }
2743e4bf26fSGreg Roach
2753e4bf26fSGreg Roach        $view_file = self::$namespaces[$namespace] . $view_name . self::TEMPLATE_EXTENSION;
2763e4bf26fSGreg Roach
2773e4bf26fSGreg Roach        if (!is_file($view_file)) {
2783e4bf26fSGreg Roach            throw new RuntimeException('View file not found: ' . e($view_file));
2793e4bf26fSGreg Roach        }
2803e4bf26fSGreg Roach
28150c68a25SGreg Roach        return $view_file;
282f21917b2SGreg Roach    }
28350c68a25SGreg Roach
284f3281ad6SGreg Roach    /**
285f3281ad6SGreg Roach     * Cerate and render a view in a single operation.
286f3281ad6SGreg Roach     *
287f3281ad6SGreg Roach     * @param string  $name
288f3281ad6SGreg Roach     * @param mixed[] $data
289f3281ad6SGreg Roach     *
290f3281ad6SGreg Roach     * @return string
291f3281ad6SGreg Roach     */
2928f53f488SRico Sonntag    public static function make($name, $data = []): string
293c1010edaSGreg Roach    {
294f3281ad6SGreg Roach        $view = new static($name, $data);
295f3281ad6SGreg Roach
29644116e73SGreg Roach        DebugBar::addView($name, $data);
29744116e73SGreg Roach
298f3281ad6SGreg Roach        return $view->render();
299f3281ad6SGreg Roach    }
300f3281ad6SGreg Roach}
301