Application.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Core;
  3. use App\Core\DB\Database;
  4. use Exception;
  5. /*
  6. * Núcleo de la aplicación.
  7. */
  8. class Application
  9. {
  10. const EVENT_BEFORE_REQUEST = 'beforeRequest';
  11. const EVENT_AFTER_REQUEST = 'afterRequest';
  12. protected array $eventListeners = [];
  13. public static string $ROOT_DIR;
  14. public string $layout = 'main';
  15. public string $userClass;
  16. public Router $router;
  17. public Request $request;
  18. public Response $response;
  19. public View $view;
  20. public Session $session;
  21. public Database $db;
  22. public $userAuth = null;
  23. public static Application $app;
  24. public ?Controller $controller = null;
  25. public function __construct(string $rootPath, array $config)
  26. {
  27. self::$ROOT_DIR = $rootPath;
  28. $this->userClass = $config['userClass'];
  29. self::$app = $this;
  30. $this->request = new Request();
  31. $this->response = new Response();
  32. $this->router = new Router($this->request, $this->response);
  33. $this->view = new View();
  34. $this->session = new Session();
  35. $this->db = new Database($config['db']);
  36. $userAuth = $this->session->get('userAuth');
  37. // Obtiene la información del usuario autenticado.
  38. if ($userAuth) {
  39. $primaryKey = $this->userClass::primaryKey();
  40. $this->userAuth = $this->userClass::findOne([$primaryKey => $userAuth]);
  41. }
  42. }
  43. public function getController()
  44. {
  45. return $this->controller;
  46. }
  47. public function setController(Controller $controller)
  48. {
  49. $this->controller = $controller;
  50. }
  51. /*
  52. * Establece la variable de sesión
  53. * del usuario autenticado.
  54. */
  55. public function login($userAuth)
  56. {
  57. $this->userAuth = $userAuth;
  58. $primaryKey = $this->userClass::primaryKey();
  59. if (empty($this->userAuth->{$primaryKey})) {
  60. return false;
  61. }
  62. $this->session->set('userAuth', $this->userAuth->{$primaryKey});
  63. return true;
  64. }
  65. /*
  66. * Elimina la variable de sesión
  67. * del usuario autenticado.
  68. */
  69. public function logout()
  70. {
  71. $this->session->remove('userAuth');
  72. }
  73. /*
  74. * Comprueba si existe un usuario autenticado.
  75. */
  76. public static function isGuest()
  77. {
  78. return empty(self::$app->userAuth);
  79. }
  80. /*
  81. * Obtiene el nombre del usuario autenticado.
  82. */
  83. public function getUserAuthName()
  84. {
  85. if (empty($this->userAuth)) {
  86. return $this->userAuth;
  87. }
  88. return $this->userAuth->firstname.' '.$this->userAuth->lastname;
  89. }
  90. /*
  91. * Ejecuta la aplicación.
  92. */
  93. public function run()
  94. {
  95. $this->triggerEvent(self::EVENT_BEFORE_REQUEST);
  96. try {
  97. echo $this->router->resolve();
  98. } catch (Exception $e) {
  99. $this->response->setStatusCode($e->getCode());
  100. echo $this->view->renderView('errors', [
  101. 'execption' => $e,
  102. ]);
  103. }
  104. $this->triggerEvent(self::EVENT_AFTER_REQUEST);
  105. }
  106. /*
  107. * Almacena eventos que serán ejecutados.
  108. */
  109. public function on(string $eventName, $callback)
  110. {
  111. $this->eventListeners[$eventName][] = $callback;
  112. }
  113. /*
  114. * Ejecuta los eventos de la aplicaión.
  115. */
  116. public function triggerEvent(string $eventName)
  117. {
  118. $callbacks = $this->eventListeners[$eventName] ?? [];
  119. foreach ($callbacks as $callback) {
  120. call_user_func($callback);
  121. }
  122. }
  123. }