helpers.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Get the application instance
  5. * If there is a configuration file, create the application instance
  6. * If there is no configuration file, return the already created instance
  7. * If there is no configuration file and no instance has been created, throw an exception
  8. * @param string $bootstrap Configuration file
  9. * @return \Cyber\App
  10. */
  11. if (!function_exists('app')) {
  12. function app(): \Cyber\App
  13. {
  14. return \Cyber\App::getInstance();
  15. }
  16. }
  17. /**
  18. * Get configuration information
  19. * config(); // Returns all configurations
  20. * config('app.name'); // Gets the value of app.name
  21. * config('db.mysql.host'); // Gets the value of db.mysql.host
  22. * @param string|null $key Configuration key name, supports multi-level configuration separated by dots, e.g., 'app.debug'
  23. * @param mixed $default Default value, returned when the configuration item does not exist
  24. * @return mixed
  25. */
  26. if (!function_exists('config')) {
  27. function config(?string $key = null, $default = null)
  28. {
  29. return \Cyber\App::getInstance()->getConfig($key) ?? $default;
  30. }
  31. }
  32. function renderExceptionPage($e, $debug = true, $templateFile = ''): string
  33. {
  34. // Determine template path
  35. $templateFile = !empty($templateFile) ? $templateFile : __DIR__ . '/views/errors/exception.html';
  36. // Prepare template variables
  37. $data = [
  38. 'code' => $e->getCode(),
  39. 'message' => $debug ? $e->getMessage() : 'The current server is experiencing an error, please contact the administrator or try again later.',
  40. 'error' => $e->getMessage(),
  41. ];
  42. // Add more information in debug mode
  43. if ($debug) {
  44. $data['trace'] = [];
  45. $data['file'] = $e->getFile();
  46. $data['line'] = $e->getLine();
  47. $traceFiles = $e->getTrace();
  48. array_unshift($traceFiles, ['file' => $data['file'], 'line' => $data['line']]);
  49. foreach ($traceFiles as $v) {
  50. try {
  51. if (isset($v['file']) && isset($v['line'])) {
  52. $startline = max(1, $v['line'] - 10);
  53. $contents = file($v['file']);
  54. $data['trace'][] = [
  55. 'file' => $v['file'],
  56. 'line' => $v['line'],
  57. 'source0' => $contents ? array_slice($contents, 0, 1) : '',
  58. 'source' => [
  59. 'startline' => $startline,
  60. 'content' => array_slice($contents, $startline - 1, 16)
  61. ]
  62. ];
  63. }
  64. } catch (\Throwable $e) {
  65. continue;
  66. }
  67. }
  68. }
  69. // Render error page
  70. if (!file_exists($templateFile)) {
  71. $msg = '<div style="margin:100px auto 20px;text-align:center"><h1>Error ' . $data['code'] . '</h1></div>';
  72. $msg .= '<div style="margin:0px auto;text-align:center">Sorry, the server encountered an error</div>';
  73. $msg .= '<div style="margin:50px auto;text-align:center"><h2><pre>' . htmlspecialchars($data['message']) . '</pre></h2></div>';
  74. $msg .= '<div style="margin:100px auto;text-align:center"><a href="/"><button>Return to Home</button></a></div>';
  75. return $msg;
  76. }
  77. extract($data);
  78. ob_start();
  79. include $templateFile;
  80. return ob_get_clean();
  81. }