Merge pull request #288 from opichon/1.4.x

fixed bundle package under PSR-4
This commit is contained in:
Toni Uebernickel 2014-09-29 11:14:01 +02:00
commit 2652ade31c

View file

@ -67,32 +67,50 @@ abstract class AbstractCommand extends ContainerAwareCommand
protected $input;
/**
* Return the package prefix for a given bundle.
* Return the package for a given bundle.
*
* @param Bundle $bundle
* @param string $baseDirectory The base directory to exclude from prefix.
*
* @return string
*/
protected function getPackagePrefix(Bundle $bundle, $baseDirectory = '')
protected function getPackage(Bundle $bundle, $namespace = '', $baseDirectory = '')
{
$parts = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath()));
$segments = explode('\\', $bundle->getNamespace());
$length = count($parts);
$path = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath()));
$bundle_namespace = explode('\\', $bundle->getNamespace());
$partsDiff = array_diff($segments, $parts);
if (empty($partsDiff)) {
$length = count(explode('\\', $bundle->getNamespace())) * (-1);
$diff = array_diff($bundle_namespace, $path);
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));
$prefix = ltrim(str_replace($baseDirectory, '', $prefix), DIRECTORY_SEPARATOR);
$package = ltrim(str_replace($baseDirectory, '', $package), DIRECTORY_SEPARATOR);
if (!empty($prefix)) {
$prefix = str_replace(DIRECTORY_SEPARATOR, '.', $prefix).'.';
if (!empty($package)) {
$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);
foreach ($finalSchemas as $schema) {
list($bundle, $finalSchema) = $schema;
$packagePrefix = $this->getPackagePrefix($bundle, $base);
$tempSchema = $bundle->getName().'-'.$finalSchema->getBaseName();
$this->tempSchemas[$tempSchema] = array(
@ -235,9 +252,9 @@ abstract class AbstractCommand extends ContainerAwareCommand
if (isset($database['package'])) {
// Do not use the prefix!
// This is used to override the package resulting from namespace conversion.
$database['package'] = $database['package'];
$package = $database['package'];
} elseif (isset($database['namespace'])) {
$database['package'] = $packagePrefix . str_replace('\\', '.', $database['namespace']);
$package = $this->getPackage($bundle, $database['namespace'], $base);
} else {
throw new \RuntimeException(
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')
&& $database['name'] != $this->input->getOption('connection')) {
//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) {
if (isset($table['package'])) {
$table['package'] = $table['package'];
} elseif (isset($table['namespace'])) {
$table['package'] = $packagePrefix . str_replace('\\', '.', $table['namespace']);
} else {
$table['package'] = $database['package'];
$table['package'] = $package;
}
}