xref: /webtrees/app/View.php (revision 571e6fcac743cc9b048486db27eb10ae509eb172)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees;
17
18/**
19 * Simple view/template class.
20 */
21class View {
22	/**
23	 * @var string The (file) name of the view.
24	 */
25	private $name;
26
27	/**
28	 * @var mixed[] Data to be inserted into the view.
29	 */
30	private $data;
31
32	/**
33	 * @var mixed[] Data to be inserted into all views.
34	 */
35	private static $shared_data = [];
36
37	/**
38	 * @var string Implementation of Blade "stacks".
39	 */
40	private static $stack;
41
42	/**
43	 * @var array[] Implementation of Blade "stacks".
44	 */
45	private static $stacks = [];
46
47	/**
48	 * Createa view from a template name and optional data.
49	 *
50	 * @param       $name
51	 * @param array $data
52	 */
53	public function __construct($name, $data = []) {
54		$this->name = $name;
55		$this->data = $data;
56	}
57
58	/**
59	 * Shared data that is available to all views.
60	 */
61	public static function share($key, $value) {
62		self::$shared_data[$key] = $value;
63	}
64
65	/**
66	 * Implementation of Blade "stacks".
67	 *
68	 * @see https://laravel.com/docs/5.5/blade#stacks
69	 */
70	public static function push(string $stack) {
71		self::$stack = $stack;
72		ob_start();
73	}
74
75	/**
76	 * Implementation of Blade "stacks".
77	 */
78	public static function endpush() {
79		self::$stacks[self::$stack][] = ob_get_clean();
80	}
81
82	/**
83	 * Implementation of Blade "stacks".
84	 *
85	 * @return string
86	 */
87	public static function stack(string $stack): string {
88		$content = implode('', self::$stacks[$stack] ?? []);
89
90		self::$stacks[$stack] = [];
91
92		return $content;
93	}
94
95	/**
96	 * Render a view.
97	 *
98	 * @return string
99	 */
100	public function render() {
101		extract($this->data + self::$shared_data);
102
103		ob_start();
104		require $this->getFilenameForView($this->name);
105
106		return ob_get_clean();
107	}
108
109	/**
110	 * Allow a theme to override the default views.
111	 *
112	 * @param string $view_name
113	 *
114	 * @return string
115	 */
116	public static function getFilenameForView($view_name) {
117		$view_file  = '/resources/views/' . $view_name . '.php';
118		$theme_view = WT_ROOT . WT_THEMES_DIR . Theme::theme()->themeId() . $view_file;
119
120		if (is_file($theme_view)) {
121			return $theme_view;
122		} else {
123			return WT_ROOT . $view_file;
124		}
125	}
126
127	/**
128	 * Cerate and render a view in a single operation.
129	 *
130	 * @param string  $name
131	 * @param mixed[] $data
132	 *
133	 * @return string
134	 */
135	public static function make($name, $data = []) {
136		$view = new static($name, $data);
137
138		DebugBar::addView($name, $data);
139
140		return $view->render();
141	}
142}
143