Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 44 additions & 13 deletions Classes/Configuration/PhinxConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
namespace Pagemachine\Phinx\Configuration;

use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;

final class PhinxConfiguration
{
Expand All @@ -12,7 +15,8 @@ final class PhinxConfiguration
public function toArray(): array
{
$extensionsPath = Environment::getExtensionsPath();
$connectionParameters = $this->getConnectionParameters();
$connection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable(self::MIGRATION_TABLE_NAME);

return [
'paths' => [
Expand All @@ -28,22 +32,49 @@ public function toArray(): array
'environments' => [
'default_migration_table' => self::MIGRATION_TABLE_NAME,
'default_environment' => 'typo3',
'typo3' => [
'adapter' => 'mysql',
'host' => $connectionParameters['host'],
'port' => $connectionParameters['port'],
'user' => $connectionParameters['user'],
'pass' => $connectionParameters['password'],
'name' => $connectionParameters['dbname'],
'unix_socket' => $connectionParameters['unix_socket'] ?? null,
'charset' => $connectionParameters['charset'],
],
'typo3' => $this->getEnvironment($connection),
],
];
}

private function getConnectionParameters(): array
private function getEnvironment(Connection $connection): array
{
return $GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default'];
switch ($connection->getDatabasePlatform()->getName()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we could put this directly in $adapter instead.

Also what's the reason from using a connection instead of the raw TYPO3 config?

case 'sqlite':
$pathInfo = pathinfo($connection->getDatabase());
$name = $pathInfo['dirname'] . '/' . $pathInfo['filename'];
$suffix = '.' . $pathInfo['extension'];
Comment on lines +44 to +46
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be a lot of magic / hidden knowledge going on here. Is there no Doctrine DBAL API for this?


return [
'adapter' => 'sqlite',
'name' => $name,
'suffix' => $suffix,
];
case 'mssql':
$adapter = 'sqlsrv';
break;

case 'postgresql':
$adapter = 'pgsql';
break;

case 'mysql':
$adapter = 'mysql';
break;

default:
return [];
}

return [
'adapter' => $adapter,
'host' => $connection->getParams()['host'] ?? null,
Copy link
Copy Markdown
Member

@mbrodala mbrodala Mar 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd at least call getParams() only once. Also it seems like we can basically return that $params then.

Finally getParams() is marked as @internal so we should not rely on it.

'port' => $connection->getParams()['port'] ?? null,
'user' => $connection->getParams()['user'] ?? null,
'pass' => $connection->getParams()['password'] ?? null,
'name' => $connection->getDatabase(),
'unix_socket' => $connection->getParams()['unix_socket'] ?? null,
'charset' => $connection->getParams()['charset'] ?? null,
];
}
}