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