Skip to content

Commit d5f024c

Browse files
committed
Modify the test code to comply with the testing specifications
1 parent 2441f1f commit d5f024c

File tree

6 files changed

+37
-94
lines changed

6 files changed

+37
-94
lines changed

frameworks/PHP/cyberphp/app/config.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
'dsn' => 'pgsql:host=tfb-database;dbname=hello_world',
2020
'username' => 'benchmarkdbuser',
2121
'password' => 'benchmarkdbpass',
22-
'options' => [
23-
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
24-
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
25-
PDO::ATTR_EMULATE_PREPARES => false,
26-
]
22+
'options' => [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,PDO::ATTR_EMULATE_PREPARES => false]
2723
],
2824
'eloquent' => [
2925
'driver' => 'mysql',

frameworks/PHP/cyberphp/app/controller/Index.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ public function plaintext()
1616

1717
public function db()
1818
{
19-
$prepare = app()->db->prepare('SELECT id,randomNumber FROM World WHERE id=?');
19+
$prepare = app()->dbWorld;
2020
$prepare->execute([mt_rand(1, 10000)]);
21-
return Response::json($prepare->fetch());
21+
$data = $prepare->fetch();
22+
return Response::json($data);
2223
}
2324
public function fortunes()
2425
{
25-
$fortune = app()->db->prepare('SELECT id,message FROM Fortune');
26+
$fortune = app()->dbFortune;
2627
$fortune->execute();
2728
$arr = $fortune->fetchAll(\PDO::FETCH_KEY_PAIR);
2829
$arr[0] = 'Additional fortune added at request time.';
@@ -37,7 +38,7 @@ public function fortunes()
3738

3839
public function queries($q=1)
3940
{
40-
$statement = app()->db->prepare('SELECT id,randomNumber FROM World WHERE id=?');
41+
$statement = app()->dbWorld;
4142
$query_count = max(min(intval($q), 500), 1);
4243
$arr = [];
4344
while ($query_count--) {
@@ -51,7 +52,7 @@ public function updates($q=1)
5152
{
5253
static $updates = [];
5354

54-
$random = app()->db->prepare('SELECT id,randomNumber FROM World WHERE id=?');
55+
$random = app()->dbWorld;
5556
$count = max(min(intval($q), 500), 1);
5657

5758
$worlds = $keys = $values = [];

frameworks/PHP/cyberphp/app/route.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
['/plaintext', 'GET', 'app\controller\Index@plaintext'],
88
['/db', 'GET', 'app\controller\Index@db'],
99
['/fortunes', 'GET', 'app\controller\Index@fortunes'],
10+
['/queries/', 'GET', 'app\controller\Index@queries'],
1011
['/queries/{q}', 'GET', 'app\controller\Index@queries'],
12+
['/updates/', 'GET', 'app\controller\Index@updates'],
1113
['/updates/{q}', 'GET', 'app\controller\Index@updates'],
1214
];

frameworks/PHP/cyberphp/composer.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313
"php": ">=8.3",
1414
"php-di/php-di": "^7.0",
1515
"nikic/fast-route": "^1.3",
16-
"workerman/workerman": "^4.1",
17-
"illuminate/database": "^11.37",
18-
"topthink/think-orm": "^3.0"
16+
"workerman/workerman": "^4.1"
1917
},
2018
"autoload": {
2119
"psr-4": {
2220
"app\\": "app/",
23-
"Cyber\\": "src/",
24-
"": "extend/"
21+
"Cyber\\": "src/"
2522
},
2623
"files": [
2724
"app/helpers.php"

frameworks/PHP/cyberphp/src/App.php

Lines changed: 24 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use Cyber\Middleware;
1111
use Cyber\Utility;
1212
use PDO;
13-
use Illuminate\Database\Capsule\Manager as EloquentDb;
14-
use think\facade\Db as ThinkormDb;
1513

1614
class 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

frameworks/PHP/cyberphp/src/Route.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public function handleRoute()
4747
// If handler is a string (controller@method)
4848
if (is_string($handler)) {
4949
list($controller, $method) = explode('@', $handler);
50-
$class = new $controller();
51-
return $class->$method(...$parameters);
50+
$ctrl = $container->get($controller);
51+
return $ctrl->$method(...$parameters);
5252
} elseif (is_callable($handler)) {
5353
return $handler(...$parameters);
5454
} else {

0 commit comments

Comments
 (0)