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