1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Console; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Schema\SchemaManagerInterface; |
6
|
|
|
use Bdf\Prime\Schema\Visitor\MapperVisitor; |
7
|
|
|
use Bdf\Prime\ServiceLocator; |
8
|
|
|
use Bdf\Util\Console\BdfStyle; |
9
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* MapperCommand |
17
|
|
|
*/ |
18
|
|
|
#[AsCommand('prime:mapper', 'Generate mapper prototype class')] |
19
|
|
|
class MapperCommand extends Command |
20
|
|
|
{ |
21
|
|
|
protected static $defaultName = 'prime:mapper'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ServiceLocator |
25
|
|
|
*/ |
26
|
|
|
private $locator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* MapperCommand constructor. |
30
|
|
|
* |
31
|
|
|
* @param ServiceLocator $locator |
32
|
|
|
*/ |
33
|
|
|
public function __construct(ServiceLocator $locator) |
34
|
|
|
{ |
35
|
|
|
$this->locator = $locator; |
36
|
|
|
|
37
|
|
|
parent::__construct(static::$defaultName); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
protected function configure() |
44
|
|
|
{ |
45
|
|
|
$this |
46
|
|
|
->setDescription('Generate mapper prototype class') |
47
|
|
|
->addOption('table', 't', InputOption::VALUE_REQUIRED, 'the table name to import. Default: all tables from database.') |
48
|
|
|
->addOption('connection', 'c', InputOption::VALUE_REQUIRED, 'Connection name. Default: the default prime connection.') |
49
|
|
|
->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Path to output mappers files.') |
50
|
|
|
; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
57
|
|
|
{ |
58
|
|
|
$io = new BdfStyle($input, $output); |
59
|
|
|
|
60
|
|
|
$connection = $this->locator->connection($io->option('connection')); |
61
|
|
|
$schemaManager = $connection->schema(); |
62
|
|
|
|
63
|
|
|
if (!$schemaManager instanceof SchemaManagerInterface) { |
64
|
|
|
$io->error('The connection "%s" do not handle schema', $io->option('connection')); |
65
|
|
|
return 1; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($io->option('table')) { |
69
|
|
|
$schema = $schemaManager->schema( |
70
|
|
|
$schemaManager->load($io->option('table')) |
71
|
|
|
); |
72
|
|
|
} else { |
73
|
|
|
$schema = $schemaManager->loadSchema(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$visitor = new MapperVisitor($connection->getName(), $this->locator->mappers()->getNameResolver()); |
77
|
|
|
$schema->visit($visitor); |
|
|
|
|
78
|
|
|
|
79
|
|
|
if (!$io->option('output')) { |
80
|
|
|
$io->line($visitor->getOutput()); |
81
|
|
|
} else { |
82
|
|
|
$io->line('Writting mapper files in directory <info>'.$io->option('output').'</info>'); |
83
|
|
|
$visitor->write($io->option('output')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return 0; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|