1919use Symfony \Component \HttpKernel \Bundle \Bundle ;
2020use Symfony \Component \HttpKernel \Bundle \BundleInterface ;
2121use Symfony \Component \HttpKernel \KernelInterface ;
22+ use Propel \Bundle \PropelBundle \Service \SchemaLocator ;
2223
2324/**
2425 * Wrapper for Propel commands.
@@ -67,6 +68,12 @@ abstract class AbstractCommand extends Command
6768 */
6869 protected $ input ;
6970
71+ /**
72+ *
73+ * @var OutputInterface
74+ */
75+ protected $ output ;
76+
7077 private ContainerInterface $ container ;
7178
7279 public function __construct (ContainerInterface $ container , $ name = null )
@@ -140,6 +147,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
140147 }
141148
142149 $ this ->input = $ input ;
150+ $ this ->output = $ output ;
143151
144152 $ this ->checkConfiguration ();
145153
@@ -241,24 +249,19 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)
241249 {
242250 $ filesystem = new Filesystem ();
243251
244- if (!is_dir ($ cacheDir )) {
245- $ filesystem ->mkdir ($ cacheDir );
246- }
247-
248252 $ base = ltrim (realpath ($ kernel ->getProjectDir ()), DIRECTORY_SEPARATOR );
249253
254+ /** @var array<string, array{?BundleInterface, \SplFileInfo}> $finalSchemas */
250255 $ finalSchemas = $ this ->getFinalSchemas ($ kernel , $ this ->bundle );
251256 foreach ($ finalSchemas as $ schema ) {
252257 list ($ bundle , $ finalSchema ) = $ schema ;
253258
254- $ tempSchema = $ bundle ->getName ().'- ' .$ finalSchema ->getBaseName ();
255- $ this ->tempSchemas [$ tempSchema ] = array (
256- 'bundle ' => $ bundle ->getName (),
257- 'basename ' => $ finalSchema ->getBaseName (),
258- 'path ' => $ finalSchema ->getPathname (),
259- );
259+ if ($ bundle ) {
260+ $ file = $ cacheDir .DIRECTORY_SEPARATOR .'bundle- ' .$ bundle ->getName ().'- ' .$ finalSchema ->getBaseName ();
261+ } else {
262+ $ file = $ cacheDir .DIRECTORY_SEPARATOR .'app- ' .$ finalSchema ->getBaseName ();
263+ }
260264
261- $ file = $ cacheDir .DIRECTORY_SEPARATOR .$ tempSchema ;
262265 $ filesystem ->copy ((string ) $ finalSchema , $ file , true );
263266
264267 // the package needs to be set absolute
@@ -269,31 +272,47 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)
269272 if (isset ($ database ['package ' ])) {
270273 // Do not use the prefix!
271274 // This is used to override the package resulting from namespace conversion.
272- $ package = $ database ['package ' ];
275+ $ database [ ' package ' ] = $ database ['package ' ];
273276 } elseif (isset ($ database ['namespace ' ])) {
274- $ package = $ this ->getPackage ($ bundle , $ database ['namespace ' ], $ base );
277+ if ($ bundle ) {
278+ $ database ['package ' ] = $ this ->getPackage ($ bundle , (string )$ database ['namespace ' ], $ base );
279+ } else {
280+ $ database ['package ' ] = $ this ->getPackageFromApp ((string )$ database ['namespace ' ]);
281+ }
275282 } else {
276283 throw new \RuntimeException (
277- sprintf ('%s : Please define a `package` attribute or a `namespace` attribute for schema `%s` ' ,
278- $ bundle ->getName (), $ finalSchema ->getBaseName ())
284+ sprintf (
285+ '%s : Please define a `package` attribute or a `namespace` attribute for schema `%s` ' ,
286+ $ bundle ? $ bundle ->getName () : 'App ' ,
287+ $ finalSchema ->getBaseName ()
288+ )
279289 );
280290 }
281291
282- $ database ['package ' ] = $ package ;
283-
284292 if ($ this ->input && $ this ->input ->hasOption ('connection ' ) && $ this ->input ->getOption ('connection ' )
285293 && $ database ['name ' ] != $ this ->input ->getOption ('connection ' )) {
286- //we skip this schema because the connection name doesn't match the input value
287- unset($ this ->tempSchemas [$ tempSchema ]);
294+ // we skip this schema because the connection name doesn't match the input values
288295 $ filesystem ->remove ($ file );
296+ $ this ->output ->writeln (sprintf (
297+ '<info>Skipped schema %s due to database name missmatch (%s not in [%s]).</info> ' ,
298+ $ finalSchema ->getPathname (),
299+ $ database ['name ' ],
300+ $ this ->input ->getOption ('connection ' )
301+ ));
289302 continue ;
290303 }
291304
292305 foreach ($ database ->table as $ table ) {
293306 if (isset ($ table ['package ' ])) {
294307 $ table ['package ' ] = $ table ['package ' ];
295- } else {
296- $ table ['package ' ] = $ package ;
308+ } elseif (isset ($ table ['namespace ' ])) {
309+ if ($ bundle ) {
310+ $ table ['package ' ] = $ this ->getPackage ($ bundle , (string )$ table ['namespace ' ]);
311+ } else {
312+ $ table ['package ' ] = $ this ->getPackageFromApp ((string )$ table ['namespace ' ]);
313+ }
314+ } elseif (isset ($ database ['package ' ])) {
315+ $ table ['package ' ] = $ database ['package ' ];
297316 }
298317 }
299318
@@ -311,41 +330,10 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)
311330 protected function getFinalSchemas (KernelInterface $ kernel , BundleInterface $ bundle = null )
312331 {
313332 if (null !== $ bundle ) {
314- return $ this ->getSchemasFromBundle ($ bundle );
315- }
316-
317- $ finalSchemas = array ();
318- foreach ($ kernel ->getBundles () as $ bundle ) {
319- $ finalSchemas = array_merge ($ finalSchemas , $ this ->getSchemasFromBundle ($ bundle ));
320- }
321-
322- return $ finalSchemas ;
323- }
324-
325- /**
326- * @param \Symfony\Component\HttpKernel\Bundle\BundleInterface $bundle
327- *
328- * @return array
329- */
330- protected function getSchemasFromBundle (BundleInterface $ bundle )
331- {
332- $ finalSchemas = array ();
333-
334- if (is_dir ($ dir = $ bundle ->getPath ().'/Resources/config ' )) {
335- $ finder = new Finder ();
336- $ schemas = $ finder ->files ()->name ('*schema.xml ' )->followLinks ()->in ($ dir );
337-
338- if (iterator_count ($ schemas )) {
339- foreach ($ schemas as $ schema ) {
340- $ logicalName = $ this ->transformToLogicalName ($ schema , $ bundle );
341- $ finalSchema = new \SplFileInfo ($ this ->getFileLocator ()->locate ($ logicalName ));
342-
343- $ finalSchemas [(string ) $ finalSchema ] = array ($ bundle , $ finalSchema );
344- }
345- }
333+ return $ this ->getSchemaLocator ()->locateFromBundle ($ bundle );
346334 }
347335
348- return $ finalSchemas ;
336+ return $ this -> getSchemaLocator ()-> locateFromBundlesAndConfiguration ( $ kernel -> getBundles ()) ;
349337 }
350338
351339 /**
@@ -471,6 +459,24 @@ protected function getProperties($file)
471459 return $ properties ;
472460 }
473461
462+ /**
463+ * @param string $namespace
464+ *
465+ * @return string
466+ */
467+ protected function getPackageFromApp (string $ namespace ): string
468+ {
469+ if ('\\' === $ namespace [0 ]) {
470+ $ namespace = substr ($ namespace , 1 );
471+ }
472+
473+ if (0 === stripos ($ namespace , 'App \\' )) {
474+ $ namespace = substr ($ namespace , 4 );
475+ }
476+
477+ return 'src. ' .str_replace ('\\' , '. ' , $ namespace );
478+ }
479+
474480 /**
475481 * Return the current Propel cache directory.
476482 * @return string The current Propel cache directory.
@@ -488,6 +494,17 @@ protected function getFileLocator()
488494 return $ this ->getContainer ()->get ('propel.file_locator ' );
489495 }
490496
497+ /**
498+ * @return SchemaLocator
499+ */
500+ protected function getSchemaLocator (): SchemaLocator
501+ {
502+ /** @var SchemaLocator $obj */
503+ $ obj = $ this ->getContainer ()->get ('propel.schema_locator ' );
504+
505+ return $ obj ;
506+ }
507+
491508 /**
492509 * Get connection by checking the input option named 'connection'.
493510 * Returns the default connection if no option specified or an exception
0 commit comments