1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DoctrineORMModule; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; |
8
|
|
|
use Doctrine\Migrations\Tools\Console\Command\VersionCommand; |
9
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
10
|
|
|
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; |
11
|
|
|
use Interop\Container\ContainerInterface; |
12
|
|
|
use Laminas\Stdlib\ArrayUtils; |
13
|
|
|
use Symfony\Component\Console\Application; |
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Helper\DialogHelper; |
16
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
17
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
19
|
|
|
use function assert; |
20
|
|
|
use function class_exists; |
21
|
|
|
|
22
|
|
|
class CliConfigurator |
23
|
|
|
{ |
24
|
|
|
/** @var string */ |
25
|
|
|
private $defaultObjectManagerName = 'doctrine.entitymanager.orm_default'; |
26
|
|
|
|
27
|
|
|
/** @var string[] */ |
28
|
|
|
private $commands = [ |
29
|
|
|
'doctrine.dbal_cmd.runsql', |
30
|
|
|
'doctrine.dbal_cmd.import', |
31
|
|
|
'doctrine.orm_cmd.clear_cache_metadata', |
32
|
|
|
'doctrine.orm_cmd.clear_cache_result', |
33
|
|
|
'doctrine.orm_cmd.clear_cache_query', |
34
|
|
|
'doctrine.orm_cmd.schema_tool_create', |
35
|
|
|
'doctrine.orm_cmd.schema_tool_update', |
36
|
|
|
'doctrine.orm_cmd.schema_tool_drop', |
37
|
|
|
'doctrine.orm_cmd.ensure_production_settings', |
38
|
|
|
'doctrine.orm_cmd.convert_d1_schema', |
39
|
|
|
'doctrine.orm_cmd.generate_repositories', |
40
|
|
|
'doctrine.orm_cmd.generate_entities', |
41
|
|
|
'doctrine.orm_cmd.generate_proxies', |
42
|
|
|
'doctrine.orm_cmd.convert_mapping', |
43
|
|
|
'doctrine.orm_cmd.run_dql', |
44
|
|
|
'doctrine.orm_cmd.validate_schema', |
45
|
|
|
'doctrine.orm_cmd.info', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** @var string[] */ |
49
|
|
|
private $migrationCommands = [ |
50
|
|
|
'doctrine.migrations_cmd.execute', |
51
|
|
|
'doctrine.migrations_cmd.generate', |
52
|
|
|
'doctrine.migrations_cmd.migrate', |
53
|
|
|
'doctrine.migrations_cmd.status', |
54
|
|
|
'doctrine.migrations_cmd.version', |
55
|
|
|
'doctrine.migrations_cmd.diff', |
56
|
|
|
'doctrine.migrations_cmd.latest', |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** @var ContainerInterface */ |
60
|
|
|
private $container; |
61
|
|
|
|
62
|
|
|
public function __construct(ContainerInterface $container) |
63
|
|
|
{ |
64
|
|
|
$this->container = $container; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function configure(Application $cli) : void |
68
|
|
|
{ |
69
|
|
|
$commands = $this->getAvailableCommands(); |
70
|
|
|
foreach ($commands as $commandName) { |
71
|
|
|
$command = $this->container->get($commandName); |
72
|
|
|
assert($command instanceof Command); |
73
|
|
|
$command->getDefinition()->addOption($this->createObjectManagerInputOption()); |
74
|
|
|
|
75
|
|
|
$cli->add($command); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$objectManager = $this->container->get($this->getObjectManagerName()); |
79
|
|
|
assert($objectManager instanceof EntityManagerInterface); |
80
|
|
|
|
81
|
|
|
$helpers = $this->getHelpers($objectManager); |
82
|
|
|
foreach ($helpers as $name => $instance) { |
83
|
|
|
$cli->getHelperSet()->set($instance, $name); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return mixed[] |
89
|
|
|
*/ |
90
|
|
|
private function getHelpers(EntityManagerInterface $objectManager) : array |
91
|
|
|
{ |
92
|
|
|
$helpers = []; |
93
|
|
|
|
94
|
|
|
if (class_exists('Symfony\Component\Console\Helper\QuestionHelper')) { |
95
|
|
|
$helpers['dialog'] = new QuestionHelper(); |
96
|
|
|
} else { |
97
|
|
|
$helpers['dialog'] = new DialogHelper(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$helpers['db'] = new ConnectionHelper($objectManager->getConnection()); |
101
|
|
|
$helpers['em'] = new EntityManagerHelper($objectManager); |
102
|
|
|
|
103
|
|
|
return $helpers; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function createObjectManagerInputOption() : InputOption |
107
|
|
|
{ |
108
|
|
|
return new InputOption( |
109
|
|
|
'object-manager', |
110
|
|
|
null, |
111
|
|
|
InputOption::VALUE_OPTIONAL, |
112
|
|
|
'The name of the object manager to use.', |
113
|
|
|
$this->defaultObjectManagerName |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function getObjectManagerName() : string |
118
|
|
|
{ |
119
|
|
|
$arguments = new ArgvInput(); |
120
|
|
|
|
121
|
|
|
if (! $arguments->hasParameterOption('--object-manager')) { |
122
|
|
|
return $this->defaultObjectManagerName; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $arguments->getParameterOption('--object-manager'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return mixed[] |
130
|
|
|
*/ |
131
|
|
|
private function getAvailableCommands() : array |
132
|
|
|
{ |
133
|
|
|
if (class_exists(VersionCommand::class)) { |
134
|
|
|
return ArrayUtils::merge($this->commands, $this->migrationCommands); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->commands; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|