Skip to content

Commit

Permalink
Add Symfony 6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
mwaterboehr committed Oct 6, 2023
1 parent 0d73dde commit fe58f09
Show file tree
Hide file tree
Showing 54 changed files with 307 additions and 449 deletions.
42 changes: 20 additions & 22 deletions Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

namespace Propel\Bundle\PropelBundle\Command;

use Propel\Bundle\PropelBundle\Service\SchemaLocator;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function getContainer(): ContainerInterface
/**
* {@inheritdoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->input = $input;
$this->output = $output;
Expand All @@ -68,7 +69,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
/**
* Create all the files needed by Propel's commands.
*/
protected function setupBuildTimeFiles()
protected function setupBuildTimeFiles(): void
{
$fs = new Filesystem();
$fs->mkdir($this->cacheDir);
Expand All @@ -85,7 +86,7 @@ protected function setupBuildTimeFiles()
* @param string $cacheDir The directory in which the schemas will
* be copied.
*/
protected function copySchemas(KernelInterface $kernel, $cacheDir)
protected function copySchemas(KernelInterface $kernel, string $cacheDir): void
{
$filesystem = new Filesystem();

Expand Down Expand Up @@ -171,7 +172,7 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)
*
* @return array A list of schemas.
*/
protected function getFinalSchemas(KernelInterface $kernel, BundleInterface $bundle = null)
protected function getFinalSchemas(KernelInterface $kernel, BundleInterface $bundle = null): array
{
if (null !== $bundle) {
return $this->getSchemaLocator()->locateFromBundle($bundle);
Expand All @@ -190,7 +191,7 @@ protected function getFinalSchemas(KernelInterface $kernel, BundleInterface $bun
*
* @return int The command return code.
*/
protected function runCommand(Command $command, array $parameters, InputInterface $input, OutputInterface $output)
protected function runCommand(Command $command, array $parameters, InputInterface $input, OutputInterface $output): int
{
// add the command's name to the parameters
array_unshift($parameters, $this->getName());
Expand Down Expand Up @@ -227,7 +228,7 @@ protected function runCommand(Command $command, array $parameters, InputInterfac
*
* @param string $file Should be 'propel.json'.
*/
protected function createPropelConfigurationFile($file)
protected function createPropelConfigurationFile(string $file): void
{
$propelConfig = $this->getContainer()->getParameter('propel.configuration');

Expand All @@ -249,7 +250,7 @@ protected function createPropelConfigurationFile($file)
*
* @return array
*/
protected function getConnections(array $connections)
protected function getConnections(array $connections): array
{
$dsnList = array();
foreach ($connections as $connection) {
Expand All @@ -266,7 +267,7 @@ protected function getConnections(array $connections)
*
* @return array The connection data.
*/
protected function getConnectionData($name)
protected function getConnectionData(string $name): array
{
$knownConnections = $this->getContainer()->getParameter('propel.configuration');
if (!isset($knownConnections['database']['connections'][$name])) {
Expand All @@ -283,7 +284,7 @@ protected function getConnectionData($name)
*
* @return string The DSN.
*/
protected function getDsn($connectionName)
protected function getDsn(string $connectionName): string
{
$connection = $this->getConnectionData($connectionName);
// Add user and password to dsn string
Expand All @@ -298,10 +299,7 @@ protected function getDsn($connectionName)
return implode(';', $dsn);
}

/**
* @return \Symfony\Component\Config\FileLocatorInterface
*/
protected function getSchemaLocator()
protected function getSchemaLocator(): SchemaLocator
{
return $this->getContainer()->get('propel.schema_locator');
}
Expand All @@ -311,7 +309,7 @@ protected function getSchemaLocator()
*
* @return string
*/
protected function getPackageFromApp($namespace)
protected function getPackageFromApp(string $namespace): string
{
if ('\\' === $namespace[0]) {
$namespace = substr($namespace, 1);
Expand All @@ -330,7 +328,7 @@ protected function getPackageFromApp($namespace)
*
* @return string
*/
protected function getPackageFromBundle(Bundle $bundle, $namespace)
protected function getPackageFromBundle(Bundle $bundle, string $namespace): string
{
//find relative path from namespace to bundle->getNamespace()
$baseNamespace = (new \ReflectionClass($bundle))->getNamespaceName();
Expand Down Expand Up @@ -365,7 +363,7 @@ protected function getPackageFromBundle(Bundle $bundle, $namespace)
*
* @return string The current Propel cache directory.
*/
protected function getCacheDir()
protected function getCacheDir(): string
{
return $this->cacheDir;
}
Expand All @@ -378,7 +376,7 @@ protected function getCacheDir()
*
* @return string relative path without trailing slash
*/
public static function getRelativePath($from, $to)
public static function getRelativePath(string $from, string $to): string
{
$from = '/' . trim($from, '/');
$to = '/' . trim($to, '/');
Expand All @@ -402,7 +400,7 @@ public static function getRelativePath($from, $to)
* @param string $dsn A DSN
* @return string The database name extracted from the given DSN
*/
protected function parseDbName($dsn)
protected function parseDbName(string $dsn): ?string
{
preg_match('#(dbname|Database)=([a-zA-Z0-9\_]+)#', $dsn, $matches);

Expand All @@ -419,7 +417,7 @@ protected function parseDbName($dsn)
*
* @return string
*/
protected function getMigrationsTable()
protected function getMigrationsTable(): string
{
$config = $this->getContainer()->getParameter('propel.configuration');

Expand All @@ -431,7 +429,7 @@ protected function getMigrationsTable()
*
* @return string
*/
protected function getDefaultConnection()
protected function getDefaultConnection(): string
{
$config = $this->getContainer()->getParameter('propel.configuration');

Expand All @@ -443,7 +441,7 @@ protected function getDefaultConnection()
*
* @return string The platform class name.
*/
protected function getPlatform()
protected function getPlatform(): ?string
{
$config = $this->getContainer()->getParameter('propel.configuration');
return $config['generator']['platformClass'];
Expand Down
6 changes: 4 additions & 2 deletions Command/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BuildCommand extends Command
/**
* {@inheritdoc}
*/
protected function configure()
protected function configure(): void
{
$this
->setName('propel:build')
Expand All @@ -44,7 +44,7 @@ protected function configure()
*
* @throws \InvalidArgumentException When the target directory does not exist
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$input->getOption('sql')) {
$in = new ArrayInput(array(
Expand Down Expand Up @@ -76,5 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$cmd = $this->getApplication()->find('propel:sql:insert');
$cmd->run($in, $output);
}

return 0;
}
}
20 changes: 5 additions & 15 deletions Command/BundleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,17 @@
*/
trait BundleTrait
{
/**
* @return ContainerInterface
*/
protected abstract function getContainer();
protected abstract function getContainer(): ContainerInterface;

/**
* Returns the selected bundle.
* If no bundle argument is set, the user will get ask for it.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return BundleInterface
*/
protected function getBundle(InputInterface $input, OutputInterface $output)
protected function getBundle(InputInterface $input, OutputInterface $output): BundleInterface
{
$kernel = $this
->getContainer()
->get('kernel');
$kernel = $this->getContainer()->get('kernel');

if ($input->hasArgument('bundle') && '@' === substr($input->getArgument('bundle'), 0, 1)) {
if ($input->hasArgument('bundle') && str_starts_with($input->getArgument('bundle'), '@')) {
return $kernel->getBundle(substr($input->getArgument('bundle'), 1));
}

Expand All @@ -62,4 +52,4 @@ protected function getBundle(InputInterface $input, OutputInterface $output)

return $kernel->getBundle($bundleName);
}
}
}
12 changes: 8 additions & 4 deletions Command/DatabaseCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Propel\Runtime\Connection\ConnectionManagerSingle;
use Propel\Runtime\Propel;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -27,7 +28,7 @@ class DatabaseCreateCommand extends AbstractCommand
/**
* @see Command
*/
protected function configure()
protected function configure(): void
{
$this
->setName('propel:database:create')
Expand All @@ -42,14 +43,15 @@ protected function configure()
*
* @throws \InvalidArgumentException When the target directory does not exist
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$connectionName = $input->getOption('connection') ?: $this->getDefaultConnection();
$config = $this->getConnectionData($connectionName);
$dbName = $this->parseDbName($config['dsn']);

if (null === $dbName) {
return $output->writeln('<error>No database name found.</error>');
$output->writeln('<error>No database name found.</error>');
return 1;
} else {
$query = 'CREATE DATABASE '. $dbName .';';
}
Expand All @@ -67,6 +69,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$statement->execute();

$output->writeln(sprintf('<info>Database <comment>%s</comment> has been created.</info>', $dbName));

return 0;
}

/**
Expand All @@ -78,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
* @param array $config A Propel connection configuration.
* @return array
*/
private function getTemporaryConfiguration($config)
private function getTemporaryConfiguration(array $config): array
{
$dbName = $this->parseDbName($config['dsn']);

Expand Down
16 changes: 10 additions & 6 deletions Command/DatabaseDropCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Propel\Bundle\PropelBundle\Command;

use Propel\Runtime\Propel;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -27,7 +28,7 @@ class DatabaseDropCommand extends AbstractCommand
/**
* {@inheritdoc}
*/
protected function configure()
protected function configure(): void
{
$this
->setName('propel:database:drop')
Expand All @@ -51,12 +52,12 @@ protected function configure()
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$input->getOption('force')) {
$output->writeln('<error>You have to use the "--force" option to drop the database.</error>');

return;
return 1;
}

if ('prod' === $this->getKernel()->getEnvironment()) {
Expand All @@ -65,7 +66,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (false === $this->askConfirmation($input, $output, 'Are you sure ? (y/n) ', false)) {
$output->writeln('Aborted, nice decision !');

return -2;
return 1;
}
}

Expand All @@ -74,7 +75,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$dbName = $this->parseDbName($config['dsn']);

if (null === $dbName) {
return $output->writeln('<error>No database name found.</error>');
$output->writeln('<error>No database name found.</error>');
return 1;
} else {
$query = 'DROP DATABASE '. $dbName .';';
}
Expand All @@ -93,6 +95,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$statement->execute();

$output->writeln(sprintf('<info>Database <comment>%s</comment> has been dropped.</info>', $dbName));

return 0;
}

/**
Expand All @@ -104,7 +108,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
* @param array $config A Propel connection configuration.
* @return array
*/
private function getTemporaryConfiguration($config)
private function getTemporaryConfiguration(array $config): array
{
$dbName = $this->parseDbName($config['dsn']);

Expand Down
Loading

0 comments on commit fe58f09

Please sign in to comment.