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