__Output.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Core\Template;
  3. class Output {
  4. private static $_instance;
  5. private $view = false;
  6. protected $values = Array();
  7. private $_template = '';
  8. private $_extension = '';
  9. private function __construct() {
  10. $this->_template = ( defined('TEMPLATE_DEFAULT') ) ? TEMPLATE_DEFAULT : 'DefaultTemplate';
  11. $this->_extension = ( defined('TEMPLATE_FILETYPE') ) ? TEMPLATE_FILETYPE : '.mustache';
  12. }
  13. private static function newObj() {
  14. if (!isset(self::$_instance)) {
  15. self::$_instance = new Output();
  16. }
  17. return self::$_instance;
  18. }
  19. public function getInstance() {
  20. if (!isset(self::$_instance)) {
  21. return self::newObj();
  22. }
  23. return self::$_instance;
  24. }
  25. public function setTemplate($template = 'DefaultTemplate') {
  26. self::getInstance()->_template = $template;
  27. // self::getInstance()->_template = '';
  28. return self::getInstance();
  29. }
  30. public static function renderView($name, $values = null, $location = null) {
  31. if (!$location) {
  32. $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']);
  33. array_pop($segments);
  34. $location = implode('/', $segments) . '/views/';
  35. }
  36. self::getInstance()->setView($name, $values, $location)->render();
  37. }
  38. public static function setView($name = '', $values = Array(), $location = null) {
  39. if($name == ''){ return self::getInstance(); }
  40. $instance = self::getInstance();
  41. $folder = '';
  42. if (!$location) {
  43. $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']);
  44. array_pop($segments);
  45. $location = implode('/', $segments) . '/views/';
  46. }
  47. $folder = $location;
  48. $location .= $name;
  49. if (strpos($instance->_extension, $location) == 0) {
  50. $location .= $instance->_extension;
  51. }
  52. $instance->view = Array();
  53. $instance->view['location'] = $location;
  54. $instance->view['folder'] = $folder;
  55. $instance->view['values'] = $values;
  56. return self::getInstance();
  57. }
  58. public static function addValue($key, $value){
  59. self::getInstance()->view['values'][$key] = $value;
  60. }
  61. public static function render() {
  62. $instance = self::getInstance();
  63. if(!is_file(__DIR__."/classes/".strtolower($instance->_template)."/".$instance->_template.'.php') ){
  64. $instance->_template = 'DefaultTemplate';
  65. }
  66. include_once __DIR__."/classes/".strtolower($instance->_template)."/".$instance->_template.'.php';
  67. $class = new $instance->_template;
  68. $class->setView($instance->view)->render();
  69. }
  70. public static function addMenu($route, $name, $icon = '', $attributes = [], $weight = 0){
  71. global $SIDEBAR;
  72. $SIDEBAR->add($route, $name, $icon, $attributes, $weight);
  73. }
  74. public static function addSubmenu($key, $name, $icon = '', $attributes = [], $weight = 0){
  75. global $SIDEBAR;
  76. $SIDEBAR->create_submenu($key, $name, $icon, $attributes , $weight);
  77. }
  78. public static function addOnSubmenu($key, $route, $name, $icon = '', $attributes = [], $weight = 0){
  79. global $SIDEBAR;
  80. $SIDEBAR->add_on_new($key, $route, $name, $icon, $attributes, $weight);
  81. }
  82. //========
  83. public static function headAdd($route, $name){
  84. global $DROPDOWN;
  85. $DROPDOWN[] = ['route' => $route, 'name' => $name];
  86. }
  87. public static function addSubHeader($key, $name, $icon = '', $attributes = [], $weight = 0){
  88. global $DROPDOWN;
  89. $DROPDOWN->create_submenu($key, $name, $icon, $attributes , $weight);
  90. }
  91. public static function addOnSubHeader($key, $route, $name, $icon = '', $attributes = [], $weight = 0){
  92. global $DROPDOWN;
  93. $DROPDOWN->add_on_new($key, $route, $name, $icon, $attributes, $weight);
  94. }
  95. public static function icon($icon){
  96. return "<i class='$icon' aria-hidden='true'></i>";
  97. }
  98. }