1010use Cyber \Middleware ;
1111use Cyber \Utility ;
1212use PDO ;
13- use Illuminate \Database \Capsule \Manager as EloquentDb ;
14- use think \facade \Db as ThinkormDb ;
1513
1614class App
1715{
@@ -28,16 +26,18 @@ class App
2826 public Response $ response ;
2927 /** Middleware */
3028 public Middleware $ middleware ;
31-
29+
3230 /** Route configuration */
3331 public array $ routes ;
3432 /** Route manager */
3533 public Route $ route ;
36-
34+
3735 /** Application name */
3836 public string $ appName ;
3937 public $ db ;
40-
38+ public $ dbWorld ;
39+ public $ dbFortune ;
40+
4141 public $ start_time ;
4242 public $ timestamps ;
4343 /**
@@ -47,33 +47,28 @@ class App
4747 public function __construct ($ containerConfig = null )
4848 {
4949 $ this ->start_time = time ();
50- /* Check PHP environment version | extension */
51- Utility::checkPHPenv ();
52-
50+
5351 /* Build container instance */
5452 $ this ->container = new Container ($ containerConfig );
55-
53+
5654 /* Load route configuration */
57- $ routes = require_once $ this ->container ->get ('route_path ' );
55+ $ routes = require $ this ->container ->get ('route_path ' );
5856 /* Create route manager */
5957 $ this ->route = $ this ->container ->get ('Route ' );
6058 /* Call route dispatcher */
6159 $ this ->route ->dispatcher ($ routes );
62-
60+
6361 /* Configuration */
6462 $ this ->config = $ this ->container ->get ('config ' );
6563 /* Request object */
6664 $ 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+
7466 /* 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+
7772 }
7873 /**
7974 * Run application
@@ -82,66 +77,18 @@ public function run()
8277 {
8378 $ this ->timestamps = time ();
8479 /* 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 ) {
11881 $ 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 ' );
14386 }
87+
88+ /* Return response */
89+ return $ this ->route ->handleRoute ();
14490 }
91+
14592 /**
14693 * Get the current application configuration
14794 * $app->getConfig(); // Returns the entire configuration content of the current application
0 commit comments