This commit is contained in:
2021-11-30 18:04:41 -03:00
parent 87323b22d8
commit f589ff960b
56 changed files with 1960 additions and 0 deletions

53
setup/app.php Normal file
View File

@ -0,0 +1,53 @@
<?php
use DI\ContainerBuilder as Builder;
use DI\Bridge\Slim\Bridge;
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);
if ($app->getContainer()->has('base_url')) {
$app->setBasePath($app->getContainer()->get('base_url'));
}
$folder = implode(DIRECTORY_SEPARATOR, [
__DIR__,
'middlewares'
]);
if (file_exists($folder)) {
$files = new DirectoryIterator($folder);
foreach ($files as $file) {
if ($file->isDir() and $file->getExtension() != 'php') {
continue;
}
include_once $file->getRealPath();
}
}
$app->addRoutingMiddleware();
include_once 'databases.php';
include_once 'router.php';

6
setup/composer.php Normal file
View File

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

24
setup/databases.php Normal file
View File

@ -0,0 +1,24 @@
<?php
$database = $app->getContainer()->get('database');
foreach ($database->databases as $name => $settings) {
switch (strtolower($settings->engine)) {
case 'mysql':
$dsn = "mysql:host={$settings->host->name};dbname={$settings->name}" . (isset($settings->host->port) ? ';port=' . $settings->host->port : '');
ORM::configure([
'connection_string' => $dsn,
'username' => $settings->user->name,
'password' => $settings->user->password
], null, $name);
break;
}
if (isset($settings->logging) and $settings->logging) {
ORM::configure('logging', true, $name);
}
if (isset($settings->caching) and $settings->caching) {
ORM::configure('caching', true, $name);
}
}
if (isset($database->short_names) and $database->short_names) {
Model::$short_table_names = true;
}

View File

@ -0,0 +1,4 @@
<?php
use Psr\Container\ContainerInterface as Container;
$app->add($app->getContainer()->get(Zeuxisoo\Whoops\Slim\WhoopsMiddleware::class));

View File

@ -0,0 +1,2 @@
<?php
$app->add($app->getContainer()->get(Incoviba\API\Common\Middleware\Auth::class));

9
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,5 @@
<?php
return [
'debug' => $_ENV['DEBUG'] ?? false,
'AUTH_KEY' => $_ENV['API_KEY']
];

View File

@ -0,0 +1,24 @@
<?php
return [
'database' => function() {
$arr = [
'default' => (object) [
'engine' => 'mysql',
'host' => (object) [
'name' => $_ENV['MYSQL_HOST'] ?? 'db'
],
'user' => (object) [
'name' => $_ENV['MYSQL_USER'],
'password' => $_ENV['MYSQL_PASSWORD']
],
'name' => $_ENV['MYSQL_DATABASE'],
'logging' => true,
'caching' => true
]
];
return (object) [
'short_names' => true,
'databases' => $arr
];
}
];

View File

@ -0,0 +1,15 @@
<?php
return [
'folders' => function() {
$arr = ['base' => dirname(__FILE__, 3)];
$arr['resources'] = implode(DIRECTORY_SEPARATOR, [
$arr['base'],
'resources'
]);
$arr['routes'] = implode(DIRECTORY_SEPARATOR, [
$arr['resources'],
'routes'
]);
return (object) $arr;
}
];

11
setup/setups/01_env.php Normal file
View File

@ -0,0 +1,11 @@
<?php
use Psr\Container\ContainerInterface as Container;
return [
Zeuxisoo\Whoops\Slim\WhoopsMiddleware::class => function(Container $c) {
return new Zeuxisoo\Whoops\Slim\WhoopsMiddleware([
'enable' => $c->get('debug'),
'editor' => 'vscode'
]);
}
];

15
setup/setups/02_auth.php Normal file
View File

@ -0,0 +1,15 @@
<?php
use Psr\Container\ContainerInterface as Container;
return [
Incoviba\API\Common\Service\Auth::class => function(Container $c) {
return new Incoviba\API\Common\Service\Auth($c->get('AUTH_KEY'));
},
Incoviba\API\Common\Middleware\Auth::class => function(Container $c) {
return new Incoviba\API\Common\Middleware\Auth(
$c->get(Incoviba\API\Common\Service\Auth::class),
$c->get(Slim\Psr7\Factory\ResponseFactory::class),
['/auth/generate']
);
}
];