Source for file ConnectorFactory.php

Documentation is available at ConnectorFactory.php

  1. <?php
  2.  
  3. ///////////////////////////////////////////////////////////////////////////////
  4. /**
  5.  * Include this file in your application to use the Connector library.
  6.  *
  7.  * Normally, there is no need to include any other files from the Connector
  8.  * library. Call the static {@link ConnectorFactory::create()} method to get
  9.  * an IConnector instance with an open database connection.
  10.  *
  11.  * System requirements:
  12.  * <ul>
  13.  * <li>PHP 5</li>
  14.  * <li>The supported PHP database extensions.</li>
  15.  * </ul>
  16.  *
  17.  * This library is free software: you can redistribute it and/or modify
  18.  * it under the terms of the GNU Lesser General Public License as published by
  19.  * the Free Software Foundation, either version 3 of the License, or
  20.  * (at your option) any later version.
  21.  * The Connector library is distributed in the hope that it will be useful,
  22.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24.  * {@link http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License}
  25.  * for more details.
  26.  *
  27.  * @author Per Egil Roksvaag
  28.  * @copyright 2009 Per Egil Roksvaag
  29.  * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  30.  * @package connector
  31.  * @version 2.0.0
  32.  */
  33.  
  34. ///////////////////////////////////////////////////////////////////////////////
  35. /**
  36.  * Include the IConnector interface.
  37.  */
  38.  
  39. require_once("IConnector.php");
  40.  
  41. ///////////////////////////////////////////////////////////////////////////////
  42. /**
  43.  * Include this file in your application to use the Connector library.
  44.  *
  45.  * Normally, there is no need to include any other files from the Connector
  46.  * library. Call the static {@link ConnectorFactory::create()} method to get
  47.  * an IConnector instance with an open database connection.
  48.  *
  49.  * @package connector
  50.  */
  51.  
  52. {
  53.     ///////////////////////////////////////////////////////////////////////////
  54.     /**
  55.      * @var array An associated array of initialized IConnector instances.
  56.      */
  57.  
  58.     static private $cache array();
  59.  
  60.     ///////////////////////////////////////////////////////////////////////////
  61.     /**
  62.      * Include and create an instance of the selected IConnector class.
  63.      *
  64.      * If an IConnector instance with identical classname and connection
  65.      * parameters already exists, the existing instance is returned. If the
  66.      * classname is unknown or an error occurs, null is returend.
  67.      *
  68.      * IMPORTANT: Only use IConnector constants in the <var>$connection</var>
  69.      * and <var>$options</var> parameters. <b>No native constants</b> are
  70.      * available before this function has completed.
  71.      *
  72.      * @param string $classname The name of the IConnector class to use as a database connector.
  73.      * @param array $connection An associated array of connection settings.
  74.      * @param array $options An associated array of global options.
  75.      * @return IConnector A new or existing IConnector instance with an open database connection.
  76.      */
  77.  
  78.     static public function create($classname$connection$options array())
  79.     {
  80.         $hash md5($classname.serialize($connection));
  81.  
  82.         if(array_key_exists($hashself::$cache))
  83.         {
  84.             $instance self::$cache[$hash];
  85.             $instance->setOptions($options);
  86.             return $instance;
  87.         }
  88.         switch($classname)
  89.         {
  90.             case "PdoConnector"require_once("PdoConnector.php")break;
  91.             case "OdbcConnector"require_once("OdbcConnector.php")break;
  92.             case "MysqlConnector"require_once("MysqlConnector.php")break;
  93.             case "MssqlConnector"require_once("MssqlConnector.php")break;
  94.             case "SqlsrvConnector"require_once("SqlsrvConnector.php")break;
  95.             defaultreturn null;
  96.         }
  97.         $instance new $classname($connection$options);
  98.  
  99.         if($instance->getLink())
  100.         {
  101.             self::$cache[$hash$instance;
  102.             return $instance;
  103.         }
  104.         return null;
  105.     }
  106.  
  107.     ///////////////////////////////////////////////////////////////////////////
  108. }
  109.  
  110. ?>

Documentation generated on Wed, 03 Jun 2009 12:41:51 +0200 by phpDocumentor 1.4.1