mihasya / mihasya_libs

Some PHP libs

This URL has Read+Write access

mihasya_libs / mihasya_pdo.php
100644 39 lines (38 sloc) 1.217 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
abstract class PDONamespace {
  public static $connections;
  public static $conf = array();
}
#grab a pdo connection; create it if needed;
function pdo($db) {
  if (!PDONamespace::$connections[$db]) {
    if (!PDONamespace::$conf[$db]) return false; //init hasnt been run
    //go on and create the connection
    $conf = PDONamespace::$conf[$db];
    $dsn = $conf['dsn']; //some sort of PDO weirdness
    PDONamespace::$connections[$db] = new PDO($dsn, $conf['user'], $conf['pwd']);
  }
  return PDONamespace::$connections[$db];
}
#initialize the given configuration file
function pdo_init($conn_name, $conf_array) {
  $newConf = array($conn_name=>$conf_array);
  PDONamespace::$conf = array_merge(PDONamespace::$conf, $newConf);
  return true;
}
#abstracts getting configuration from a file (checks apc first)
#TODO: move this to a conf library;
function fetch_conf_file($fname) {
  $apc = function_exists('apc_fetch'); //do we have apc?
  $key = 'pdo_conf_'.md5(fname);
  if ($apc) {
    $apc_value = apc_fetch($key);
    if ($apc_value) return unserialize($apc_value);
  }
  $fcontents = parse_ini_file($fname, true);
  if ($apc) {
    apc_store($key, serialize($fcontents));
  }
  return $fcontents;
}
?>