Browse Source

Modify the test code to comply with the testing specifications

eoioer 7 months ago
parent
commit
d5f024c585

+ 1 - 5
frameworks/PHP/cyberphp/app/config.php

@@ -19,11 +19,7 @@ return [
         'dsn' => 'pgsql:host=tfb-database;dbname=hello_world',
         'username' => 'benchmarkdbuser',
         'password' => 'benchmarkdbpass',
-        'options' => [
-            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
-            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
-            PDO::ATTR_EMULATE_PREPARES   => false,
-        ]
+        'options' => [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,PDO::ATTR_EMULATE_PREPARES    => false]
     ],
     'eloquent' => [
         'driver' => 'mysql',

+ 6 - 5
frameworks/PHP/cyberphp/app/controller/Index.php

@@ -16,13 +16,14 @@ class Index {
 
     public function db()
     {
-        $prepare = app()->db->prepare('SELECT id,randomNumber FROM World WHERE id=?');
+        $prepare = app()->dbWorld;
         $prepare->execute([mt_rand(1, 10000)]);
-        return Response::json($prepare->fetch());
+        $data = $prepare->fetch();
+        return Response::json($data);
     }
     public function fortunes()
     {
-        $fortune = app()->db->prepare('SELECT id,message FROM Fortune');
+        $fortune = app()->dbFortune;
         $fortune->execute();
         $arr    = $fortune->fetchAll(\PDO::FETCH_KEY_PAIR);
         $arr[0] = 'Additional fortune added at request time.';
@@ -37,7 +38,7 @@ class Index {
 
     public function queries($q=1)
     {
-        $statement = app()->db->prepare('SELECT id,randomNumber FROM World WHERE id=?');
+        $statement = app()->dbWorld;
         $query_count = max(min(intval($q), 500), 1);
         $arr = [];
         while ($query_count--) {
@@ -51,7 +52,7 @@ class Index {
     {
         static $updates = [];
 
-        $random = app()->db->prepare('SELECT id,randomNumber FROM World WHERE id=?');
+        $random = app()->dbWorld;
         $count = max(min(intval($q), 500), 1);
 
         $worlds = $keys = $values = [];

+ 2 - 0
frameworks/PHP/cyberphp/app/route.php

@@ -7,6 +7,8 @@ return [
     ['/plaintext', 'GET', 'app\controller\Index@plaintext'],
     ['/db', 'GET', 'app\controller\Index@db'],
     ['/fortunes', 'GET', 'app\controller\Index@fortunes'],
+    ['/queries/', 'GET', 'app\controller\Index@queries'],
     ['/queries/{q}', 'GET', 'app\controller\Index@queries'],
+    ['/updates/', 'GET', 'app\controller\Index@updates'],
     ['/updates/{q}', 'GET', 'app\controller\Index@updates'],
 ];

+ 2 - 5
frameworks/PHP/cyberphp/composer.json

@@ -13,15 +13,12 @@
         "php": ">=8.3",
         "php-di/php-di": "^7.0",
         "nikic/fast-route": "^1.3",
-        "workerman/workerman": "^4.1",
-        "illuminate/database": "^11.37",
-        "topthink/think-orm": "^3.0"
+        "workerman/workerman": "^4.1"
     },
     "autoload": {
         "psr-4": {
             "app\\": "app/",
-            "Cyber\\": "src/",
-            "": "extend/"
+            "Cyber\\": "src/"
         },
 		"files": [
 			"app/helpers.php"

+ 24 - 77
frameworks/PHP/cyberphp/src/App.php

@@ -10,8 +10,6 @@ use Cyber\Response;
 use Cyber\Middleware;
 use Cyber\Utility;
 use PDO;
-use Illuminate\Database\Capsule\Manager as EloquentDb;
-use think\facade\Db as ThinkormDb;
 
 class App
 {
@@ -28,16 +26,18 @@ class App
     public Response $response;
     /** Middleware */
     public Middleware $middleware;
-    
+
     /** Route configuration */
     public array $routes;
     /** Route manager */
     public Route $route;
-    
+
     /** Application name */
     public string $appName;
     public $db;
-    
+    public $dbWorld;
+    public $dbFortune;
+
     public $start_time;
     public $timestamps;
     /**
@@ -47,33 +47,28 @@ class App
     public function __construct($containerConfig = null)
     {
         $this->start_time = time();
-        /* Check PHP environment version | extension */
-        Utility::checkPHPenv();
-        
+
         /* Build container instance */
         $this->container = new Container($containerConfig);
-        
+
         /* Load route configuration */
-        $routes = require_once $this->container->get('route_path');
+        $routes = require $this->container->get('route_path');
         /* Create route manager */
         $this->route = $this->container->get('Route');
         /* Call route dispatcher */
         $this->route->dispatcher($routes);
-        
+
         /* Configuration */
         $this->config = $this->container->get('config');
         /* Request object */
         $this->request = $this->container->get('Request');
-        
-        /* Response object */
-        $this->response = $this->container->get('Response');
-        
-        /* Middleware */
-        $this->middleware = $this->container->get('Middleware');
-        
+
         /* Database */
-        $this->db = $this->setDb();
-        
+        $pdo =  new PDO(...$this->getConfig('pdo'));
+        $this->db = $pdo;
+        $this->dbWorld = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
+        $this->dbFortune = $pdo->prepare('SELECT id,message FROM Fortune');
+
     }
     /**
     * Run application
@@ -82,66 +77,18 @@ class App
     {
         $this->timestamps = time();
         /* cli mode maintains database connection */
-        $this->cliMaintainDatabaseConnection($this->getConfig('orm'));
-        
-        /* Get application name */
-        $this->appName = $this->request->getAppName();
-        
-        /* Request object middleware list */
-        $requestMiddlewares = $this->getConfig('request_middleware');
-        
-        /* Execute request object middleware */
-        if(!empty($requestMiddlewares)){
-            $this->request = $this->middleware->handleRequest($requestMiddlewares);
-        }
-        
-        /* Parse route and return the closure to be executed */
-        $handleRoute = $this->route->handleRoute();
-        /* Middleware list */
-        $Middlewares = $this->getConfig('middleware');
-        /* Execute middleware */
-        if(!empty($Middlewares)){
-            $response = $this->middleware->handle($Middlewares,function() use ($handleRoute) {
-                return $handleRoute;
-            });
-        }else{
-            $response =  $handleRoute;
-        }
-        /* Return response */
-        return $response;
-    }
-    
-    // cli mode maintains database connection every 600 seconds
-    public function cliMaintainDatabaseConnection($ormName)
-    {
-        if (php_sapi_name() === 'cli' and time() - $this->start_time > 600) {
+        if (php_sapi_name() === 'cli' and time() - $this->start_time > 1) {
             $this->start_time = time();
-            if($ormName=='pdo'){
-                // Close the existing connection and recreate the PDO instance
-                $this->db = null;
-                $this->db = new PDO(...$this->getConfig('pdo'));
-            }elseif($ormName=='thinkorm'){
-                // Close the existing connection and reconnect to Thinkorm
-                $this->db::close();
-                $this->db::connect('mysql',true);
-            }
-        }
-    }
-    public function setDb()
-    {
-        if($this->getConfig('orm')=='pdo'){
-            return new PDO(...$this->getConfig('pdo'));
-        }elseif($this->getConfig('orm')=='eloquent'){
-            $EloquentDb = new EloquentDb;
-            $EloquentDb->addConnection($this->getConfig('eloquent'));
-            $EloquentDb->setAsGlobal();
-            $EloquentDb->bootEloquent();
-            return $EloquentDb;
-        }elseif($this->getConfig('orm')=='thinkorm'){
-            ThinkormDb::setConfig($this->getConfig('thinkorm'));
-            return ThinkormDb::class;
+            $pdo =  new PDO(...$this->getConfig('pdo'));
+            $this->db = $pdo;
+            $this->dbWorld = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
+            $this->dbFortune = $pdo->prepare('SELECT id,message FROM Fortune');
         }
+
+        /* Return response */
+        return $this->route->handleRoute();
     }
+
     /**
     * Get the current application configuration
     * $app->getConfig();                         // Returns the entire configuration content of the current application

+ 2 - 2
frameworks/PHP/cyberphp/src/Route.php

@@ -47,8 +47,8 @@ class Route
                 // If handler is a string (controller@method)
                 if (is_string($handler)) {
                     list($controller, $method) = explode('@', $handler);
-                    $class = new $controller();
-                    return $class->$method(...$parameters);
+                    $ctrl = $container->get($controller);
+                    return $ctrl->$method(...$parameters);
                 } elseif (is_callable($handler)) {
                     return $handler(...$parameters);
                 } else {