|
|
|
@ -17,40 +17,24 @@ namespace flight\template;
|
|
|
|
|
*/
|
|
|
|
|
class View
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Location of view templates.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
public $path;
|
|
|
|
|
/** Location of view templates. */
|
|
|
|
|
public string $path;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* File extension.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
public $extension = '.php';
|
|
|
|
|
/** File extension. */
|
|
|
|
|
public string $extension = '.php';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* View variables.
|
|
|
|
|
*
|
|
|
|
|
* @var array<string, mixed>
|
|
|
|
|
*/
|
|
|
|
|
protected $vars = [];
|
|
|
|
|
/** @var array<string, mixed> View variables. */
|
|
|
|
|
protected array $vars = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Template file.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $template;
|
|
|
|
|
/** Template file. */
|
|
|
|
|
private string $template;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
*
|
|
|
|
|
* @param string $path Path to templates directory
|
|
|
|
|
*/
|
|
|
|
|
public function __construct($path = '.')
|
|
|
|
|
public function __construct(string $path = '.')
|
|
|
|
|
{
|
|
|
|
|
$this->path = $path;
|
|
|
|
|
}
|
|
|
|
@ -58,11 +42,9 @@ class View
|
|
|
|
|
/**
|
|
|
|
|
* Gets a template variable.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key Key
|
|
|
|
|
*
|
|
|
|
|
* @return mixed Value
|
|
|
|
|
* @return mixed Variable value or `null` if doesn't exists
|
|
|
|
|
*/
|
|
|
|
|
public function get($key)
|
|
|
|
|
public function get(string $key)
|
|
|
|
|
{
|
|
|
|
|
return $this->vars[$key] ?? null;
|
|
|
|
|
}
|
|
|
|
@ -70,13 +52,13 @@ class View
|
|
|
|
|
/**
|
|
|
|
|
* Sets a template variable.
|
|
|
|
|
*
|
|
|
|
|
* @param string|iterable<string, mixed> $key Key
|
|
|
|
|
* @param string|iterable<string, mixed> $key
|
|
|
|
|
* @param mixed $value Value
|
|
|
|
|
* @return static
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function set($key, $value = null)
|
|
|
|
|
public function set($key, $value = null): self
|
|
|
|
|
{
|
|
|
|
|
if (\is_array($key) || \is_object($key)) {
|
|
|
|
|
if (\is_iterable($key)) {
|
|
|
|
|
foreach ($key as $k => $v) {
|
|
|
|
|
$this->vars[$k] = $v;
|
|
|
|
|
}
|
|
|
|
@ -90,11 +72,9 @@ class View
|
|
|
|
|
/**
|
|
|
|
|
* Checks if a template variable is set.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key Key
|
|
|
|
|
*
|
|
|
|
|
* @return bool If key exists
|
|
|
|
|
*/
|
|
|
|
|
public function has($key)
|
|
|
|
|
public function has(string $key): bool
|
|
|
|
|
{
|
|
|
|
|
return isset($this->vars[$key]);
|
|
|
|
|
}
|
|
|
|
@ -102,10 +82,9 @@ class View
|
|
|
|
|
/**
|
|
|
|
|
* Unsets a template variable. If no key is passed in, clear all variables.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key Key
|
|
|
|
|
* @return static
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function clear($key = null)
|
|
|
|
|
public function clear(?string $key = null): self
|
|
|
|
|
{
|
|
|
|
|
if (null === $key) {
|
|
|
|
|
$this->vars = [];
|
|
|
|
@ -120,25 +99,24 @@ class View
|
|
|
|
|
* Renders a template.
|
|
|
|
|
*
|
|
|
|
|
* @param string $file Template file
|
|
|
|
|
* @param array<string, mixed> $data Template data
|
|
|
|
|
* @param array<string, mixed> $data Template data
|
|
|
|
|
*
|
|
|
|
|
* @throws \Exception If template not found
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function render($file, $data = null)
|
|
|
|
|
public function render(string $file, ?array $data = null): void
|
|
|
|
|
{
|
|
|
|
|
$this->template = $this->getTemplate($file);
|
|
|
|
|
|
|
|
|
|
if (!file_exists($this->template)) {
|
|
|
|
|
if (!\file_exists($this->template)) {
|
|
|
|
|
$this->template = self::normalizePath($this->template);
|
|
|
|
|
throw new \Exception("Template file not found: {$this->template}.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (\is_array($data)) {
|
|
|
|
|
$this->vars = array_merge($this->vars, $data);
|
|
|
|
|
$this->vars = \array_merge($this->vars, $data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extract($this->vars);
|
|
|
|
|
\extract($this->vars);
|
|
|
|
|
|
|
|
|
|
include $this->template;
|
|
|
|
|
}
|
|
|
|
@ -147,17 +125,17 @@ class View
|
|
|
|
|
* Gets the output of a template.
|
|
|
|
|
*
|
|
|
|
|
* @param string $file Template file
|
|
|
|
|
* @param array<string, mixed> $data Template data
|
|
|
|
|
* @param ?array<string, mixed> $data Template data
|
|
|
|
|
*
|
|
|
|
|
* @return string Output of template
|
|
|
|
|
*/
|
|
|
|
|
public function fetch($file, $data = null)
|
|
|
|
|
public function fetch(string $file, ?array $data = null): string
|
|
|
|
|
{
|
|
|
|
|
ob_start();
|
|
|
|
|
\ob_start();
|
|
|
|
|
|
|
|
|
|
$this->render($file, $data);
|
|
|
|
|
|
|
|
|
|
return ob_get_clean();
|
|
|
|
|
return \ob_get_clean();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -167,9 +145,9 @@ class View
|
|
|
|
|
*
|
|
|
|
|
* @return bool Template file exists
|
|
|
|
|
*/
|
|
|
|
|
public function exists($file)
|
|
|
|
|
public function exists(string $file): bool
|
|
|
|
|
{
|
|
|
|
|
return file_exists($this->getTemplate($file));
|
|
|
|
|
return \file_exists($this->getTemplate($file));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -179,17 +157,17 @@ class View
|
|
|
|
|
*
|
|
|
|
|
* @return string Template file location
|
|
|
|
|
*/
|
|
|
|
|
public function getTemplate($file)
|
|
|
|
|
public function getTemplate(string $file): string
|
|
|
|
|
{
|
|
|
|
|
$ext = $this->extension;
|
|
|
|
|
|
|
|
|
|
if (!empty($ext) && (substr($file, -1 * \strlen($ext)) != $ext)) {
|
|
|
|
|
if (!empty($ext) && (\substr($file, -1 * \strlen($ext)) != $ext)) {
|
|
|
|
|
$file .= $ext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$is_windows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
|
|
|
|
|
$is_windows = \strtoupper(\substr(PHP_OS, 0, 3)) === 'WIN';
|
|
|
|
|
|
|
|
|
|
if (('/' == substr($file, 0, 1)) || ($is_windows === true && ':' == substr($file, 1, 1))) {
|
|
|
|
|
if (('/' == \substr($file, 0, 1)) || ($is_windows === true && ':' == \substr($file, 1, 1))) {
|
|
|
|
|
return $file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -203,15 +181,15 @@ class View
|
|
|
|
|
*
|
|
|
|
|
* @return string Escaped string
|
|
|
|
|
*/
|
|
|
|
|
public function e($str)
|
|
|
|
|
public function e(string $str): string
|
|
|
|
|
{
|
|
|
|
|
$value = htmlentities($str);
|
|
|
|
|
$value = \htmlentities($str);
|
|
|
|
|
echo $value;
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static function normalizePath(string $path, string $separator = DIRECTORY_SEPARATOR): string
|
|
|
|
|
{
|
|
|
|
|
return str_replace(['\\', '/'], $separator, $path);
|
|
|
|
|
return \str_replace(['\\', '/'], $separator, $path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|