importer general
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Modules\MiningChecker\Infrastructure\ConnectionFactory;
|
||||
use Modules\MiningChecker\Infrastructure\ModuleConfig;
|
||||
|
||||
spl_autoload_register(static function (string $class): void {
|
||||
$prefix = 'Modules\\MiningChecker\\';
|
||||
if (!str_starts_with($class, $prefix)) {
|
||||
@@ -14,3 +17,14 @@ spl_autoload_register(static function (string $class): void {
|
||||
require_once $file;
|
||||
}
|
||||
});
|
||||
|
||||
$mm = isset($modules) && $modules instanceof App\ModuleManager ? $modules : modules();
|
||||
$mm->registerFunction('mining-checker', 'sql_import_target', static function (): array {
|
||||
$moduleBasePath = __DIR__;
|
||||
$config = ModuleConfig::load($moduleBasePath);
|
||||
|
||||
return [
|
||||
'pdo' => ConnectionFactory::make($config),
|
||||
'label' => 'Mining-Checker Projekt-Datenbank',
|
||||
];
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
namespace Modules\MiningChecker\Infrastructure;
|
||||
|
||||
use App\SqlDataImporter;
|
||||
use App\UploadedSqlFile;
|
||||
use Modules\MiningChecker\Support\ApiException;
|
||||
use PDO;
|
||||
|
||||
@@ -370,34 +371,16 @@ final class SchemaManager
|
||||
|
||||
public function importSqlFile(array $uploadedFile): array
|
||||
{
|
||||
$errorCode = (int) ($uploadedFile['error'] ?? UPLOAD_ERR_NO_FILE);
|
||||
if ($errorCode !== UPLOAD_ERR_OK) {
|
||||
throw new ApiException(
|
||||
'SQL-Datei konnte nicht hochgeladen werden.',
|
||||
422,
|
||||
['upload_error' => $errorCode]
|
||||
);
|
||||
try {
|
||||
$file = UploadedSqlFile::read($uploadedFile);
|
||||
} catch (\RuntimeException $exception) {
|
||||
throw new ApiException($exception->getMessage(), 422);
|
||||
}
|
||||
|
||||
$originalName = (string) ($uploadedFile['name'] ?? 'import.sql');
|
||||
$tmpPath = (string) ($uploadedFile['tmp_name'] ?? '');
|
||||
if ($tmpPath === '' || !is_uploaded_file($tmpPath)) {
|
||||
throw new ApiException('Ungueltige Upload-Datei fuer SQL-Import.', 422);
|
||||
}
|
||||
|
||||
if (!preg_match('/\.sql$/i', $originalName)) {
|
||||
throw new ApiException('Bitte eine SQL-Datei mit Endung .sql hochladen.', 422, ['file' => $originalName]);
|
||||
}
|
||||
|
||||
$sql = @file_get_contents($tmpPath);
|
||||
if (!is_string($sql) || trim($sql) === '') {
|
||||
throw new ApiException('Die hochgeladene SQL-Datei ist leer oder konnte nicht gelesen werden.', 422, ['file' => $originalName]);
|
||||
}
|
||||
|
||||
$statementCount = $this->executeSqlContent($sql, $originalName);
|
||||
$statementCount = $this->executeSqlContent((string) $file['sql'], (string) $file['file']);
|
||||
|
||||
return [
|
||||
'file' => $originalName,
|
||||
'file' => (string) $file['file'],
|
||||
'statement_count' => $statementCount,
|
||||
'message' => 'SQL-Datei wurde erfolgreich eingespielt.',
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user