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