Ejemplo n.º 1
0
 /**
  * Constructor.
  *
  * @param   array  $options  Array of database options with keys: host, user, password, database, select.
  *
  * @since   12.1
  */
 public function __construct($options)
 {
     // PHP's `mysql` extension is not present in PHP 7, block instantiation in this environment
     if (PHP_MAJOR_VERSION >= 7) {
         throw new RuntimeException('This driver is unsupported in PHP 7, please use the MySQLi or PDO MySQL driver instead.');
     }
     // Get some basic values from the options.
     $options['host'] = isset($options['host']) ? $options['host'] : 'localhost';
     $options['user'] = isset($options['user']) ? $options['user'] : '******';
     $options['password'] = isset($options['password']) ? $options['password'] : '';
     $options['database'] = isset($options['database']) ? $options['database'] : '';
     $options['select'] = isset($options['select']) ? (bool) $options['select'] : true;
     // Finalize initialisation.
     parent::__construct($options);
 }
Ejemplo n.º 2
0
 /**
  * Method to return a F0FDatabaseDriver instance based on the given options.  There are three global options and then
  * the rest are specific to the database driver.  The 'driver' option defines which F0FDatabaseDriver class is
  * used for the connection -- the default is 'mysqli'.  The 'database' option determines which database is to
  * be used for the connection.  The 'select' option determines whether the connector should automatically select
  * the chosen database.
  *
  * Instances are unique to the given options and new objects are only created when a unique options array is
  * passed into the method.  This ensures that we don't end up with unnecessary database connection resources.
  *
  * @param   array  $options  Parameters to be passed to the database driver.
  *
  * @return  F0FDatabaseDriver  A database object.
  *
  * @since   11.1
  * @throws  RuntimeException
  */
 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 = F0FDatabaseDriverMysqli::isSupported();
         $pdoMysqlSupported = F0FDatabaseDriverPdomysql::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) {
             if (class_exists('JLog')) {
                 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 {
             if (class_exists('JLog')) {
                 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 = 'F0FDatabaseDriver' . 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 F0FDatabaseDriver 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()), $e->getCode(), $e);
         }
         // Set the new connector to the global instances based on signature.
         self::$instances[$signature] = $instance;
     }
     return self::$instances[$signature];
 }