API 1.0.0-rc

This commit is contained in:
2021-07-27 22:29:56 -04:00
parent c2d7729e71
commit 6172da2f95
33 changed files with 1013 additions and 0 deletions

46
api/setup/app.php Normal file
View File

@ -0,0 +1,46 @@
<?php
use DI\ContainerBuilder as Builder;
use DI\Bridge\Slim\Bridge as Bridge;
use Zeuxisoo\Whoops\Slim\WhoopsMiddleware;
include_once 'composer.php';
$builder = new Builder();
$folders = [
'settings',
'setups'
];
foreach ($folders as $f) {
$folder = implode(DIRECTORY_SEPARATOR, [__DIR__, $f]);
if (!file_exists($folder)) {
continue;
}
$files = new DirectoryIterator($folder);
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
$builder->addDefinitions($file->getRealPath());
}
}
$container = $builder->build();
$app = Bridge::create($container);
$app->addRoutingMiddleware();
$app->add(new WhoopsMiddleware());
$folder = 'middlewares';
if (file_exists($folder)) {
$files = new DirectoryIterator($folder);
foreach ($files as $file) {
if ($file->isDir() or $file->getExtension() != 'php') {
continue;
}
include_once $file->getRealPath();
}
}
include_once 'databases.php';
include_once 'router.php';

6
api/setup/composer.php Normal file
View File

@ -0,0 +1,6 @@
<?php
require_once implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__),
'vendor',
'autoload.php'
]);

37
api/setup/databases.php Normal file
View File

@ -0,0 +1,37 @@
<?php
$databases = $app->getContainer()->get('databases');
foreach ($databases->databases as $name => $settings) {
if (!is_object($settings)) {
continue;
}
$auth = false;
$dsn = '';
switch (strtolower($settings->type)) {
case 'mysql':
$data = [
['host', $settings->host->name],
['dbname', $settings->name]
];
if (isset($settings->host->port)) {
$data []= ['port', $settings->host->port];
}
array_walk($data, function(&$item) {
$item = implode('=', $item);
});
$dsn = implode(':', [
'mysql',
implode(';', $data)
]);
$auth = true;
break;
}
ORM::configure($dsn, null, $name);
if ($auth) {
ORM::configure('username', $settings->user->name, $name);
ORM::configure('password', $settings->user->password, $name);
}
}
if (isset($databases->short_names) and $databases->short_names) {
Model::$short_table_names = true;
}

9
api/setup/router.php Normal file
View File

@ -0,0 +1,9 @@
<?php
$folder = $app->getContainer()->get('folders')->routes;
$files = new DirectoryIterator($folder);
foreach ($files as $file) {
if ($file->isDir() or $file->getExtension() != 'php') {
continue;
}
include_once $file->getRealPath();
}

View File

@ -0,0 +1,4 @@
<?php
return [
'debug' => $_ENV['DEBUG'] ?? false
];

View File

@ -0,0 +1,19 @@
<?php
use Psr\Container\ContainerInterface as Container;
return [
'folders' => function(Container $c) {
$arr = [
'base' => dirname(__DIR__, 2)
];
$arr['resources'] = implode(DIRECTORY_SEPARATOR, [
$arr['base'],
'resources'
]);
$arr['routes'] = implode(DIRECTORY_SEPARATOR, [
$arr['resources'],
'routes'
]);
return (object) $arr;
}
];

View File

@ -0,0 +1,28 @@
<?php
return [
'databases' => function() {
$arr = [
ORM::DEFAULT_CONNECTION => [
'type' => 'mysql',
'host' => [
'name' => $_ENV['MYSQL_HOST'] ?? 'db'
],
'user' => [
'name' => $_ENV['MYSQL_USER'],
'password' => $_ENV['MYSQL_PASSWORD']
],
'name' => $_ENV['MYSQL_DATABASE']
]
];
function toObj($arr) {
$obj = (object) $arr;
foreach ($arr as $k => $v) {
if (is_array($v)) {
$obj->{$k} = toObj($v);
}
}
return $obj;
}
return (object) ['databases' => toObj($arr), 'short_names' => true];
}
];