Kernel.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace KarmaFW;
  3. use \KarmaFW\App\Container;
  4. use \KarmaFW\App\Pipe;
  5. use \KarmaFW\App\Tools;
  6. use \KarmaFW\Database\Sql\SqlDb;
  7. use \KarmaFW\Http\Request;
  8. use \KarmaFW\Http\Response;
  9. define('FW_SRC_DIR', __DIR__);
  10. define('FW_DIR', __DIR__ . "/..");
  11. if (! defined('APP_DIR')) {
  12. echo "ERROR: Please, define APP_DIR" . PHP_EOL;
  13. exit(1);
  14. }
  15. class Kernel
  16. {
  17. protected static $booted = false;
  18. protected static $helpers_dirs = [
  19. APP_DIR . "/src/helpers",
  20. FW_SRC_DIR . "/helpers",
  21. ];
  22. protected $db = null;
  23. protected $middlewares;
  24. protected $container;
  25. public function __construct($middlewares=[])
  26. {
  27. $this->middlewares = $middlewares;
  28. $this->container = new Container;
  29. try {
  30. $this->configure();
  31. } catch (\Exception $e) {
  32. header("HTTP/1.0 500 Internal Server Error");
  33. echo "<h1>Internal Server error</h1>";
  34. if (ENV === 'dev') {
  35. echo "<pre>";
  36. print_r($e);
  37. echo "</pre>";
  38. }
  39. exit;
  40. }
  41. }
  42. public function configure()
  43. {
  44. if (is_file(APP_DIR . '/config/config.php')) {
  45. require APP_DIR . '/config/config.php';
  46. }
  47. if (! defined('APP_NAME')) {
  48. define('APP_NAME', "PHP Application");
  49. }
  50. if (! defined('TPL_DIR')) {
  51. //define('TPL_DIR', APP_DIR . '/templates');
  52. }
  53. if (! defined('ENV')) {
  54. $env = defined('ENVIRONMENT') ? ENVIRONMENT : 'prod';
  55. define('ENV', $env);
  56. }
  57. if (! defined('DB_DSN')) {
  58. //define('DB_DSN', 'mysql://root@localhost/my_app');
  59. }
  60. if (! defined('ERROR_TEMPLATE')) {
  61. //define('ERROR_TEMPLATE', "page_error.tpl.php");
  62. }
  63. if (defined('DB_DSN')) {
  64. //$this->db = static::getDb('default', DB_DSN);
  65. $this->db = $this->connectDb('default', DB_DSN);
  66. }
  67. // Load helpers
  68. Tools::loadHelpers(APP_DIR . '/src/helpers');
  69. Tools::loadHelpers(FW_DIR . '/src/helpers');
  70. }
  71. /* MAIN APP PROCESS */
  72. public function handle(Request $request)
  73. {
  74. try {
  75. $response = new Response(200, [], null);
  76. $pipe = new Pipe($this->middlewares);
  77. $response = $pipe->next($request, $response);
  78. } catch (\Exception $e) {
  79. $error_code = $e->getCode();
  80. $error_message = $e->getMessage();
  81. error_log("[App] Error " . $error_code . " : " . $error_message);
  82. $content = null;
  83. if (ENV == 'dev') {
  84. $title = "App CATCHED EXCEPTION CODE " . $error_code;
  85. $message = '<pre>' . print_r($e, true) . '</pre>';
  86. $content = '<title>' . $title . '</title><h1>' . $title . '</h1><h2>' . $error_message . '</h2><p>' . $message . '</p>';
  87. }
  88. //throw $e;
  89. $response->setStatus(500)->setHtml($content);
  90. }
  91. return $response;
  92. }
  93. /* CONTAINER */
  94. // GET an element from the container
  95. public function get($key, $default_value=null)
  96. {
  97. return isset($this->container[$key]) ? $this->container[$key] : $default_value;
  98. }
  99. // STORE an element to the container
  100. public function set($key, $value=null)
  101. {
  102. if (is_array($key)) {
  103. foreach ($key as $k => $v) {
  104. $this->set($k, $v);
  105. }
  106. } else {
  107. $this->container[$key] = $value;
  108. }
  109. return $this;
  110. }
  111. public function has($name)
  112. {
  113. return isset($this->container[$name]);
  114. }
  115. /* DATABASE */
  116. public function connectDb($instance_name=null, $dsn=null)
  117. {
  118. /*
  119. $dsn = 'mysql://user:pass@localhost/my_app';
  120. */
  121. static $instances = [];
  122. //static $last_instance_name = null;
  123. if (empty($instance_name)) {
  124. if (! empty($this->db)) {
  125. return $this->db;
  126. }
  127. $instance_name = 'default';
  128. //if (! empty($last_instance_name)) {
  129. // $instance_name = $last_instance_name;
  130. //}
  131. }
  132. //$last_instance_name = $instance_name;
  133. if (empty($instances[$instance_name])) {
  134. if (empty($dsn) && defined('DB_DSN')) {
  135. $dsn = DB_DSN;
  136. }
  137. $instances[$instance_name] = new SqlDb($dsn);
  138. }
  139. return $instances[$instance_name];
  140. }
  141. public function getDb()
  142. {
  143. return $this->db;
  144. }
  145. public function setDb($db)
  146. {
  147. $this->db = $db;
  148. return $this;
  149. }
  150. }