App.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. namespace KarmaFW;
  3. use \KarmaFW\Routing\Router;
  4. use \KarmaFW\Lib\Hooks\HooksManager;
  5. use \KarmaFW\Database\Sql\SqlDb;
  6. //use \KarmaFW\Database\Sql\SqlOrmModel;
  7. use \KarmaFW\App\Request;
  8. use \KarmaFW\App\Response;
  9. use \KarmaFW\App\ResponseError;
  10. use \KarmaFW\App\Pipe;
  11. use \KarmaFW\App\Container;
  12. define('FW_SRC_DIR', __DIR__);
  13. define('FW_DIR', __DIR__ . "/..");
  14. if (! defined('APP_DIR')) {
  15. echo "ERROR: Please, define APP_DIR" . PHP_EOL;
  16. exit(1);
  17. }
  18. class App
  19. {
  20. protected static $booted = false;
  21. protected static $helpers_dirs = [
  22. APP_DIR . "/src/helpers",
  23. FW_SRC_DIR . "/helpers",
  24. ];
  25. public static $db = null;
  26. public static $data = [];
  27. protected static $instance = null;
  28. protected $middlewares;
  29. protected $container;
  30. public function __construct($middlewares=[])
  31. {
  32. $this->middlewares = $middlewares;
  33. $this->container = new Container;
  34. self::$instance = $this;
  35. try {
  36. $this->configure();
  37. self::loadHelpersDirs();
  38. } catch (\Exception $e) {
  39. header("HTTP/1.0 500 Internal Server Error");
  40. echo "<h1>Server error</h1>";
  41. if (ENV === 'dev') {
  42. echo "<pre>";
  43. print_r($e);
  44. echo "</pre>";
  45. }
  46. exit;
  47. }
  48. }
  49. public function configure()
  50. {
  51. if (is_file(APP_DIR . '/config/config.php')) {
  52. require APP_DIR . '/config/config.php';
  53. }
  54. if (! defined('APP_NAME')) {
  55. define('APP_NAME', "PHP Application");
  56. }
  57. if (! defined('TPL_DIR')) {
  58. //define('TPL_DIR', APP_DIR . '/templates');
  59. }
  60. if (! defined('ENV')) {
  61. $env = defined('ENVIRONMENT') ? ENVIRONMENT : 'prod';
  62. define('ENV', $env);
  63. }
  64. if (! defined('DB_DSN')) {
  65. //define('DB_DSN', 'mysql://root@localhost/my_app');
  66. }
  67. if (! defined('ERROR_TEMPLATE')) {
  68. //define('ERROR_TEMPLATE', "page_error.tpl.php");
  69. }
  70. if (defined('DB_DSN')) {
  71. self::$db = static::getDb();
  72. }
  73. }
  74. public function handle($request)
  75. {
  76. try {
  77. $response = new Response;
  78. $pipe = new Pipe($this->middlewares);
  79. $response = $pipe->next($request, $response);
  80. } catch (\Exception $e) {
  81. $content = null;
  82. if (ENV == 'dev') {
  83. $title = "App CATCHED EXCEPTION";
  84. $message = '<pre>' . print_r($e, true) . '</pre>';
  85. $content = '<title>' . $title . '</title><h1>' . $title . '</h1><p>' . $message . '</p>';
  86. }
  87. //throw $e;
  88. return new ResponseError(500, $content);
  89. }
  90. return $response;
  91. }
  92. /* #### */
  93. public static function boot()
  94. {
  95. if (defined('USE_HOOKS') && USE_HOOKS) {
  96. HooksManager::applyHook('app.boot.before', []);
  97. }
  98. // TODO: config à migrer dans un fichier .env et .env.prod et .env.dev et .env.local (à charger dans cet ordre, avec overwrite)
  99. if (is_file(APP_DIR . '/config/config.php')) {
  100. require APP_DIR . '/config/config.php';
  101. }
  102. if (! defined('APP_NAME')) {
  103. define('APP_NAME', "PHP Application");
  104. }
  105. if (! defined('TPL_DIR')) {
  106. //define('TPL_DIR', APP_DIR . '/templates');
  107. }
  108. if (! defined('ENV')) {
  109. define('ENV', 'prod');
  110. }
  111. if (! defined('DB_DSN')) {
  112. //define('DB_DSN', 'mysql://root@localhost/my_app');
  113. }
  114. if (! defined('ERROR_TEMPLATE')) {
  115. //define('ERROR_TEMPLATE', "page_error.tpl.php");
  116. }
  117. // move fw_helpers at the end of the list (to be loaded the last one)
  118. /*
  119. if (count(self::$helpers_dirs) > 1) {
  120. $fw_helpers = array_shift(self::$helpers_dirs);
  121. self::$helpers_dirs[] = $fw_helpers;
  122. }
  123. */
  124. // include helpers
  125. foreach (self::$helpers_dirs as $helpers_dir) {
  126. self::loadHelpers($helpers_dir);
  127. }
  128. // define class aliases
  129. class_alias('\\KarmaFW\\App', 'App');
  130. class_alias('\\KarmaFW\\Database\\Sql\\SqlDb', 'SqlDb');
  131. class_alias('\\KarmaFW\\Database\\Sql\\SqlSchema', 'SqlSchema');
  132. class_alias('\\KarmaFW\\Database\\Sql\\SqlTable', 'SqlTable');
  133. class_alias('\\KarmaFW\\Database\\Sql\\SqlOrmModel', 'SqlOrmModel');
  134. class_alias('\\KarmaFW\\Database\\Sql\\SqlQuery', 'SqlQuery');
  135. class_alias('\\KarmaFW\\Database\\Sql\\SqlWhere', 'SqlWhere');
  136. class_alias('\\KarmaFW\\Database\\Sql\\SqlExpr', 'SqlExpr');
  137. class_alias('\\KarmaFW\\Database\\Sql\\SqlLike', 'SqlLike');
  138. class_alias('\\KarmaFW\\Database\\Sql\\SqlIn', 'SqlIn');
  139. class_alias('\\KarmaFW\\Database\\Sql\\SqlTools', 'SqlTools');
  140. if (defined('DB_DSN')) {
  141. self::$db = static::getDb();
  142. }
  143. // ERRORS HANDLER // NOTE => a déplacer dans \KarmaFW\WebApp::boot() ??
  144. if (defined('ENV') && ENV == 'dev') {
  145. $whoops = new \Whoops\Run;
  146. $whoops->prependHandler(new \Whoops\Handler\PrettyPageHandler);
  147. $whoops->register();
  148. }
  149. if (defined('USE_HOOKS') && USE_HOOKS) {
  150. HooksManager::applyHook('app.boot.after', []);
  151. }
  152. self::$booted = true;
  153. }
  154. public static function registerHelpersDir($dir)
  155. {
  156. $dir = rtrim($dir, '/');
  157. if (! in_array($dir, self::$helpers_dirs)) {
  158. self::$helpers_dirs[] = $dir;
  159. }
  160. }
  161. public function get($key, $default_value=null)
  162. {
  163. return isset($this->container[$key]) ? $this->container[$key] : $default_value;
  164. }
  165. public function set($key, $value)
  166. {
  167. $this->container[$key] = $value;
  168. }
  169. public function has($name)
  170. {
  171. return isset($this->container[$name]);
  172. }
  173. // DEPRECATED
  174. public static function setData($key, $value=null)
  175. {
  176. self::$data[$key] = $value;
  177. }
  178. // DEPRECATED
  179. public static function getData($key, $default_value=null)
  180. {
  181. return array_key_exists($key, self::$data) ? self::$data[$key] : $default_value;
  182. }
  183. // DEPRECATED
  184. public static function hasData($key)
  185. {
  186. return array_key_exists($key, self::$data);
  187. }
  188. public static function unregisterHelpersDir($dir)
  189. {
  190. $dir = rtrim($dir, '/');
  191. $k = array_search($dir, self::$helpers_dirs);
  192. if ($k !== false) {
  193. unset(self::$helpers_dirs[$k]);
  194. }
  195. }
  196. protected static function loadHelpersDirs()
  197. {
  198. if (is_array(self::$helpers_dirs)) {
  199. foreach (self::$helpers_dirs as $helpers_dir) {
  200. self::loadHelpers($helpers_dir);
  201. }
  202. }
  203. }
  204. protected static function loadHelpers($dir)
  205. {
  206. $helpers = glob($dir . '/helpers_*.php');
  207. foreach ($helpers as $helper) {
  208. //echo '<li>'.$helper.'</li>';
  209. require $helper;
  210. }
  211. }
  212. public static function getApp()
  213. {
  214. if (isset(self::$instance)) {
  215. return self::$instance;
  216. }
  217. throw new Exception("App is not instancied", 1);
  218. }
  219. public static function getDb($instance_name=null, $dsn=null)
  220. {
  221. /*
  222. $dsn = 'mysql://user:pass@localhost/my_app';
  223. */
  224. static $instances = [];
  225. static $last_instance_name = null;
  226. if (empty($instance_name)) {
  227. if (! empty(self::$db)) {
  228. return self::$db;
  229. }
  230. $instance_name = 'default';
  231. //if (! empty($last_instance_name)) {
  232. // $instance_name = $last_instance_name;
  233. //}
  234. }
  235. $last_instance_name = $instance_name;
  236. if (empty($instances[$instance_name])) {
  237. if (empty($dsn) && defined('DB_DSN')) {
  238. $dsn = DB_DSN;
  239. }
  240. $instances[$instance_name] = new SqlDb($dsn);
  241. }
  242. return $instances[$instance_name];
  243. }
  244. /*
  245. public static function createOrmItem($table_name, $primary_key_values=[], $db=null)
  246. {
  247. return new SqlOrmModel($table_name, $primary_key_values, $db);
  248. }
  249. */
  250. /*
  251. public static function routeCommand($argv)
  252. {
  253. if (! self::$booted) {
  254. self::boot();
  255. }
  256. $arguments = array_slice($argv, 0);
  257. $script_name = array_shift($arguments);
  258. $command_name = array_shift($arguments);
  259. $class_name = implode('', array_map('ucfirst', explode("_", $command_name)));
  260. if (! empty($class_name)) {
  261. $class_user = '\\App\\Commands\\' . $class_name;
  262. $class_fw = '\\KarmaFW\\Commands\\' . $class_name;
  263. if (class_exists($class_user)) {
  264. $command = new $class_user;
  265. $command->run($arguments);
  266. exit(0);
  267. } else if (class_exists($class_fw)) {
  268. $command = new $class_fw;
  269. $command->run($arguments);
  270. exit(0);
  271. } else {
  272. echo "PHP Console script" . PHP_EOL . PHP_EOL;
  273. echo "Usage: php console.php <command> [arguments]" . PHP_EOL . PHP_EOL;
  274. echo "Warning: invalid command" . PHP_EOL;
  275. }
  276. } else {
  277. echo "PHP Console script" . PHP_EOL . PHP_EOL;
  278. echo "Usage: php console.php <command> [arguments]" . PHP_EOL . PHP_EOL;
  279. echo "Warning: missing command" . PHP_EOL;
  280. }
  281. exit(1);
  282. }
  283. */
  284. }