[TASK] migration working

This commit is contained in:
chrosey
2020-01-21 22:26:42 +01:00
parent 42c50342ab
commit 699709110e
8 changed files with 786 additions and 4 deletions
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace Chrosey\Inventur\Controller;
use Illuminate\Database\Migrations\DatabaseMigrationRepository;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Filesystem\Filesystem;
use Psr\Container\ContainerInterface;
use Slim\Psr7\Stream;
class DatabaseController
{
protected $container;
function __construct(ContainerInterface $container)
{
$this->container = $container;
}
function migrate($request, $response, $args)
{
$capsule = $this->container->get('db')->getDatabaseManager();
$repository = new DatabaseMigrationRepository($capsule, 'migrations');
if (!$repository->repositoryExists()) {
$repository->createRepository();
}
$migrationFilesPath = __DIR__ . '/../../database/migrations/';
$fs = new Filesystem();
$migrator = new Migrator($repository, $capsule, $fs);
$files = $fs->files($migrationFilesPath);
$migrator->run($files);
}
function getMigrations()
{
}
}
+73
View File
@@ -0,0 +1,73 @@
<?php
namespace Chrosey\Inventur\Controller;
use Psr\Container\ContainerInterface;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Slim\Psr7\Stream;
class SpreadsheetController
{
protected $spreadsheet;
protected $container;
function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->spreadsheet = new Spreadsheet();
$this->spreadsheet->getProperties()
->setCreator("Christian Seyfferth")
->setLastModifiedBy("hristian Seyfferth")
->setTitle("Inventur Tabelle")
->setSubject("Inventur Tabelle")
->setDescription("Inventur Tabelle, generated using PHP classes.")
->setKeywords("office 2016 openxml php")
->setCategory("Test result file");
}
function create($request, $response, $args)
{
$this->getTemplate();
$path = $this->container->get('data_directory');
$writer = new Xlsx($this->spreadsheet);
$writer->save($path . 'template.xlsx');
return $response->withHeader('Location', "excel/download")
->withStatus(200);
}
function getTemplate()
{
$sheet = $this->spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Template !');
}
function getInventurTabelle()
{
}
function download($request, $response, $args)
{
$file = $this->container->get('data_directory') . 'template.xlsx';
$fh = fopen($file, 'rb');
$stream = new Stream($fh);
$response = $response
->withHeader('Content-Description', 'File Transfer')
->withHeader('Content-Type', 'application/force-download')
->withHeader('Content-Type', 'application/octet-stream')
->withHeader('Content-Type', 'application/download')
->withHeader('Content-Disposition', 'attachment;filename="' . basename($file) . '"')
->withHeader('Expires', '0')
->withHeader('Cache-Control', 'must-revalidate')
->withHeader('Pragma', 'public')
->withBody($stream);
return $response;
}
}
+10
View File
@@ -4,6 +4,8 @@ declare(strict_types=1);
use DI\Container;
use Illuminate\Database\Capsule\Manager as Capsule;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
@@ -37,7 +39,15 @@ $container->set('db', static function () {
return $capsule;
});
$container->set('log', static function() {
$log = new Logger('inventur');
$log->pushHandler(new StreamHandler(__DIR__.'/../var/logs/app.log', Logger::WARNING));
return $log;
});
$container->set('upload_directory', __DIR__ . '/../data/uploads');
$container->set('data_directory', __DIR__ . '/../data/');
$app = AppFactory::create();
+128 -1
View File
@@ -2,11 +2,13 @@
declare(strict_types=1);
use Chrosey\Inventur\Controller\DatabaseController;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Psr7\Stream;
use Slim\Psr7\UploadedFile;
use Slim\Routing\RouteCollectorProxy;
use Chrosey\Inventur\Controller\SpreadsheetController;
$app->get('/', function (Request $request, Response $response, $args) {
return $this->get('view')->render($response, 'frontend.html', []);
@@ -26,7 +28,7 @@ $app->group('/api', function (RouteCollectorProxy $group): void {
});
$group->get('/artikel/theater', function (Request $request, Response $response, $args) {
$file= __DIR__ . '/../data/articles.theater.json';
$file = __DIR__ . '/../data/articles.theater.json';
$fh = fopen($file, 'rb');
$stream = new Stream($fh);
@@ -48,8 +50,133 @@ $app->group('/api', function (RouteCollectorProxy $group): void {
return $response;
});
$group->get('/excel', SpreadsheetController::class . ':create');
$group->get('/excel/download', SpreadsheetController::class . ':download');
});
$app->get('/migrate', DatabaseController::class . ':migrate')->setName('migrations');
$app->get('/seed', function (Request $request, Response $response, $args) {
$db = $this->get('db');
if ($db::schema()->hasTable('dimensions')) {
$entries = [
[
'name' => 'Liter',
'short' => 'l'
], [
'name' => 'Stück',
'short' => 'Stk'
], [
'name' => 'Flasche',
'short' => 'Fl'
], [
'name' => 'Glas',
'short' => 'Gl'
], [
'name' => 'Tasse',
'short' => 'T'
]
];
foreach ($entries as $item) {
try {
$db->table('dimensions')->insert($item);
} catch (\Throwable $th) {
$this->get('log')->error($th);
}
}
}
if ($db::schema()->hasTable('group')) {
$entries = [
[
'name' => 'Bier'
], [
'name' => 'afG Gastro'
], [
'name' => 'afG offen'
], [
'name' => 'Wein und Sekt'
], [
'name' => 'Fingerfood'
], [
'name' => 'Heißgetränke'
]
];
foreach ($entries as $item) {
try {
$db->table('group')->insert($item);
} catch (\Throwable $th) {
$this->get('log')->error($th);
}
}
}
if ($db::schema()->hasTable('sets')) {
$entries = [
[
'name' => 'UG'
], [
'name' => 'mobile'
], [
'name' => 'Studio'
], [
'name' => 'Domstufen'
]
];
foreach ($entries as $item) {
try {
$db->table('sets')->insert($item);
} catch (\Throwable $th) {
$this->get('log')->error($th);
}
}
}
if ($db::schema()->hasTable('articles')) {
$file = file_get_contents(__DIR__ . '/../data/articles.theater.json');
$entries = json_decode($file, true);
foreach ($entries as $entry) {
try {
$cDim = $db
->table('dimensions')
->where(
'short',
'=',
str_replace('.', '', $entry['dimension'])
)->first();
$pDim = $db
->table('dimensions')
->where(
'short',
'=',
str_replace('.', '', $entry['portion']['type'])
)->first();
$item = [
'name' => $entry['name'],
'short' => $entry['short'],
'content_size' => $entry['content']['size'],
'portion_size' => $entry['portion']['size'],
'content_dimension' => $cDim->id,
'portion_dimension' => $pDim->id,
'portion_price' => $entry['portion']['price'],
'group_id' => 1
];
print_r($item);
$db->table('articles')->insert($item);
} catch (\Throwable $th) {
$this->get('log')->error($th);
}
}
}
return $this->get('view')->render($response, 'frontend.html', []);
})->setName('migrations');
/**
* Move Uploaded File to Target Destination
*