. */ declare(strict_types=1); namespace Fisharebest\Webtrees; use Exception; use Illuminate\Support\Str; use InvalidArgumentException; use RuntimeException; use Throwable; use function array_key_exists; use function explode; use function extract; use function implode; use function is_file; use function ob_end_clean; use function ob_start; use function sha1; /** * Simple view/template class. */ class View { public const NAMESPACE_SEPARATOR = '::'; // File extension for our template files. private const TEMPLATE_EXTENSION = '.phtml'; /** * @var string The (file) name of the view. */ private $name; /** * @var mixed[] Data to be inserted into the view. */ private $data; /** * @var string[] Where do the templates live, for each namespace. */ private static $namespaces = [ '' => WT_ROOT . 'resources/views/', ]; /** * @var string[] Modules can replace core views with their own. */ private static $replacements = []; /** * @var mixed[] Data to be inserted into all views. */ private static $shared_data = []; /** * @var string Implementation of Blade "stacks". */ private static $stack; /** * @var array[] Implementation of Blade "stacks". */ private static $stacks = []; /** * Createa view from a template name and optional data. * * @param string $name * @param array $data */ public function __construct(string $name, $data = []) { $this->name = $name; $this->data = $data; } /** * Shared data that is available to all views. * * @param string $key * @param mixed $value * * @return void */ public static function share(string $key, $value): void { self::$shared_data[$key] = $value; } /** * Implementation of Blade "stacks". * * @see https://laravel.com/docs/5.5/blade#stacks * * @param string $stack * * @return void */ public static function push(string $stack): void { self::$stack = $stack; ob_start(); } /** * Implementation of Blade "stacks". * * @return void */ public static function endpush(): void { $content = ob_get_clean(); self::$stacks[self::$stack][] = $content; } /** * Variant of push that will only add one copy of each item. * * @param string $stack * * @return void */ public static function pushunique(string $stack): void { self::$stack = $stack; ob_start(); } /** * Variant of push that will only add one copy of each item. * * @return void */ public static function endpushunique(): void { $content = ob_get_clean(); self::$stacks[self::$stack][sha1($content)] = $content; } /** * Implementation of Blade "stacks". * * @param string $stack * * @return string */ public static function stack(string $stack): string { $content = implode('', self::$stacks[$stack] ?? []); self::$stacks[$stack] = []; return $content; } /** * Render a view. * * @return string * @throws Throwable */ public function render(): string { $variables_for_view = $this->data + self::$shared_data; extract($variables_for_view, EXTR_SKIP); try { ob_start(); // Do not use require, so we can catch errors for missing files include $this->getFilenameForView($this->name); return ob_get_clean(); } catch (Throwable $ex) { ob_end_clean(); throw $ex; } } /** * @param string $namespace * @param string $path * * @throws InvalidArgumentException */ public static function registerNamespace(string $namespace, string $path): void { if ($namespace === '') { throw new InvalidArgumentException('Cannot register the default namespace'); } if (!Str::endsWith($path, '/')) { throw new InvalidArgumentException('Paths must end with a directory separator'); } self::$namespaces[$namespace] = $path; } /** * @param string $old * @param string $new * * @throws InvalidArgumentException */ public static function registerCustomView(string $old, string $new): void { if (Str::contains($old, self::NAMESPACE_SEPARATOR) && Str::contains($new, self::NAMESPACE_SEPARATOR)) { self::$replacements[$old] = $new; } else { throw new InvalidArgumentException(); } } /** * Find the file for a view. * * @param string $view_name * * @return string * @throws Exception */ public function getFilenameForView(string $view_name): string { if (!Str::contains($view_name, self::NAMESPACE_SEPARATOR)) { $view_name = self::NAMESPACE_SEPARATOR . $view_name; } // Apply replacements / customisations while (array_key_exists($view_name, self::$replacements)) { $view_name = self::$replacements[$view_name]; } [$namespace, $view_name] = explode(self::NAMESPACE_SEPARATOR, $view_name, 2); if ((self::$namespaces[$namespace] ?? null) === null) { throw new RuntimeException('Namespace "' . e($namespace) . '" not found.'); } $view_file = self::$namespaces[$namespace] . $view_name . self::TEMPLATE_EXTENSION; if (!is_file($view_file)) { throw new RuntimeException('View file not found: ' . e($view_file)); } return $view_file; } /** * Cerate and render a view in a single operation. * * @param string $name * @param mixed[] $data * * @return string */ public static function make($name, $data = []): string { $view = new static($name, $data); DebugBar::addView($name, $data); return $view->render(); } }