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 * @param string $key 62 * @param mixed $value 63 */ 64 public static function share(string $key, $value) { 65 self::$shared_data[$key] = $value; 66 } 67 68 /** 69 * Implementation of Blade "stacks". 70 * 71 * @see https://laravel.com/docs/5.5/blade#stacks 72 * 73 * @param string $stack 74 */ 75 public static function push(string $stack) { 76 self::$stack = $stack; 77 ob_start(); 78 } 79 80 /** 81 * Implementation of Blade "stacks". 82 */ 83 public static function endpush() { 84 self::$stacks[self::$stack][] = ob_get_clean(); 85 } 86 87 /** 88 * Implementation of Blade "stacks". 89 * 90 * @param string $stack 91 * 92 * @return string 93 */ 94 public static function stack(string $stack): string { 95 $content = implode('', self::$stacks[$stack] ?? []); 96 97 self::$stacks[$stack] = []; 98 99 return $content; 100 } 101 102 /** 103 * Render a view. 104 * 105 * @return string 106 */ 107 public function render() { 108 extract($this->data + self::$shared_data); 109 110 ob_start(); 111 // Do not use require, so we can catch errors for missing files 112 include $this->getFilenameForView($this->name); 113 114 return ob_get_clean(); 115 } 116 117 /** 118 * Allow a theme to override the default views. 119 * 120 * @param string $view_name 121 * 122 * @return string 123 */ 124 public static function getFilenameForView($view_name) { 125 $view_file = '/resources/views/' . $view_name . '.php'; 126 $theme_view = WT_ROOT . WT_THEMES_DIR . Theme::theme()->themeId() . $view_file; 127 128 if (is_file($theme_view)) { 129 return $theme_view; 130 } else { 131 return WT_ROOT . $view_file; 132 } 133 } 134 135 /** 136 * Cerate and render a view in a single operation. 137 * 138 * @param string $name 139 * @param mixed[] $data 140 * 141 * @return string 142 */ 143 public static function make($name, $data = []) { 144 $view = new static($name, $data); 145 146 DebugBar::addView($name, $data); 147 148 return $view->render(); 149 } 150} 151