Exemplo n.º 1
0
 public static function getInstance($options = array())
 {
     // Sanitize the database connector options.
     $options['driver'] = isset($options['driver']) ? preg_replace('/[^A-Z0-9_\\.-]/i', '', $options['driver']) : 'mysql';
     $options['database'] = isset($options['database']) ? $options['database'] : null;
     $options['select'] = isset($options['select']) ? $options['select'] : true;
     // 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 = 'MDatabase' . ucfirst($options['driver']);
         // If the class doesn't exist, let's look for it and register it.
         if (!class_exists($class)) {
             // Derive the file path for the driver class.
             $path = dirname(__FILE__) . '/database/' . $options['driver'] . '.php';
             // If the file exists register the class with our class loader.
             if (file_exists($path)) {
                 MLoader::register($class, $path);
             } else {
                 // Legacy error handling switch based on the MError::$legacy switch.
                 // @deprecated  12.1
                 if (MError::$legacy) {
                     // Deprecation warning.
                     MLog::add('MError is deprecated.', MLog::WARNING, 'deprecated');
                     MError::setErrorHandling(E_ERROR, 'die');
                     return MError::raiseError(500, MText::sprintf('MLIB_DATABASE_ERROR_LOAD_DATABASE_DRIVER', $options['driver']));
                 } else {
                     throw new MDatabaseException(MText::sprintf('MLIB_DATABASE_ERROR_LOAD_DATABASE_DRIVER', $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)) {
             if (MError::$legacy) {
                 // Deprecation warning.
                 MLog::add('MError() is deprecated.', MLog::WARNING, 'deprecated');
                 MError::setErrorHandling(E_ERROR, 'die');
                 return MError::raiseError(500, MText::sprintf('MLIB_DATABASE_ERROR_LOAD_DATABASE_DRIVER', $options['driver']));
             } else {
                 throw new MDatabaseException(MText::sprintf('MLIB_DATABASE_ERROR_LOAD_DATABASE_DRIVER', $options['driver']));
             }
         }
         // Create our new MDatabase connector based on the options given.
         try {
             $instance = new $class($options);
         } catch (MDatabaseException $e) {
             // Legacy error handling switch based on the MError::$legacy switch.
             // @deprecated  12.1
             if (MError::$legacy) {
                 // Deprecation warning.
                 MLog::add('MError() is deprecated.', MLog::WARNING, 'deprecated');
                 MError::setErrorHandling(E_ERROR, 'ignore');
                 return MError::raiseError(500, MText::sprintf('MLIB_DATABASE_ERROR_CONNECT_DATABASE', $e->getMessage()));
             } else {
                 throw new MDatabaseException(MText::sprintf('MLIB_DATABASE_ERROR_CONNECT_DATABASE', $e->getMessage()));
             }
         }
         // Set the new connector to the global instances based on signature.
         self::$instances[$signature] = $instance;
     }
     return self::$instances[$signature];
 }
Exemplo n.º 2
0
 public static function registerErrorLevel($level, $name, $handler = 'ignore')
 {
     // Deprecation warning.
     MLog::add('MError::registerErrorLevel() is deprecated.', MLog::WARNING, 'deprecated');
     if (isset(MError::$levels[$level])) {
         return false;
     }
     MError::$levels[$level] = $name;
     MError::setErrorHandling($level, $handler);
     return true;
 }