PR CI / ledger (pull_request) Successful in 3s
PR CI / flow (pull_request) Successful in 4s
PR CI / ui (pull_request) Successful in 17s
PR CI / test (lowest) (pull_request) Successful in 45s
PR CI / test (stable) (pull_request) Successful in 48s
PR CI / security (pull_request) Successful in 54s
PR CI / pr-ci (pull_request) Successful in 1s
PR CI / release-automerge (pull_request) Successful in 3s
71 lines
2.6 KiB
PHP
71 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Console;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use InvalidArgumentException;
|
|
use IriksIT\Manager\Support\Importer;
|
|
|
|
/**
|
|
* Copies a site's existing table into a table of the package. The source is only read.
|
|
*
|
|
* php artisan manager:import posts --table=articles --map=title=headline --map=body=content --dry-run
|
|
*
|
|
* Mappings can also be set once in config `manager.import.<target>` (`table`, `connection`, `map`).
|
|
*/
|
|
class ImportCommand extends Command
|
|
{
|
|
protected $signature = 'manager:import {target : posts, pages or reviews}
|
|
{--table= : Table to read (default: config manager.import.<target>.table)}
|
|
{--connection= : Database connection of that table (default: the default one)}
|
|
{--map=* : package_column=source_column, repeat for each column that is named differently}
|
|
{--limit= : Only the first rows}
|
|
{--dry-run : Count what would happen and write nothing}';
|
|
|
|
protected $description = 'Copy the existing posts, pages or reviews of the site into the package tables';
|
|
|
|
public function handle(Importer $importer): int
|
|
{
|
|
$target = (string) $this->argument('target');
|
|
$config = (array) config('manager.import.'.$target, []);
|
|
$table = $this->option('table') ?: ($config['table'] ?? null);
|
|
|
|
if (! is_string($table) || $table === '') {
|
|
$this->error('Which table? Give --table=... or set manager.import.'.$target.'.table.');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$map = (array) ($config['map'] ?? []);
|
|
|
|
foreach ((array) $this->option('map') as $pair) {
|
|
if (! str_contains((string) $pair, '=')) {
|
|
$this->error("--map wants package_column=source_column, got {$pair}");
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
[$to, $from] = explode('=', (string) $pair, 2);
|
|
$map[trim($to)] = trim($from);
|
|
}
|
|
|
|
try {
|
|
$connection = DB::connection($this->option('connection') ?: ($config['connection'] ?? null));
|
|
$result = $importer->run($target, $connection, $table, $map, (bool) $this->option('dry-run'), $this->option('limit') !== null ? (int) $this->option('limit') : null);
|
|
} catch (InvalidArgumentException $e) {
|
|
$this->error($e->getMessage());
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
foreach ($result['problems'] as $problem) {
|
|
$this->warn($problem);
|
|
}
|
|
|
|
$this->info(($this->option('dry-run') ? 'Dry run: ' : '')."{$result['created']} created, {$result['updated']} updated, {$result['skipped']} skipped.");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|