Skip to content

Commit

Permalink
Modify the test code to comply with the testing specifications
Browse files Browse the repository at this point in the history
  • Loading branch information
eoioer committed Jan 19, 2025
1 parent 2441f1f commit d5f024c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 94 deletions.
6 changes: 1 addition & 5 deletions frameworks/PHP/cyberphp/app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
'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',
Expand Down
11 changes: 6 additions & 5 deletions frameworks/PHP/cyberphp/app/controller/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ public function plaintext()

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.';
Expand All @@ -37,7 +38,7 @@ public function fortunes()

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--) {
Expand All @@ -51,7 +52,7 @@ public function updates($q=1)
{
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 = [];
Expand Down
2 changes: 2 additions & 0 deletions frameworks/PHP/cyberphp/app/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
['/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'],
];
7 changes: 2 additions & 5 deletions frameworks/PHP/cyberphp/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
101 changes: 24 additions & 77 deletions frameworks/PHP/cyberphp/src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use Cyber\Middleware;
use Cyber\Utility;
use PDO;
use Illuminate\Database\Capsule\Manager as EloquentDb;
use think\facade\Db as ThinkormDb;

class App
{
Expand All @@ -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;
/**
Expand All @@ -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
Expand All @@ -82,66 +77,18 @@ public function run()
{
$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
Expand Down
4 changes: 2 additions & 2 deletions frameworks/PHP/cyberphp/src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public function handleRoute()
// 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 {
Expand Down

0 comments on commit d5f024c

Please sign in to comment.