diff --git a/src/Propel/Common/Config/PropelConfiguration.php b/src/Propel/Common/Config/PropelConfiguration.php index 48d84b4f76..9ce0ddedf9 100644 --- a/src/Propel/Common/Config/PropelConfiguration.php +++ b/src/Propel/Common/Config/PropelConfiguration.php @@ -241,6 +241,7 @@ protected function addReverseSection(ArrayNodeDefinition $node) ->arrayNode('reverse') ->children() ->scalarNode('connection')->end() + ->scalarNode('namespace')->defaultNull()->end() ->scalarNode('parserClass')->end() ->end() ->end() //reverse diff --git a/src/Propel/Generator/Command/DatabaseReverseCommand.php b/src/Propel/Generator/Command/DatabaseReverseCommand.php index dadc2ac196..37a64c65e5 100644 --- a/src/Propel/Generator/Command/DatabaseReverseCommand.php +++ b/src/Propel/Generator/Command/DatabaseReverseCommand.php @@ -40,7 +40,7 @@ protected function configure() 'connection', InputArgument::OPTIONAL, 'Connection name or dsn to use. Example: \'mysql:host=127.0.0.1;dbname=test;user=root;password=foobar\' (don\'t forget the quote for dsn)', - 'default' + NULL ) ->setName('database:reverse') ->setAliases(['reverse']) @@ -54,14 +54,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int { $configOptions = []; - $connection = (string)$input->getArgument('connection'); - if (strpos($connection, ':') === false) { + $connection = (string) $input->getArgument('connection'); + if ($connection !== NULL && false === strpos($connection, ':')) { //treat it as connection name $configOptions['propel']['reverse']['connection'] = $connection; if (!$input->getOption('database-name')) { $input->setOption('database-name', $connection); } - } else { + } else if ($connection !== NULL) { //probably a dsn $configOptions += $this->connectionToProperties('reverseconnection=' . $connection, 'reverse'); $configOptions['propel']['reverse']['parserClass'] = sprintf( @@ -73,9 +73,46 @@ protected function execute(InputInterface $input, OutputInterface $output): int $input->setOption('database-name', self::DEFAULT_DATABASE_NAME); } } + + if ($input->getOption('namespace')) { + $configOptions['propel']['reverse']['namespace'] = $input->getOption('namespace'); + } + if ($input->getOption('output-dir') !== self::DEFAULT_OUTPUT_DIRECTORY) { + $configOptions['propel']['paths']['schemaDir'] = $input->getOption('output-dir'); + } + + if ($input->getOption('schema-name') !== self::DEFAULT_SCHEMA_NAME) { + $configOptions['propel']['generator']['schema']['basename'] = $input->getOption('schema-name'); + } + $generatorConfig = $this->getGeneratorConfig($configOptions, $input); - $this->createDirectory($input->getOption('output-dir')); + $outputDir = $generatorConfig->getConfigProperty('paths.schemaDir'); + $shemaName = $generatorConfig->getConfigProperty('generator.schema.basename'); + if (empty($outputDir) || empty($shemaName)) { + // if output dir or shema name is still empty, we have to set them + // to his default values, in order to keep the previous + // behaviour of the script + if (empty($outputDir)) { + $configOptions['propel']['paths']['schemaDir'] = $input->getOption('output-dir'); + } + if (empty($shemaName)) { + $configOptions['propel']['generator']['schema']['basename'] = $input->getOption('schema-name'); + } + + // regenerate config with the new options + $generatorConfig = $this->getGeneratorConfig($configOptions, $input);exit(1); + } + + if (!$connection) { + $connection = $generatorConfig->getConfigProperty('reverse.connection'); + if ($connection && !$input->getOption('database-name')) { + $input->setOption('database-name', $connection); + } + } + + $this->createDirectory( + $generatorConfig->getConfigProperty('paths.schemaDir')); $manager = new ReverseManager(new XmlDumper()); $manager->setGeneratorConfig($generatorConfig); @@ -84,9 +121,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeln($message); } }); - $manager->setWorkingDirectory($input->getOption('output-dir')); + $manager->setWorkingDirectory( + $generatorConfig->getConfigProperty('paths.schemaDir')); $manager->setDatabaseName($input->getOption('database-name')); - $manager->setSchemaName($input->getOption('schema-name')); + $manager->setSchemaName( + $generatorConfig->getConfigProperty('generator.schema.basename')); + + $namespace = $generatorConfig->getConfigProperty('reverse.namespace'); $namespace = $input->getOption('namespace'); diff --git a/tests/Fixtures/bookstore/propel.yaml.dist b/tests/Fixtures/bookstore/propel.yaml.dist index 5c371f8445..b24e0b003b 100644 --- a/tests/Fixtures/bookstore/propel.yaml.dist +++ b/tests/Fixtures/bookstore/propel.yaml.dist @@ -3,6 +3,9 @@ propel: general: project: bookstore + paths: + schemaDir: ../../reversecommand + database: connections: bookstore: @@ -45,6 +48,10 @@ propel: settings: charset: utf8 + reverse: + connection: bookstore + namespace: \ReverseVendor\ReversePackageWithConfDir + generator: defaultConnection: bookstore connections: diff --git a/tests/Propel/Tests/Generator/Command/DatabaseReverseTest.php b/tests/Propel/Tests/Generator/Command/DatabaseReverseTest.php index d6f0b6d26a..2ea69a89e1 100644 --- a/tests/Propel/Tests/Generator/Command/DatabaseReverseTest.php +++ b/tests/Propel/Tests/Generator/Command/DatabaseReverseTest.php @@ -21,6 +21,19 @@ */ class DatabaseReverseTest extends TestCaseFixturesDatabase { + protected $configDir; + protected $outputDir; + + /** + * @return void + */ + public function setUp() + { + parent::setUp(); + $this->configDir = __DIR__ . '/../../../../Fixtures/bookstore'; + $this->outputDir = __DIR__ . '/../../../../reversecommand'; + } + /** * @return void */ @@ -31,14 +44,13 @@ public function testCommandWithoutNamespace() $app->add($command); $currentDir = getcwd(); - $outputDir = __DIR__ . '/../../../../reversecommand'; chdir(__DIR__ . '/../../../../Fixtures/bookstore'); $input = new ArrayInput([ 'command' => 'database:reverse', '--database-name' => 'reverse-test', - '--output-dir' => $outputDir, + '--output-dir' => $this->outputDir, '--verbose' => true, '--platform' => ucfirst($this->getDriver()) . 'Platform', 'connection' => $this->getConnectionDsn('bookstore-schemas', true), @@ -56,7 +68,7 @@ public function testCommandWithoutNamespace() } $this->assertSame(AbstractCommand::CODE_SUCCESS, $result, 'database:reverse tests exited successfully'); - $databaseXml = simplexml_load_file($outputDir . '/schema.xml'); + $databaseXml = simplexml_load_file($this->outputDir . '/schema.xml'); $this->assertEquals('reverse-test', $databaseXml['name']); $this->assertGreaterThan(20, $databaseXml->xpath('table')); @@ -79,7 +91,6 @@ public function testCommandWithNamespace() $app->add($command); $currentDir = getcwd(); - $outputDir = __DIR__ . '/../../../../reversecommand'; $testNamespace = '\ReverseVendor\ReversePackage'; chdir(__DIR__ . '/../../../../Fixtures/bookstore'); @@ -87,7 +98,7 @@ public function testCommandWithNamespace() $input = new ArrayInput([ 'command' => 'database:reverse', '--database-name' => 'reverse-test', - '--output-dir' => $outputDir, + '--output-dir' => $this->outputDir, '--verbose' => true, '--platform' => ucfirst($this->getDriver()) . 'Platform', '--namespace' => $testNamespace, @@ -106,7 +117,79 @@ public function testCommandWithNamespace() } $this->assertEquals(0, $result, 'database:reverse tests exited successfully'); - $databaseXml = simplexml_load_file($outputDir . '/schema.xml'); + $databaseXml = simplexml_load_file($this->outputDir . '/schema.xml'); + $this->assertEquals($testNamespace, $databaseXml['namespace']); + } + + public function testCommandWithConfigDirAndAllParams() + { + $app = new Application('Propel', Propel::VERSION); + $command = new DatabaseReverseCommand(); + $app->add($command); + + $currentDir = getcwd(); + $testNamespace = '\ReverseVendor\ReversePackage'; + + chdir(__DIR__ . '/../../../../Fixtures/bookstore'); + + $input = new \Symfony\Component\Console\Input\ArrayInput([ + 'command' => 'database:reverse', + '--config-dir' => $this->configDir, + '--database-name' => 'reverse-test', + '--output-dir' => $this->outputDir, + '--verbose' => true, + '--platform' => ucfirst($this->getDriver()) . 'Platform', + '--namespace' => $testNamespace, + 'connection' => $this->getConnectionDsn('bookstore-schemas', true) + ]); + + $output = new \Symfony\Component\Console\Output\StreamOutput(fopen("php://temp", 'r+')); + $app->setAutoExit(false); + $result = $app->run($input, $output); + + chdir($currentDir); + + if (0 !== $result) { + rewind($output->getStream()); + echo stream_get_contents($output->getStream()); + } + $this->assertEquals(0, $result, 'database:reverse tests exited successfully'); + + $databaseXml = simplexml_load_file($this->outputDir . '/schema.xml'); + $this->assertEquals($testNamespace, $databaseXml['namespace']); + } + + public function testCommandWithConfigDirAndNoParams() + { + $app = new Application('Propel', Propel::VERSION); + $command = new DatabaseReverseCommand(); + $app->add($command); + + $currentDir = getcwd(); + $testNamespace = '\ReverseVendor\ReversePackageWithConfDir'; + + chdir(__DIR__ . '/../../../../Fixtures/bookstore'); + + $input = new \Symfony\Component\Console\Input\ArrayInput([ + 'command' => 'database:reverse', + '--config-dir' => $this->configDir, + '--verbose' => true, + '--platform' => ucfirst($this->getDriver()) . 'Platform' + ]); + + $output = new \Symfony\Component\Console\Output\StreamOutput(fopen("php://temp", 'r+')); + $app->setAutoExit(false); + $result = $app->run($input, $output); + + chdir($currentDir); + + if (0 !== $result) { + rewind($output->getStream()); + echo stream_get_contents($output->getStream()); + } + $this->assertEquals(0, $result, 'database:reverse tests exited successfully'); + + $databaseXml = simplexml_load_file($this->outputDir . '/schema.xml'); $this->assertEquals($testNamespace, $databaseXml['namespace']); } }