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 string Implementation of Blade "stacks". 34 */ 35 private static $stack; 36 37 /** 38 * @var array[] Implementation of Blade "stacks". 39 */ 40 private static $stacks = []; 41 42 /** 43 * Createa view from a template name and optional data. 44 * 45 * @param $name 46 * @param array $data 47 */ 48 public function __construct($name, $data = []) { 49 $this->name = $name; 50 $this->data = $data; 51 } 52 53 /** 54 * Implementation of Blade "stacks". 55 * 56 * @see https://laravel.com/docs/5.5/blade#stacks 57 */ 58 public static function push(string $stack) { 59 self::$stack = $stack; 60 ob_start(); 61 } 62 63 /** 64 * Implementation of Blade "stacks". 65 */ 66 public static function endpush() { 67 self::$stacks[self::$stack][] = ob_get_clean(); 68 } 69 70 /** 71 * Implementation of Blade "stacks". 72 * 73 * @return string 74 */ 75 public static function stack(string $stack): string { 76 $content = implode('', self::$stacks[$stack] ?? []); 77 78 self::$stacks[$stack] = []; 79 80 return $content; 81 } 82 83 /** 84 * Render a view. 85 * 86 * @return string 87 */ 88 public function render() { 89 extract($this->data); 90 91 ob_start(); 92 require $this->getFilenameForView($this->name); 93 94 return ob_get_clean(); 95 } 96 97 /** 98 * Allow a theme to override the default views. 99 * 100 * @param string $view_name 101 * 102 * @return string 103 */ 104 public static function getFilenameForView($view_name) { 105 $view_file = $view_name . '.php'; 106 //$theme_view = WT_THEMES_DIR . Theme::theme()->themeId() . '/resources/views/' . $view_file; 107 108 //if (file_exists($theme_view)) { 109 // return $theme_view; 110 //} else { 111 return WT_ROOT . 'resources/views/' . $view_file; 112 //} 113 } 114 115 /** 116 * Cerate and render a view in a single operation. 117 * 118 * @param string $name 119 * @param mixed[] $data 120 * 121 * @return string 122 */ 123 public static function make($name, $data = []) { 124 $view = new static($name, $data); 125 126 DebugBar::addView($name, $data); 127 128 return $view->render(); 129 } 130} 131