コード例 #1
0
ファイル: Db.php プロジェクト: NareshChennuri/pyng
 /**
  * Factory for JO_Db_Adapter_Abstract classes.
  *
  * First argument may be a string containing the base of the adapter class
  * name, e.g. 'Mysqli' corresponds to class JO_Db_Adapter_Mysqli.  This
  * name is currently case-insensitive, but is not ideal to rely on this behavior.
  * If your class is named 'My_Company_Pdo_Mysql', where 'My_Company' is the namespace
  * and 'Pdo_Mysql' is the adapter name, it is best to use the name exactly as it
  * is defined in the class.  This will ensure proper use of the factory API.
  *
  * First argument may alternatively be an object of type JO_Config.
  * The adapter class base name is read from the 'adapter' property.
  * The adapter config parameters are read from the 'params' property.
  *
  * Second argument is optional and may be an associative array of key-value
  * pairs.  This is used as the argument to the adapter constructor.
  *
  * If the first argument is of type JO_Config, it is assumed to contain
  * all parameters, and the second argument is ignored.
  *
  * @param  mixed $adapter String name of base adapter class, or JO_Config object.
  * @param  mixed $config  OPTIONAL; an array or JO_Config object with adapter parameters.
  * @return JO_Db_Adapter_Abstract
  * @throws JO_Db_Exception
  */
 public static function factory($adapter, $config = array())
 {
     if ($config instanceof JO_Config) {
         $config = $config->toArray();
     }
     /*
      * Convert JO_Config argument to plain string
      * adapter name and separate config object.
      */
     if ($adapter instanceof JO_Config) {
         if (isset($adapter->params)) {
             $config = $adapter->params->toArray();
         }
         if (isset($adapter->adapter)) {
             $adapter = (string) $adapter->adapter;
         } else {
             $adapter = null;
         }
     }
     /*
      * Verify that adapter parameters are in an array.
      */
     if (!is_array($config)) {
         /**
          * @see JO_Db_Exception
          */
         require_once 'JO/Db/Exception.php';
         throw new JO_Db_Exception('Adapter parameters must be in an array or a JO_Config object');
     }
     /*
      * Verify that an adapter name has been specified.
      */
     if (!is_string($adapter) || empty($adapter)) {
         /**
          * @see JO_Db_Exception
          */
         require_once 'JO/Db/Exception.php';
         throw new JO_Db_Exception('Adapter name must be specified in a string');
     }
     /*
      * Form full adapter class name
      */
     $adapterNamespace = 'JO_Db_Adapter';
     if (isset($config['adapterNamespace'])) {
         if ($config['adapterNamespace'] != '') {
             $adapterNamespace = $config['adapterNamespace'];
         }
         unset($config['adapterNamespace']);
     }
     // Adapter no longer normalized- see http://framework.zend.com/issues/browse/ZF-5606
     $adapterName = $adapterNamespace . '_';
     $adapterName .= str_replace(' ', '_', ucwords(str_replace('_', ' ', strtolower($adapter))));
     /*
      * Load the adapter class.  This throws an exception
      * if the specified class cannot be loaded.
      */
     if (!class_exists($adapterName)) {
         require_once 'JO/Loader.php';
         JO_Loader::loadClass($adapterName);
     }
     /*
      * Create an instance of the adapter class.
      * Pass the config to the adapter class constructor.
      */
     self::$dbAdapter_name = $adapterName;
     self::$dbAdapter = new $adapterName($config);
     /*
      * Verify that the object created is a descendent of the abstract adapter type.
      */
     if (!self::$dbAdapter instanceof JO_Db_Adapter_Abstract) {
         /**
          * @see JO_Db_Exception
          */
         require_once 'JO/Db/Exception.php';
         throw new JO_Db_Exception("Adapter class '{$adapterName}' does not extend JO_Db_Adapter_Abstract");
     }
     return self::$dbAdapter;
 }