view.abstract.class.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. private static $name;
  8. protected static $template;
  9. public static function views() : array{
  10. return 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 render(array $data = []) : string{
  27. if(is_null(static::$template) && method_exists(get_called_class(), 'setup')){
  28. static::setup();
  29. }
  30. $template = static::$template ?? static::name();
  31. $data = array_merge(static::data(),$data);
  32. if(is_string($template)){
  33. return Template::from($template, $data);
  34. }elseif(is_array($template)){
  35. $template = new Template($template['name'], $template['fn']);
  36. }
  37. return $template->run($data);
  38. }
  39. }
  40. ?>