fixed bundle package under PSR-4

Fixed package attribute on tables for PSR-4

fixed unsetting temp schemas
This commit is contained in:
Olivier Pichon 2014-09-27 12:22:01 +07:00
parent ba23a366ff
commit 78cf831795

View file

@ -67,32 +67,50 @@ abstract class AbstractCommand extends ContainerAwareCommand
protected $input; protected $input;
/** /**
* Return the package prefix for a given bundle. * Return the package for a given bundle.
* *
* @param Bundle $bundle * @param Bundle $bundle
* @param string $baseDirectory The base directory to exclude from prefix. * @param string $baseDirectory The base directory to exclude from prefix.
* *
* @return string * @return string
*/ */
protected function getPackagePrefix(Bundle $bundle, $baseDirectory = '') protected function getPackage(Bundle $bundle, $namespace = '', $baseDirectory = '')
{ {
$parts = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath())); $path = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath()));
$segments = explode('\\', $bundle->getNamespace()); $bundle_namespace = explode('\\', $bundle->getNamespace());
$length = count($parts);
$partsDiff = array_diff($segments, $parts); $diff = array_diff($bundle_namespace, $path);
if (empty($partsDiff)) {
$length = count(explode('\\', $bundle->getNamespace())) * (-1); if (empty($diff)) {
// PSR-0
$length = count($bundle_namespace) * (-1);
$package = implode(
DIRECTORY_SEPARATOR,
array_merge(
array_slice($path, 0, $length),
explode('\\', $namespace)
)
);
} else {
// PSR-4
$ns = explode('\\', $namespace);
$diff = array_diff($ns, $bundle_namespace);
$package = implode(
DIRECTORY_SEPARATOR,
array_merge($path, $diff)
);
} }
$prefix = implode(DIRECTORY_SEPARATOR, array_slice($parts, 0, $length)); $package = ltrim(str_replace($baseDirectory, '', $package), DIRECTORY_SEPARATOR);
$prefix = ltrim(str_replace($baseDirectory, '', $prefix), DIRECTORY_SEPARATOR);
if (!empty($prefix)) { if (!empty($package)) {
$prefix = str_replace(DIRECTORY_SEPARATOR, '.', $prefix).'.'; $package = str_replace(DIRECTORY_SEPARATOR, '.', $package).'.';
} }
return $prefix; return $package;
} }
/** /**
@ -215,7 +233,6 @@ 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) {
list($bundle, $finalSchema) = $schema; list($bundle, $finalSchema) = $schema;
$packagePrefix = $this->getPackagePrefix($bundle, $base);
$tempSchema = $bundle->getName().'-'.$finalSchema->getBaseName(); $tempSchema = $bundle->getName().'-'.$finalSchema->getBaseName();
$this->tempSchemas[$tempSchema] = array( $this->tempSchemas[$tempSchema] = array(
@ -235,9 +252,9 @@ abstract class AbstractCommand extends ContainerAwareCommand
if (isset($database['package'])) { if (isset($database['package'])) {
// Do not use the prefix! // Do not use the prefix!
// 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']; $package = $database['package'];
} elseif (isset($database['namespace'])) { } elseif (isset($database['namespace'])) {
$database['package'] = $packagePrefix . str_replace('\\', '.', $database['namespace']); $package = $this->getPackage($bundle, $database['namespace'], $base);
} else { } else {
throw new \RuntimeException( throw new \RuntimeException(
sprintf('%s : Please define a `package` attribute or a `namespace` attribute for schema `%s`', sprintf('%s : Please define a `package` attribute or a `namespace` attribute for schema `%s`',
@ -245,6 +262,8 @@ abstract class AbstractCommand extends ContainerAwareCommand
); );
} }
$database['package'] = $package;
if ($this->input && $this->input->hasOption('connection') && $this->input->getOption('connection') if ($this->input && $this->input->hasOption('connection') && $this->input->getOption('connection')
&& $database['name'] != $this->input->getOption('connection')) { && $database['name'] != $this->input->getOption('connection')) {
//we skip this schema because the connection name doesn't match the input value //we skip this schema because the connection name doesn't match the input value
@ -256,10 +275,8 @@ abstract class AbstractCommand extends ContainerAwareCommand
foreach ($database->table as $table) { foreach ($database->table as $table) {
if (isset($table['package'])) { if (isset($table['package'])) {
$table['package'] = $table['package']; $table['package'] = $table['package'];
} elseif (isset($table['namespace'])) {
$table['package'] = $packagePrefix . str_replace('\\', '.', $table['namespace']);
} else { } else {
$table['package'] = $database['package']; $table['package'] = $package;
} }
} }