Skip to content

Instantly share code, notes, and snippets.

@mbabker
Created June 13, 2015 22:47
Show Gist options
  • Save mbabker/f7f87195b31c597bd92e to your computer and use it in GitHub Desktop.
Save mbabker/f7f87195b31c597bd92e to your computer and use it in GitHub Desktop.
getInstance Comparision
public static function getInstance($options = array())
{
// Sanitize the database connector options.
$options['driver'] = (isset($options['driver'])) ? preg_replace('/[^A-Z0-9_\.-]/i', '', $options['driver']) : 'mysqli';
$options['database'] = (isset($options['database'])) ? $options['database'] : null;
$options['select'] = (isset($options['select'])) ? $options['select'] : true;
// If the selected driver is `mysql` and we are on PHP 7 or greater, switch to the `mysqli` driver.
if ($options['driver'] === 'mysql' && PHP_MAJOR_VERSION >= 7)
{
// Check if we have support for the other MySQL drivers
$mysqliSupported = JDatabaseDriverMysqli::isSupported();
$pdoMysqlSupported = JDatabaseDriverPdomysql::isSupported();
// If neither is supported, then the user cannot use MySQL; throw an exception
if (!$mysqliSupported && !$pdoMysqlSupported)
{
throw new RuntimeException(
'The PHP `ext/mysql` extension is removed in PHP 7, cannot use the `mysql` driver.'
. ' Also, this system does not support MySQLi or PDO MySQL. Cannot instantiate database driver.'
);
}
// Prefer MySQLi as it is a closer replacement for the removed MySQL driver, otherwise use the PDO driver
if ($mysqliSupported)
{
JLog::add(
'The PHP `ext/mysql` extension is removed in PHP 7, cannot use the `mysql` driver. Trying `mysqli` instead.',
JLog::WARNING,
'deprecated'
);
$options['driver'] = 'mysqli';
}
else
{
JLog::add(
'The PHP `ext/mysql` extension is removed in PHP 7, cannot use the `mysql` driver. Trying `pdomysql` instead.',
JLog::WARNING,
'deprecated'
);
$options['driver'] = 'pdomysql';
}
}
// Get the options signature for the database connector.
$signature = md5(serialize($options));
// If we already have a database connector instance for these options then just use that.
if (empty(self::$instances[$signature]))
{
// Derive the class name from the driver.
$class = 'JDatabaseDriver' . ucfirst(strtolower($options['driver']));
// If the class still doesn't exist we have nothing left to do but throw an exception. We did our best.
if (!class_exists($class))
{
throw new RuntimeException(sprintf('Unable to load Database Driver: %s', $options['driver']));
}
// Create our new JDatabaseDriver connector based on the options given.
try
{
$instance = new $class($options);
}
catch (RuntimeException $e)
{
throw new RuntimeException(sprintf('Unable to connect to the Database: %s', $e->getMessage()));
}
// Set the new connector to the global instances based on signature.
self::$instances[$signature] = $instance;
}
return self::$instances[$signature];
}
public static function getInstance($options = array())
{
// Sanitize the database connector options.
$options['driver'] = (isset($options['driver'])) ? preg_replace('/[^A-Z0-9_\.-]/i', '', $options['driver']) : 'mysqli';
$options['database'] = (isset($options['database'])) ? $options['database'] : null;
$options['select'] = (isset($options['select'])) ? $options['select'] : true;
// If the selected driver is `mysql` and we are on PHP 7 or greater, switch to the `mysqli` driver.
// START MODIFICATION
$isPhp7 = PHP_MAJOR_VERSION >= 7;
$isDeprecatedMysql = $options['driver'] === 'mysql';
if ($isPhp7 && $isDeprecatedMysql)
// END MODIFICATION
{
// Check if we have support for the other MySQL drivers
$mysqliSupported = JDatabaseDriverMysqli::isSupported();
$pdoMysqlSupported = JDatabaseDriverPdomysql::isSupported();
// If neither is supported, then the user cannot use MySQL; throw an exception
if (!$mysqliSupported && !$pdoMysqlSupported)
{
throw new RuntimeException(
'The PHP `ext/mysql` extension is removed in PHP 7, cannot use the `mysql` driver.'
. ' Also, this system does not support MySQLi or PDO MySQL. Cannot instantiate database driver.'
);
}
// Prefer MySQLi as it is a closer replacement for the removed MySQL driver, otherwise use the PDO driver
if ($mysqliSupported)
{
JLog::add(
'The PHP `ext/mysql` extension is removed in PHP 7, cannot use the `mysql` driver. Trying `mysqli` instead.',
JLog::WARNING,
'deprecated'
);
$options['driver'] = 'mysqli';
}
else
{
JLog::add(
'The PHP `ext/mysql` extension is removed in PHP 7, cannot use the `mysql` driver. Trying `pdomysql` instead.',
JLog::WARNING,
'deprecated'
);
$options['driver'] = 'pdomysql';
}
}
// Get the options signature for the database connector.
$signature = md5(serialize($options));
// If we already have a database connector instance for these options then just use that.
if (empty(self::$instances[$signature]))
{
// Derive the class name from the driver.
$class = 'JDatabaseDriver' . ucfirst(strtolower($options['driver']));
// If the class still doesn't exist we have nothing left to do but throw an exception. We did our best.
if (!class_exists($class))
{
throw new RuntimeException(sprintf('Unable to load Database Driver: %s', $options['driver']));
}
// Create our new JDatabaseDriver connector based on the options given.
try
{
$instance = new $class($options);
}
catch (RuntimeException $e)
{
throw new RuntimeException(sprintf('Unable to connect to the Database: %s', $e->getMessage()));
}
// Set the new connector to the global instances based on signature.
self::$instances[$signature] = $instance;
}
return self::$instances[$signature];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment