Skip to content

Commit efe7749

Browse files
committed
Allows for schema to be specified in application, rather than bundle.
1 parent cdbe840 commit efe7749

3 files changed

Lines changed: 72 additions & 8 deletions

File tree

Command/AbstractCommand.php

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Finder\Finder;
1717
use Symfony\Component\HttpKernel\Bundle\Bundle;
1818
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
19+
use Symfony\Component\HttpKernel\Kernel;
1920
use Symfony\Component\HttpKernel\KernelInterface;
2021

2122
/**
@@ -65,6 +66,24 @@ abstract class AbstractCommand extends ContainerAwareCommand
6566
*/
6667
protected $input;
6768

69+
/**
70+
* @param string $namespace
71+
*
72+
* @return string
73+
*/
74+
protected function getPackageFromApp($namespace)
75+
{
76+
if ('\\' === $namespace[0]) {
77+
$namespace = substr($namespace, 1);
78+
}
79+
80+
if (0 === stripos($namespace, 'App\\')) {
81+
$namespace = substr($namespace, 4);
82+
}
83+
84+
return 'src.'.str_replace('\\', '.', $namespace);
85+
}
86+
6887
/**
6988
* Return the package for a given bundle.
7089
*
@@ -235,9 +254,14 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)
235254
foreach ($finalSchemas as $schema) {
236255
list($bundle, $finalSchema) = $schema;
237256

238-
$tempSchema = $bundle->getName().'-'.$finalSchema->getBaseName();
257+
if ($bundle) {
258+
$tempSchema = $bundle->getName().'-'.$finalSchema->getBaseName();
259+
} else {
260+
$tempSchema = 'app-' . $finalSchema->getBaseName();
261+
}
262+
239263
$this->tempSchemas[$tempSchema] = array(
240-
'bundle' => $bundle->getName(),
264+
'bundle' => ($bundle) ? $bundle->getName() : 'app',
241265
'basename' => $finalSchema->getBaseName(),
242266
'path' => $finalSchema->getPathname(),
243267
);
@@ -255,16 +279,18 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)
255279
// This is used to override the package resulting from namespace conversion.
256280
$package = $database['package'];
257281
} elseif (isset($database['namespace'])) {
258-
$package = $this->getPackage($bundle, $database['namespace'], $base);
282+
if ($bundle) {
283+
$database['package'] = $this->getPackage($bundle, (string)$database['namespace'], $base);
284+
} else {
285+
$database['package'] = $this->getPackageFromApp((string)$database['namespace']);
286+
}
259287
} else {
260288
throw new \RuntimeException(
261289
sprintf('%s : Please define a `package` attribute or a `namespace` attribute for schema `%s`',
262290
$bundle->getName(), $finalSchema->getBaseName())
263291
);
264292
}
265293

266-
$database['package'] = $package;
267-
268294
if ($this->input && $this->input->hasOption('connection') && $this->input->getOption('connection')
269295
&& $database['name'] != $this->input->getOption('connection')) {
270296
//we skip this schema because the connection name doesn't match the input value
@@ -277,7 +303,7 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)
277303
if (isset($table['package'])) {
278304
$table['package'] = $table['package'];
279305
} else {
280-
$table['package'] = $package;
306+
$table['package'] = $database['package'];
281307
}
282308
}
283309

@@ -303,9 +329,26 @@ protected function getFinalSchemas(KernelInterface $kernel, BundleInterface $bun
303329
$finalSchemas = array_merge($finalSchemas, $this->getSchemasFromBundle($bundle));
304330
}
305331

332+
$finalSchemas = array_merge($finalSchemas, $this->getSchemasFromConfiguration());
333+
306334
return $finalSchemas;
307335
}
308336

337+
protected function getSchemasFromConfiguration()
338+
{
339+
$schemas = [];
340+
if (is_dir($dir = $this->getContainer()->getParameter('propel.schema.path')))
341+
{
342+
$path = $dir . DIRECTORY_SEPARATOR . 'schema.xml';
343+
if (is_file($path)) {
344+
$schema = new \SplFileInfo($path);
345+
$schemas[(string) $schema] = array(null, $schema);
346+
}
347+
}
348+
349+
return $schemas;
350+
}
351+
309352
/**
310353
* @param \Symfony\Component\HttpKernel\Bundle\BundleInterface $bundle
311354
*
@@ -462,6 +505,11 @@ protected function getCacheDir()
462505
return $this->cacheDir;
463506
}
464507

508+
protected function getProjectDir()
509+
{
510+
return $this->getContainer()->getParameter('kernel.project_dir');
511+
}
512+
465513
/**
466514
* @return \Symfony\Component\Config\FileLocatorInterface
467515
*/
@@ -647,12 +695,22 @@ private function getPhingArguments(KernelInterface $kernel, $workingDirectory, $
647695
{
648696
$args = array();
649697

698+
$projectDir = $this->getProjectDir();
699+
if ($kernel instanceof Kernel)
700+
{
701+
$output = ($kernel::MAJOR_VERSION > 3) ? $projectDir.'/var/propel' : $projectDir.'/app/propel';
702+
}
703+
else
704+
{
705+
$output = is_dir($projectDir.'/var') ? $projectDir.'/var/propel' : $projectDir.'/app/propel';
706+
}
707+
650708
// Default properties
651709
$properties = array_merge(array(
652710
'propel.database' => 'mysql',
653711
'project.dir' => $workingDirectory,
654-
'propel.output.dir' => $kernel->getProjectDir().'/propel',
655-
'propel.php.dir' => $kernel->getProjectDir(),
712+
'propel.output.dir' => $output,
713+
'propel.php.dir' => $projectDir,
656714
'propel.packageObjectModel' => true,
657715
'propel.useDateTimeClass' => true,
658716
'propel.dateTimeClass' => 'DateTime',

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ private function addGeneralSection(ArrayNodeDefinition $node)
7070
$node
7171
->children()
7272
->scalarNode('path')->end()
73+
->scalarNode('schema_path')->end()
7374
->scalarNode('phing_path')->end()
7475
->scalarNode('logging')->defaultValue($this->debug)->end()
7576
->arrayNode('build_properties')

DependencyInjection/PropelExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public function load(array $configs, ContainerBuilder $container)
6262

6363
$container->setParameter('propel.logging', $logging);
6464

65+
if (!empty($config['schema_path']))
66+
{
67+
$container->setParameter('propel.schema.path', $config['schema_path']);
68+
}
69+
6570
// Load services
6671
if (!$container->hasDefinition('propel')) {
6772
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

0 commit comments

Comments
 (0)