Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
15 / 15
Template
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
15 / 15
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 __toString
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 get
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 exists
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 toHtml
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
8 / 8
<?php
namespace Win\Templates;
use Throwable;
use Win\Application;
/**
 * Templates em .PHTML
 * Ver arquivos em: "templates/"
 */
class Template
{
    public static $dir = 'templates';
    public Application $app;
    /**
     * Endereço completo do arquivo .phtml
     * @var string
     */
    protected string $file;
    protected $data = [];
    /**
     * Cria um template com base no arquivo escolhido
     * @param string $file Nome do arquivo
     * @param mixed[] $data Array de variáveis
     */
    public function __construct($file, $data = [])
    {
        $this->app = Application::app();
        $this->file = BASE_PATH . '/' . static::$dir . "/$file.phtml";
        $this->data = $data;
    }
    /**
     * Carrega e retorna o output
     * @return string
     */
    public function __toString()
    {
        return (string) $this->toHtml();
    }
    /**
     * Retorna uma variável
     * @param string $name
     * @return mixed|null
     */
    public function get($name)
    {
        return $this->data[$name] ?? null;
    }
    /**
     * Retorna TRUE se o template existe
     * @return bool
     */
    public function exists()
    {
        return file_exists($this->file);
    }
    /**
     * Carrega e retorna o output
     * @return string
     */
    protected function toHtml()
    {
        ob_start();
        try {
            if (isset($this->file) && $this->exists()) {
                extract($this->data);
                include $this->file;
            }
        } catch (Throwable $e) {
            ob_get_clean();
            throw $e;
        }
        return ob_get_clean();
    }
}