From 78cf831795a62dff58019745c3f0a8348d7042bb Mon Sep 17 00:00:00 2001 From: Olivier Pichon Date: Sat, 27 Sep 2014 12:22:01 +0700 Subject: [PATCH] fixed bundle package under PSR-4 Fixed package attribute on tables for PSR-4 fixed unsetting temp schemas --- Command/AbstractCommand.php | 55 ++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/Command/AbstractCommand.php b/Command/AbstractCommand.php index e63474a..942b554 100644 --- a/Command/AbstractCommand.php +++ b/Command/AbstractCommand.php @@ -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; } }