@@ -124,94 +124,39 @@ protected function buildFixtures(string $fixturesDir, array $connections, InputI
124124 return static ::CODE_ERROR ;
125125 }
126126
127- $ output ->writeln (sprintf ('Building fixtures in <info>%-40s</info> ' . ($ input ->getOption ('exclude-database ' ) ? '(exclude-database) ' : '' ), $ fixturesDir ));
127+ $ vendor = $ input ->getOption ('vendor ' );
128+ $ dsn = $ input ->getOption ('dsn ' );
129+ $ user = $ input ->getOption ('user ' );
130+ $ password = $ input ->getOption ('password ' );
131+ $ verbose = $ input ->getOption ('verbose ' );
132+ $ excludeDataBase = (bool )$ input ->getOption ('exclude-database ' );
128133
129- chdir ($ this ->root . '/ ' . $ fixturesDir );
130-
131- if (is_file ('propel.yaml.dist ' )) {
132- $ content = (string )file_get_contents ('propel.yaml.dist ' );
134+ $ output ->writeln (sprintf ('Building fixtures in <info>%-40s</info> %s ' , $ fixturesDir , $ excludeDataBase ? '(exclude-database) ' : '' ));
133135
134- $ content = str_replace ('##DATABASE_VENDOR## ' , $ input ->getOption ('vendor ' ), $ content );
135- $ content = str_replace ('##DATABASE_URL## ' , $ input ->getOption ('dsn ' ), $ content );
136- $ content = str_replace ('##DATABASE_USER## ' , $ input ->getOption ('user ' ), $ content );
137- $ content = str_replace ('##DATABASE_PASSWORD## ' , $ input ->getOption ('password ' ), $ content );
136+ chdir ($ this ->root . '/ ' . $ fixturesDir );
138137
139- file_put_contents ('propel.yaml ' , $ content );
140- } else {
138+ if (!$ this ->updatePropelYamlDistFile ($ vendor , $ dsn , $ user , $ password )) {
141139 $ output ->writeln ('<comment>No "propel.yaml.dist" file found, skipped.</comment> ' );
142140 }
143141
144- if (is_file ('propel.yaml ' )) {
145- $ in = new ArrayInput ([
146- 'command ' => 'config:convert ' ,
147- '--output-dir ' => './build/conf ' ,
148- '--output-file ' => sprintf ('%s-conf.php ' , $ connections [0 ]), // the first connection is the main one
149- '--loader-script-dir ' => './build/conf ' ,
150- ]);
142+ $ mainConnection = $ connections [0 ];
143+ $ this ->runConfigConvertCommand ($ output , $ mainConnection );
151144
152- $ command = $ this ->getApplication ()->find ('config:convert ' );
153- $ command ->run ($ in , $ output );
145+ $ hasSchemasInCurrentDir = count ($ this ->getSchemas ('. ' )) > 0 ;
146+ if (!$ hasSchemasInCurrentDir ) {
147+ return static ::CODE_SUCCESS ;
154148 }
155149
156- if (0 < count ($ this ->getSchemas ('. ' ))) {
157- $ in = new ArrayInput ([
158- 'command ' => 'model:build ' ,
159- '--schema-dir ' => '. ' ,
160- '--output-dir ' => 'build/classes/ ' ,
161- '--loader-script-dir ' => './build/conf ' ,
162- '--platform ' => ucfirst ($ input ->getOption ('vendor ' )) . 'Platform ' ,
163- '--verbose ' => $ input ->getOption ('verbose ' ),
164- ]);
165-
166- $ command = $ this ->getApplication ()->find ('model:build ' );
167- $ command ->run ($ in , $ output );
168- }
150+ $ this ->runModelBuildCommand ($ output , $ vendor , $ verbose );
169151
170- if ($ input -> getOption ( ' exclude-database ' ) ) {
152+ if ($ excludeDataBase ) {
171153 return static ::CODE_SUCCESS ;
172154 }
173155
174- if (0 < count ($ this ->getSchemas ('. ' ))) {
175- $ in = new ArrayInput ([
176- 'command ' => 'sql:build ' ,
177- '--schema-dir ' => '. ' ,
178- '--output-dir ' => 'build/sql/ ' ,
179- '--platform ' => ucfirst ($ input ->getOption ('vendor ' )) . 'Platform ' ,
180- '--verbose ' => $ input ->getOption ('verbose ' ),
181- ]);
182-
183- $ command = $ this ->getApplication ()->find ('sql:build ' );
184- $ command ->run ($ in , $ output );
185-
186- $ conParams = [];
187- foreach ($ connections as $ con ) {
188- if (substr ($ input ->getOption ('dsn ' ), 0 , 6 ) === 'sqlite ' ) {
189- $ conParams [] = sprintf (
190- '%s=%s ' ,
191- $ con ,
192- $ input ->getOption ('dsn ' ),
193- );
194- } else {
195- $ conParams [] = sprintf (
196- '%s=%s;user=%s;password=%s ' ,
197- $ con ,
198- $ input ->getOption ('dsn ' ),
199- $ input ->getOption ('user ' ),
200- $ input ->getOption ('password ' ),
201- );
202- }
203- }
204-
205- $ in = new ArrayInput ([
206- 'command ' => 'sql:insert ' ,
207- '--sql-dir ' => 'build/sql/ ' ,
208- '--connection ' => $ conParams ,
209- '--verbose ' => $ input ->getOption ('verbose ' ),
210- ]);
156+ $ this ->runSqlBuildCommand ($ output , $ vendor , $ verbose );
211157
212- $ command = $ this ->getApplication ()->find ('sql:insert ' );
213- $ command ->run ($ in , $ output );
214- }
158+ $ connectionStrings = $ this ->buildConnectionString ($ connections , $ dsn , $ user , $ password );
159+ $ this ->runSqlInsert ($ output , $ connectionStrings , $ verbose );
215160
216161 return static ::CODE_SUCCESS ;
217162 }
@@ -225,4 +170,134 @@ protected function resetCounters(): void
225170 {
226171 AggregateMultipleColumnsBehavior::resetInsertedAggregationNames ();
227172 }
173+
174+ /**
175+ * Updates connection information in propel.yaml.dist in current working directory.
176+ *
177+ * @param string $vendor
178+ * @param string $dsn
179+ * @param string $user
180+ * @param string $password
181+ *
182+ * @return bool
183+ */
184+ protected function updatePropelYamlDistFile (string $ vendor , string $ dsn , string $ user , string $ password ): bool
185+ {
186+ if (!is_file ('propel.yaml.dist ' )) {
187+ return false ;
188+ }
189+
190+ $ content = (string )file_get_contents ('propel.yaml.dist ' );
191+
192+ $ content = str_replace ('##DATABASE_VENDOR## ' , $ vendor , $ content );
193+ $ content = str_replace ('##DATABASE_URL## ' , $ dsn , $ content );
194+ $ content = str_replace ('##DATABASE_USER## ' , $ user , $ content );
195+ $ content = str_replace ('##DATABASE_PASSWORD## ' , $ password , $ content );
196+
197+ file_put_contents ('propel.yaml ' , $ content );
198+
199+ return true ;
200+ }
201+
202+ /**
203+ * Convert local propel.yaml file via config:convert command.
204+ *
205+ * @param \Symfony\Component\Console\Output\OutputInterface $output
206+ * @param string $connection
207+ *
208+ * @return void
209+ */
210+ protected function runConfigConvertCommand (OutputInterface $ output , string $ connection ): void
211+ {
212+ if (!is_file ('propel.yaml ' )) {
213+ return ;
214+ }
215+ $ in = new ArrayInput ([
216+ 'command ' => 'config:convert ' ,
217+ '--output-dir ' => './build/conf ' ,
218+ '--output-file ' => sprintf ('%s-conf.php ' , $ connection ),
219+ '--loader-script-dir ' => './build/conf ' ,
220+ ]);
221+
222+ $ command = $ this ->getApplication ()->find ('config:convert ' );
223+ $ command ->run ($ in , $ output );
224+ }
225+
226+ /**
227+ * @param \Symfony\Component\Console\Output\OutputInterface $output
228+ * @param string $vendor
229+ * @param string $verbose
230+ *
231+ * @return void
232+ */
233+ protected function runModelBuildCommand (OutputInterface $ output , string $ vendor , string $ verbose ): void
234+ {
235+ $ in = new ArrayInput ([
236+ 'command ' => 'model:build ' ,
237+ '--schema-dir ' => '. ' ,
238+ '--output-dir ' => 'build/classes/ ' ,
239+ '--loader-script-dir ' => './build/conf ' ,
240+ '--platform ' => ucfirst ($ vendor ) . 'Platform ' ,
241+ '--verbose ' => $ verbose ,
242+ ]);
243+
244+ $ command = $ this ->getApplication ()->find ('model:build ' );
245+ $ command ->run ($ in , $ output );
246+ }
247+
248+ /**
249+ * @param \Symfony\Component\Console\Output\OutputInterface $output
250+ * @param string $vendor
251+ * @param string $verbose
252+ *
253+ * @return void
254+ */
255+ protected function runSqlBuildCommand (OutputInterface $ output , string $ vendor , string $ verbose ): void
256+ {
257+ $ in = new ArrayInput ([
258+ 'command ' => 'sql:build ' ,
259+ '--schema-dir ' => '. ' ,
260+ '--output-dir ' => 'build/sql/ ' ,
261+ '--platform ' => ucfirst ($ vendor ) . 'Platform ' ,
262+ '--verbose ' => $ verbose ,
263+ ]);
264+
265+ $ command = $ this ->getApplication ()->find ('sql:build ' );
266+ $ command ->run ($ in , $ output );
267+ }
268+
269+ /**
270+ * @param array<string> $connections
271+ * @param string $dsn
272+ * @param string $user
273+ * @param string $password
274+ *
275+ * @return array<string>
276+ */
277+ protected function buildConnectionString (array $ connections , string $ dsn , string |null $ user , string |null $ password ): array
278+ {
279+ $ isSqlite = substr ($ dsn , 0 , 6 ) === 'sqlite ' ;
280+
281+ return array_map (fn ($ con ) => $ isSqlite ? "$ con= $ dsn " : "$ con= $ dsn;user= $ user;password= $ password " , $ connections );
282+ }
283+
284+ /**
285+ * @param \Symfony\Component\Console\Output\OutputInterface $output
286+ * @param array<string> $connectionStrings
287+ * @param string $verbose
288+ *
289+ * @return void
290+ */
291+ protected function runSqlInsert (OutputInterface $ output , array $ connectionStrings , string $ verbose ): void
292+ {
293+ $ in = new ArrayInput ([
294+ 'command ' => 'sql:insert ' ,
295+ '--sql-dir ' => 'build/sql/ ' ,
296+ '--connection ' => $ connectionStrings ,
297+ '--verbose ' => $ verbose ,
298+ ]);
299+
300+ $ command = $ this ->getApplication ()->find ('sql:insert ' );
301+ $ command ->run ($ in , $ output );
302+ }
228303}
0 commit comments