10
10
use Cyber \Middleware ;
11
11
use Cyber \Utility ;
12
12
use PDO ;
13
- use Illuminate \Database \Capsule \Manager as EloquentDb ;
14
- use think \facade \Db as ThinkormDb ;
15
13
16
14
class App
17
15
{
@@ -28,16 +26,18 @@ class App
28
26
public Response $ response ;
29
27
/** Middleware */
30
28
public Middleware $ middleware ;
31
-
29
+
32
30
/** Route configuration */
33
31
public array $ routes ;
34
32
/** Route manager */
35
33
public Route $ route ;
36
-
34
+
37
35
/** Application name */
38
36
public string $ appName ;
39
37
public $ db ;
40
-
38
+ public $ dbWorld ;
39
+ public $ dbFortune ;
40
+
41
41
public $ start_time ;
42
42
public $ timestamps ;
43
43
/**
@@ -47,33 +47,28 @@ class App
47
47
public function __construct ($ containerConfig = null )
48
48
{
49
49
$ this ->start_time = time ();
50
- /* Check PHP environment version | extension */
51
- Utility::checkPHPenv ();
52
-
50
+
53
51
/* Build container instance */
54
52
$ this ->container = new Container ($ containerConfig );
55
-
53
+
56
54
/* Load route configuration */
57
- $ routes = require_once $ this ->container ->get ('route_path ' );
55
+ $ routes = require $ this ->container ->get ('route_path ' );
58
56
/* Create route manager */
59
57
$ this ->route = $ this ->container ->get ('Route ' );
60
58
/* Call route dispatcher */
61
59
$ this ->route ->dispatcher ($ routes );
62
-
60
+
63
61
/* Configuration */
64
62
$ this ->config = $ this ->container ->get ('config ' );
65
63
/* Request object */
66
64
$ this ->request = $ this ->container ->get ('Request ' );
67
-
68
- /* Response object */
69
- $ this ->response = $ this ->container ->get ('Response ' );
70
-
71
- /* Middleware */
72
- $ this ->middleware = $ this ->container ->get ('Middleware ' );
73
-
65
+
74
66
/* Database */
75
- $ this ->db = $ this ->setDb ();
76
-
67
+ $ pdo = new PDO (...$ this ->getConfig ('pdo ' ));
68
+ $ this ->db = $ pdo ;
69
+ $ this ->dbWorld = $ pdo ->prepare ('SELECT id,randomNumber FROM World WHERE id=? ' );
70
+ $ this ->dbFortune = $ pdo ->prepare ('SELECT id,message FROM Fortune ' );
71
+
77
72
}
78
73
/**
79
74
* Run application
@@ -82,66 +77,18 @@ public function run()
82
77
{
83
78
$ this ->timestamps = time ();
84
79
/* cli mode maintains database connection */
85
- $ this ->cliMaintainDatabaseConnection ($ this ->getConfig ('orm ' ));
86
-
87
- /* Get application name */
88
- $ this ->appName = $ this ->request ->getAppName ();
89
-
90
- /* Request object middleware list */
91
- $ requestMiddlewares = $ this ->getConfig ('request_middleware ' );
92
-
93
- /* Execute request object middleware */
94
- if (!empty ($ requestMiddlewares )){
95
- $ this ->request = $ this ->middleware ->handleRequest ($ requestMiddlewares );
96
- }
97
-
98
- /* Parse route and return the closure to be executed */
99
- $ handleRoute = $ this ->route ->handleRoute ();
100
- /* Middleware list */
101
- $ Middlewares = $ this ->getConfig ('middleware ' );
102
- /* Execute middleware */
103
- if (!empty ($ Middlewares )){
104
- $ response = $ this ->middleware ->handle ($ Middlewares ,function () use ($ handleRoute ) {
105
- return $ handleRoute ;
106
- });
107
- }else {
108
- $ response = $ handleRoute ;
109
- }
110
- /* Return response */
111
- return $ response ;
112
- }
113
-
114
- // cli mode maintains database connection every 600 seconds
115
- public function cliMaintainDatabaseConnection ($ ormName )
116
- {
117
- if (php_sapi_name () === 'cli ' and time () - $ this ->start_time > 600 ) {
80
+ if (php_sapi_name () === 'cli ' and time () - $ this ->start_time > 1 ) {
118
81
$ this ->start_time = time ();
119
- if ($ ormName =='pdo ' ){
120
- // Close the existing connection and recreate the PDO instance
121
- $ this ->db = null ;
122
- $ this ->db = new PDO (...$ this ->getConfig ('pdo ' ));
123
- }elseif ($ ormName =='thinkorm ' ){
124
- // Close the existing connection and reconnect to Thinkorm
125
- $ this ->db ::close ();
126
- $ this ->db ::connect ('mysql ' ,true );
127
- }
128
- }
129
- }
130
- public function setDb ()
131
- {
132
- if ($ this ->getConfig ('orm ' )=='pdo ' ){
133
- return new PDO (...$ this ->getConfig ('pdo ' ));
134
- }elseif ($ this ->getConfig ('orm ' )=='eloquent ' ){
135
- $ EloquentDb = new EloquentDb ;
136
- $ EloquentDb ->addConnection ($ this ->getConfig ('eloquent ' ));
137
- $ EloquentDb ->setAsGlobal ();
138
- $ EloquentDb ->bootEloquent ();
139
- return $ EloquentDb ;
140
- }elseif ($ this ->getConfig ('orm ' )=='thinkorm ' ){
141
- ThinkormDb::setConfig ($ this ->getConfig ('thinkorm ' ));
142
- return ThinkormDb::class;
82
+ $ pdo = new PDO (...$ this ->getConfig ('pdo ' ));
83
+ $ this ->db = $ pdo ;
84
+ $ this ->dbWorld = $ pdo ->prepare ('SELECT id,randomNumber FROM World WHERE id=? ' );
85
+ $ this ->dbFortune = $ pdo ->prepare ('SELECT id,message FROM Fortune ' );
143
86
}
87
+
88
+ /* Return response */
89
+ return $ this ->route ->handleRoute ();
144
90
}
91
+
145
92
/**
146
93
* Get the current application configuration
147
94
* $app->getConfig(); // Returns the entire configuration content of the current application
0 commit comments