support bundle-less schema file (#458)

This commit is contained in:
Gregor Harlan 2017-08-31 12:27:56 +02:00 committed by Marc J. Schmidt
parent 59f3b4a738
commit d5f4435ce0
3 changed files with 57 additions and 18 deletions

View file

@ -45,8 +45,6 @@ abstract class AbstractCommand extends ContainerAwareCommand
*/ */
protected $output; protected $output;
protected $tempSchemas = [];
use FormattingHelpers; use FormattingHelpers;
/** /**
@ -96,17 +94,15 @@ abstract class AbstractCommand extends ContainerAwareCommand
$finalSchemas = $this->getFinalSchemas($kernel, $this->bundle); $finalSchemas = $this->getFinalSchemas($kernel, $this->bundle);
foreach ($finalSchemas as $schema) { foreach ($finalSchemas as $schema) {
/** @var Bundle $bundle */ /** @var null|Bundle $bundle */
list($bundle, $finalSchema) = $schema; list($bundle, $finalSchema) = $schema;
$tempSchema = $bundle->getName().'-'.$finalSchema->getBaseName(); if ($bundle) {
$this->tempSchemas[$tempSchema] = array( $file = $cacheDir.DIRECTORY_SEPARATOR.'bundle-'.$bundle->getName().'-'.$finalSchema->getBaseName();
'bundle' => $bundle->getName(), } else {
'basename' => $finalSchema->getBaseName(), $file = $cacheDir.DIRECTORY_SEPARATOR.'app-'.$finalSchema->getBaseName();
'path' => $finalSchema->getPathname(), }
);
$file = $cacheDir.DIRECTORY_SEPARATOR.$tempSchema;
$filesystem->copy((string) $finalSchema, $file, true); $filesystem->copy((string) $finalSchema, $file, true);
// the package needs to be set absolute // the package needs to be set absolute
@ -119,12 +115,18 @@ abstract class AbstractCommand extends ContainerAwareCommand
// This is used to override the package resulting from namespace conversion. // This is used to override the package resulting from namespace conversion.
$database['package'] = $database['package']; $database['package'] = $database['package'];
} elseif (isset($database['namespace'])) { } elseif (isset($database['namespace'])) {
if ($bundle) {
$database['package'] = $this->getPackageFromBundle($bundle, (string)$database['namespace']); $database['package'] = $this->getPackageFromBundle($bundle, (string)$database['namespace']);
} else {
$database['package'] = $this->getPackageFromApp((string)$database['namespace']);
}
} else { } else {
throw new \RuntimeException( throw new \RuntimeException(
sprintf('%s : Please define a `package` attribute or a `namespace` attribute for schema `%s`', sprintf(
$bundle->getName(), $finalSchema->getBaseName()) '%s : Please define a `package` attribute or a `namespace` attribute for schema `%s`',
$bundle ? $bundle->getName() : 'App',
$finalSchema->getBaseName()
)
); );
} }
@ -134,7 +136,6 @@ abstract class AbstractCommand extends ContainerAwareCommand
if (!in_array((string) $database['name'], $connections)) { if (!in_array((string) $database['name'], $connections)) {
// we skip this schema because the connection name doesn't // we skip this schema because the connection name doesn't
// match the input values // match the input values
unset($this->tempSchemas[$tempSchema]);
$filesystem->remove($file); $filesystem->remove($file);
$this->output->writeln(sprintf( $this->output->writeln(sprintf(
'<info>Skipped schema %s due to database name missmatch (%s not in [%s]).</info>', '<info>Skipped schema %s due to database name missmatch (%s not in [%s]).</info>',
@ -150,7 +151,11 @@ abstract class AbstractCommand extends ContainerAwareCommand
if (isset($table['package'])) { if (isset($table['package'])) {
$table['package'] = $table['package']; $table['package'] = $table['package'];
} elseif (isset($table['namespace'])) { } elseif (isset($table['namespace'])) {
$table['package'] = $this->getPackageFromBundle($bundle, (string)$table['namespace']); if ($bundle) {
$table['package'] = $this->getPackageFromBundle($bundle, (string)$table['namespace']);
} else {
$table['package'] = $this->getPackageFromApp((string)$table['namespace']);
}
} else { } else {
$table['package'] = $database['package']; $table['package'] = $database['package'];
} }
@ -175,7 +180,7 @@ abstract class AbstractCommand extends ContainerAwareCommand
return $this->getSchemaLocator()->locateFromBundle($bundle); return $this->getSchemaLocator()->locateFromBundle($bundle);
} }
return $this->getSchemaLocator()->locateFromBundles($kernel->getBundles()); return $this->getSchemaLocator()->locateFromBundlesAndConfiguration($kernel->getBundles());
} }
/** /**
@ -304,6 +309,24 @@ abstract class AbstractCommand extends ContainerAwareCommand
return $this->getContainer()->get('propel.schema_locator'); return $this->getContainer()->get('propel.schema_locator');
} }
/**
* @param string $namespace
*
* @return string
*/
protected function getPackageFromApp($namespace)
{
if ('\\' === $namespace[0]) {
$namespace = substr($namespace, 1);
}
if (0 === stripos($namespace, 'App\\')) {
$namespace = substr($namespace, 4);
}
return 'src.'.str_replace('\\', '.', $namespace);
}
/** /**
* @param Bundle $bundle * @param Bundle $bundle
* @param string $namespace * @param string $namespace

View file

@ -21,6 +21,7 @@
<services> <services>
<service id="propel.schema_locator" class="%propel.schema_locator.class%"> <service id="propel.schema_locator" class="%propel.schema_locator.class%">
<argument type="service" id="file_locator" /> <argument type="service" id="file_locator" />
<argument>%propel.configuration%</argument>
</service> </service>
<service id="propel.logger" class="%propel.logger.class%"> <service id="propel.logger" class="%propel.logger.class%">

View file

@ -17,10 +17,25 @@ use Symfony\Component\HttpKernel\Bundle\BundleInterface;
class SchemaLocator class SchemaLocator
{ {
protected $fileLocator; protected $fileLocator;
protected $configuration;
public function __construct(FileLocatorInterface $fileLocator) public function __construct(FileLocatorInterface $fileLocator, array $configuration)
{ {
$this->fileLocator = $fileLocator; $this->fileLocator = $fileLocator;
$this->configuration = $configuration;
}
public function locateFromBundlesAndConfiguration(array $bundles)
{
$schemas = $this->locateFromBundles($bundles);
$path = $this->configuration['paths']['schemaDir'].'/schema.xml';
if (file_exists($path)) {
$schema = new \SplFileInfo($path);
$schemas[(string) $schema] = array(null, $schema);
}
return $schemas;
} }
public function locateFromBundles(array $bundles) public function locateFromBundles(array $bundles)