Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #14 from hboomsma/feature/php7
Browse files Browse the repository at this point in the history
prepare for removal of mysql extension (PHP7)
  • Loading branch information
nicoschoenmaker committed Jan 27, 2016
2 parents c1ced43 + 1bde6cc commit d3abf5a
Showing 1 changed file with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private function createSymfony11Output(array $connections)
$output = '';
foreach ($connections as $name => $connection) {
$config = array(
'dsn' => $this->formatDSN($connection),
'dsn' => $this->format11DSN($connection),
'name' => $name
);

Expand All @@ -104,7 +104,7 @@ private function createSymfony14Output($debug, array $connections)

$dsn = sprintf(
'%s:dbname=%s;host=%s;port=%s',
$this->getPropelDriverName($connection->getDriver()),
$this->getPropel14DriverName($connection->getDriver()),
$connection->getDatabase(),
$connection->getHost(),
$connection->getPort()
Expand Down Expand Up @@ -148,9 +148,9 @@ private function getPropelClass()
* @todo make the mysql bit dynamic
* @param Connection $connection
*/
private function formatDSN(Connection $connection)
private function format11DSN(Connection $connection)
{
$driver_name = $this->getPropelDriverName($connection->getDriver());
$driver_name = $this->getPropel12DriverName($connection->getDriver());
if ($connection->getDriver()->getName() == 'pdo_sqlite') {
return sprintf(
'%s://hack.nl/%s',
Expand All @@ -172,17 +172,57 @@ private function formatDSN(Connection $connection)
}

/**
* Return the proper drivername for Propel version 1.2
* If the mysql extension is not loaded, fall back to
* mysqli, thus being compatible with PHP 7.
*
* @param Driver $driver
* @throws \DomainException
* @throws \RuntimeException
* @return string
*/
private function getPropelDriverName(Driver $driver)
private function getPropel12DriverName(Driver $driver)
{
switch($driver->getName()) {
case 'pdo_mysql':
if (extension_loaded('mysql')) {
return 'mysql';
} elseif(extension_loaded('mysqli')) {
return 'mysqli';
} else {
throw new \RuntimeException('The mysql and mysqli extension both not loaded, need one of them.');
}
case 'pdo_pgsql':
if (extension_loaded('pgsql')) {
return 'pgsql';
} else {
throw new \RuntimeException('The pgsql extension is not loaded.');
}
return 'pgsql';
case 'pdo_sqlite':
if (function_exists('sqlite_open')) {
return 'sqlite';
} else {
throw new \RuntimeException('The sqlite extension is not loaded.');
}
default:
throw new \DomainException(sprintf('Unknown driver "%s"', $driver->getName()));
}
}

/**
* @param Driver $driver
* @throws \DomainException
* @return string
*/
private function getPropel14DriverName(Driver $driver)
{
$lookup_table = array(
'pdo_mysql' => 'mysql',
'pdo_pgsql' => 'pgsql',
'pdo_sqlite' => 'sqlite'
);

if (isset($lookup_table[$driver->getName()])) {
return $lookup_table[$driver->getName()];
}
Expand Down

0 comments on commit d3abf5a

Please sign in to comment.