12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
- namespace Juju\App;
- require_once(realpath(dirname(__DIR__).'/Http/response.class.php'));
- require_once(realpath(dirname(__DIR__).'/Data/template.class.php'));
- use Juju\Data\{Response, Template};
- abstract class View {
- protected static $name;
- protected static $template;
- public static function views() : array{
- return array_values(array_filter(get_declared_classes(), function($class){
- return 0 === strpos($class, "View\\");
- }));
- }
- public static function name() : string{
- if(is_null(static::$name)){
- $name = get_called_class();
- return substr($name, strrpos($name, '\\') + 1);
- }
- return static::$name;
- }
- public static function data(){
- return [
- 'view'=>get_called_class()
- ];
- }
- final public static function template(){
- if(is_null(static::$template) && method_exists(get_called_class(), 'setup')){
- static::setup();
- }
- $template = static::$template ?? static::name();
- if(is_string($template)){
- $template = Template::get($template);
- }elseif(is_array($template)){
- $template = new Template($template['name'], $template['fn']);
- }
- return $template;
- }
- final public static function render(array $data = []) : string{
- $data = array_merge(static::data(), $data);
- return static::template()->run($data);
- }
- final public static function cache(){
- foreach(self::views() as $view){
- if(method_exists($view, 'setup')){
- $view::setup();
- }
- $template = $view::template();
- if(!is_null($template)){
- $template->to_file();
- }
- }
- }
- }
- ?>
|