Skip to content

Commit

Permalink
fix resulting package names to not include folder
Browse files Browse the repository at this point in the history
  • Loading branch information
havvg committed Nov 25, 2011
1 parent 1ee6477 commit beab50c
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions Command/AbstractPropelCommand.php
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\Util\Filesystem;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Wrapper for Propel commands.
Expand Down Expand Up @@ -149,7 +150,7 @@ private function getPhingArguments(KernelInterface $kernel, $workingDirectory, $
'propel.database' => 'mysql',
'project.dir' => $workingDirectory,
'propel.output.dir' => $kernel->getRootDir().'/propel',
'propel.php.dir' => '/',
'propel.php.dir' => $kernel->getRootDir().'/..',
'propel.packageObjectModel' => true,
), $properties);

Expand Down Expand Up @@ -181,14 +182,18 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)
$filesystem->mkdir($cacheDir);
}

$base = ltrim(realpath($kernel->getRootDir().'/..'), DIRECTORY_SEPARATOR);

foreach ($kernel->getBundles() as $bundle) {
if (is_dir($dir = $bundle->getPath().'/Resources/config')) {
$finder = new Finder();
$schemas = $finder->files()->name('*schema.xml')->followLinks()->in($dir);

$parts = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath()));
$length = count(explode('\\', $bundle->getNamespace())) * (-1);
$prefix = implode('/', array_slice($parts, 1, $length));
if (empty($schemas)) {
continue;
}

$packagePrefix = self::getPackagePrefix($bundle, $base);

foreach ($schemas as $schema) {
$tempSchema = $bundle->getName().'-'.$schema->getBaseName();
Expand All @@ -207,9 +212,11 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)
$database = simplexml_load_file($file);

if (isset($database['package'])) {
$database['package'] = $prefix . '/' . $database['package'];
// Do not use the prefix!
// This is used to override the package resulting from namespace conversion.
$database['package'] = $database['package'];
} elseif (isset($database['namespace'])) {
$database['package'] = $prefix . '/' . str_replace('\\', '/', $database['namespace']);
$database['package'] = $packagePrefix . str_replace('\\', '.', $database['namespace']);
} else {
throw new \RuntimeException(
sprintf('%s : Please define a `package` attribute or a `namespace` attribute for schema `%s`',
Expand All @@ -219,9 +226,9 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)

foreach ($database->table as $table) {
if (isset($table['package'])) {
$table['package'] = $prefix . '/' . $table['package'];
$table['package'] = $table['package'];
} elseif (isset($table['namespace'])) {
$table['package'] = $prefix . '/' . str_replace('\\', '/', $table['namespace']);
$table['package'] = $packagePrefix . str_replace('\\', '.', $table['namespace']);
} else {
$table['package'] = $database['package'];
}
Expand All @@ -233,6 +240,29 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)
}
}

/**
* Return the package prefix for a given bundle.
*
* @param Bundle $bundle
* @param string $baseDirectory The base directory to exclude from prefix.
*
* @return string
*/
public static function getPackagePrefix(Bundle $bundle, $baseDirectory = '')
{
$parts = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath()));
$length = count(explode('\\', $bundle->getNamespace())) * (-1);

$prefix = implode(DIRECTORY_SEPARATOR, array_slice($parts, 1, $length));
$prefix = ltrim(str_replace($baseDirectory, '', $prefix), DIRECTORY_SEPARATOR);

if (!empty($prefix)) {
$prefix = str_replace(DIRECTORY_SEPARATOR, '.', $prefix).'.';
}

return $prefix;
}

/**
* Create a 'build.properties' file.
*
Expand Down

0 comments on commit beab50c

Please sign in to comment.