Added eloquent and phinx to project to handle database interactions and migrations; added Result model for storing benchmark results

This commit is contained in:
Gregory Ballantine 2022-11-25 16:48:54 -05:00
parent 323152a8f5
commit 9a65b5f27a
11 changed files with 2064 additions and 13 deletions

View File

@ -19,6 +19,9 @@
"slim/slim": "^4.11",
"slim/psr7": "^1.6",
"php-di/php-di": "^6.4",
"slim/twig-view": "^3.3"
"slim/twig-view": "^3.3",
"illuminate/database": "^9.41",
"robmorgan/phinx": "^0.13.1",
"hassankhan/config": "^3.0"
}
}

1910
composer.lock generated

File diff suppressed because it is too large Load Diff

6
conf/defaults.json Normal file
View File

@ -0,0 +1,6 @@
{
"database": {
"driver": "sqlite",
"database": "./data/colossus.db"
}
}

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class AddResultsTable extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('results');
$table->addColumn('component', 'string', ['null' => false])
->addColumn('benchmark', 'text', ['null' => false])
->addColumn('type', 'string', ['null' => false, 'default' => 'fps'])
->addColumn('average', 'integer', ['null' => false])
->addColumn('minimum', 'integer')
->addColumn('maximum', 'integer')
->addTimestamps()
->addIndex(['component', 'benchmark', 'type'])
->create();
}
}

37
phinx.php Normal file
View File

@ -0,0 +1,37 @@
<?php
return
[
'paths' => [
'migrations' => '%%PHINX_CONFIG_DIR%%/db/migrations',
'seeds' => '%%PHINX_CONFIG_DIR%%/db/seeds'
],
'environments' => [
'default_migration_table' => 'phinxlog',
'default_environment' => 'development',
'production' => [
'adapter' => 'mysql',
'host' => 'localhost',
'name' => 'production_db',
'user' => 'root',
'pass' => '',
'port' => '3306',
'charset' => 'utf8',
],
'development' => [
'adapter' => 'sqlite',
'name' => './data/colossus',
'suffix' => '.db',
],
'testing' => [
'adapter' => 'mysql',
'host' => 'localhost',
'name' => 'testing_db',
'user' => 'root',
'pass' => '',
'port' => '3306',
'charset' => 'utf8',
]
],
'version_order' => 'creation'
];

View File

@ -6,12 +6,17 @@ use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Routing\RouteContext;
use Slim\Views\Twig;
use BitGoblin\Colossus\Models\Result;
class ResultController extends Controller {
public function getList(Request $request, Response $response): Response {
$results = Result::all();
$view = Twig::fromRequest($request);
return $view->render($response, 'result/list.twig');
return $view->render($response, 'result/list.twig', [
'results' => $results,
]);
}
public function getAdd(Request $request, Response $response): Response {
@ -20,7 +25,17 @@ class ResultController extends Controller {
}
public function postAdd(Request $request, Response $response): Response {
// will add data to database later...
$params = (array)$request->getParsedBody();
$result = new Result;
$result->component = $params['result_component'];
$result->benchmark = $params['result_benchmark'];
$result->type = $params['result_type'];
$result->average = $params['result_avg'];
$result->minimum = $params['result_min'];
$result->maximum = $params['result_max'];
$result->save();
// redirect the user back to the home page
$routeContext = RouteContext::fromRequest($request);

18
src/Models/Result.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace BitGoblin\Colossus\Models;
use Illuminate\Database\Eloquent\Model;
class Result extends Model {
protected $fillable = [
'component',
'benchmark',
'type',
'average',
'minimum',
'maximum',
];
}

View File

@ -1,6 +1,8 @@
<?php
use DI\Container;
use Noodlehaus\Config;
use Noodlehaus\Parser\Json;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
@ -9,8 +11,17 @@ use Slim\Views\TwigMiddleware;
require __DIR__ . '/../vendor/autoload.php';
// Create Container using PHP-DI
// Load app configuration
$config = Config::load(__DIR__ . '/../conf/defaults.json');
// Create new container object and add our config object to it
$container = new Container();
$container->set('config', function () use ($config) {
return $config;
});
// Load database configuration
require_once __DIR__ . '/database.php';
// Set container to create App with on AppFactory
AppFactory::setContainer($container);

10
src/database.php Normal file
View File

@ -0,0 +1,10 @@
<?php
$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($config->get('database'));
$capsule->setAsGlobal();
$capsule->bootEloquent();
$container->set('db', function () use ($capsule) {
return $capsule;
});

View File

@ -46,7 +46,7 @@
</div>
</div>
<input class="button button-primary" type="submit" value="Submit">
<input class="button button-primary u-full-width" type="submit" value="Submit">
</form>
</div>
</div>

View File

@ -4,4 +4,29 @@
{% block content %}
<p>Results list...</p>
<table class="u-full-width">
<thead>
<tr>
<th>Hardware name</th>
<th>Benchmark</th>
<th>Result type</th>
<th>Avg.</th>
<th>Min.</th>
<th>Max.</th>
</tr>
</thead>
<tbody>
{% for r in results %}
<tr>
<td>{{ r.component }}</td>
<td>{{ r.benchmark }}</td>
<td>{{ r.type | capitalize }}</td>
<td>{{ r.average }}</td>
<td>{{ r.minimum ? r.minimum : 'N/a' }}</td>
<td>{{ r.maximum ? r.maximum : 'N/a' }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}