1
0

view.abstract.class.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Juju\App;
  3. require_once(realpath(dirname(__DIR__).'/Http/response.class.php'));
  4. require_once(realpath(dirname(__DIR__).'/Data/template.class.php'));
  5. use Juju\Data\{Response, Template};
  6. abstract class View {
  7. protected static $name;
  8. protected static $template;
  9. public static function views() : array{
  10. return array_values(array_filter(get_declared_classes(), function($class){
  11. return 0 === strpos($class, "View\\");
  12. }));
  13. }
  14. public static function name() : string{
  15. if(is_null(static::$name)){
  16. $name = get_called_class();
  17. return substr($name, strrpos($name, '\\') + 1);
  18. }
  19. return static::$name;
  20. }
  21. public static function data(){
  22. return [
  23. 'view'=>get_called_class()
  24. ];
  25. }
  26. final public static function template(){
  27. if(is_null(static::$template) && method_exists(get_called_class(), 'setup')){
  28. static::setup();
  29. }
  30. $template = static::$template ?? static::name();
  31. if(is_string($template)){
  32. $template = Template::get($template);
  33. }elseif(is_array($template)){
  34. $template = new Template($template['name'], $template['fn']);
  35. }
  36. return $template;
  37. }
  38. final public static function render(array $data = []) : string{
  39. $data = array_merge(static::data(), $data);
  40. return static::template()->run($data);
  41. }
  42. final public static function cache(){
  43. foreach(self::views() as $view){
  44. if(method_exists($view, 'setup')){
  45. $view::setup();
  46. }
  47. $template = $view::template();
  48. if(!is_null($template)){
  49. $template->to_file();
  50. }
  51. }
  52. }
  53. }
  54. ?>