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 // Do not use require, so we can catch errors for missing files 105 include $this->getFilenameForView($this->name); 106 107 return ob_get_clean(); 108 } 109 110 /** 111 * Allow a theme to override the default views. 112 * 113 * @param string $view_name 114 * 115 * @return string 116 */ 117 public static function getFilenameForView($view_name) { 118 $view_file = '/resources/views/' . $view_name . '.php'; 119 $theme_view = WT_ROOT . WT_THEMES_DIR . Theme::theme()->themeId() . $view_file; 120 121 if (is_file($theme_view)) { 122 return $theme_view; 123 } else { 124 return WT_ROOT . $view_file; 125 } 126 } 127 128 /** 129 * Cerate and render a view in a single operation. 130 * 131 * @param string $name 132 * @param mixed[] $data 133 * 134 * @return string 135 */ 136 public static function make($name, $data = []) { 137 $view = new static($name, $data); 138 139 DebugBar::addView($name, $data); 140 141 return $view->render(); 142 } 143} 144