Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
DSNManager.inc
1 <?php
14 require_once dirname(__FILE__).'/DSN.inc';
15 require_once dirname(__FILE__).'/DSNPool.inc';
16 
28 {
29 
30 
38  private static $_dsnpool_supported_conns = Array('db', 'dbsearch');
39 
45  private static $_dsns = Array();
46 
47 
57  public static function addDSN($conn_id = 'db', Array $dsn)
58  {
59  // If the DSN of the provided connection is already in the list, return FALSE immediately.
60  if (isset(self::$_dsns[$conn_id])) {
61  return FALSE;
62  }
63 
64  // Parse the DSN to convert the one entry DSN array to a DSN
65  self::_parseDSN($dsn);
66 
67  // If the DSN is a DSN array, create a DSNPool for it
68  if (self::_isDSNArray($dsn)) {
69  // Throw an exception if this connection does not support DSN Pool
70  if (!self::isDSNPoolSupportedConnection($conn_id)) {
71  throw new DALException("The connection '$conn_id' does not support DSN Pool.");
72  }
73 
74  $new_dsn = new DSNPool($dsn);
75  } else {
76  $new_dsn = new DSN($dsn);
77  }
78 
79  // Add the DSN or DSNPool object to the DSN list
80  self::$_dsns[$conn_id] = $new_dsn;
81 
82  return TRUE;
83 
84  }//end addDSN()
85 
86 
96  public static function getDSN($conn_id = 'db')
97  {
98  if (!isset(self::$_dsns[$conn_id])) {
99  return NULL;
100  }
101 
102  return self::$_dsns[$conn_id]->getDSN();
103 
104  }//end getDSN()
105 
106 
114  public static function registerDSNFailure($conn_id = 'db')
115  {
116  if (self::isDSNPool($conn_id)) {
117  self::$_dsns[$conn_id]->registerFailureDSN();
118  }
119 
120  }//end registerDSNFailure()
121 
122 
130  public static function isDSNPool($conn_id = 'db')
131  {
132  if (!isset(self::$_dsns[$conn_id])) {
133  return FALSE;
134  }
135 
136  return (self::$_dsns[$conn_id] instanceof DSNPool);
137 
138  }//end isDSNPool()
139 
140 
148  public static function isDSNPoolSupportedConnection($conn_id = 'db')
149  {
150  return in_array($conn_id, self::$_dsnpool_supported_conns);
151 
152  }//end isDSNPoolSupportedConnection()
153 
154 
163  private static function _parseDSN(&$dsn)
164  {
165  if (self::_isDSNArray($dsn) && (count($dsn) == 1)) {
166  $dsn = $dsn[0];
167  }
168 
169  }//end _parseDSN()
170 
171 
179  private static function _isDSNArray($dsn)
180  {
181  return !isset($dsn['DSN']);
182 
183  }//end _isDSNArray()
184 
185 
186 }//end class
187 
188 
189 ?>