<?php
/***************************************************************************
* Copyright (C) 2006-2007 by Konstantin V. Arkhipov *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
**************************************************************************/
final class DBPool extends Singleton implements Instantiatable
{
private $default = null;
private $pool = array();
/**
* @return DBPool
**/
public static function me()
{
return Singleton::getInstance(__CLASS__);
}
/**
* @param DBD\DBD) $db
* @return DBPool
*/
public function setDefault(DBD\DBD $db)
{
$this->default = $db;
return $this;
}
/**
* @return DBPool
**/
public function dropDefault()
{
$this->default = null;
return $this;
}
/**
* @param $name
* @param DBD\DBD $db
* @return DBPool
* @throws Exception
*/
public function addLink($name, DBD\DBD $db)
{
if (isset($this->pool[$name]))
throw new Exception(
"DBPool already have '{$name}' link"
);
$this->pool[$name] = $db;
return $this;
}
/**
* @param $name
* @return DBPool
* @throws Exception
*/
public function dropLink($name)
{
if (!isset($this->pool[$name]))
throw new Exception(
"DB link '{$name}' not found"
);
unset($this->pool[$name]);
return $this;
}
/**
* @param null $name
* @return DBD\DBD
* @throws Exception
*/
public function getLink($name = null)
{
$link = null;
// single-DB project
if (!$name) {
if (!$this->default)
throw new Exception(
'DBPool:: i have no default link'
.' and requested link name is null'
);
$link = $this->default;
} elseif (isset($this->pool[$name]))
$link = $this->pool[$name];
if ($link) {
# if (!$link->isConnected())
# $link->connect();
return $link;
}
throw new Exception(
"DBPool: can't find link with '{$name}' name"
);
}
/**
* @return DBPool
**/
public function shutdown()
{
$this->disconnect();
$this->default = null;
$this->pool = array();
return $this;
}
/**
* @return DBPool
**/
public function disconnect()
{
if ($this->default !== null) {
$this->default->disconnect();
}
foreach ($this->pool as $db)
$db->disconnect();
return $this;
}
}
<?php
/***************************************************************************
* Copyright (C) 2004-2007 by Sveta A. Smirnova *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
**************************************************************************/
abstract class Singleton
{
private static $instances = array();
protected function __construct() {/* you can't create me */}
/// @example singleton.php
final public static function getInstance(
$class, $args = null /* , ... */
)
{
// for Singleton::getInstance('class_name', $arg1, ...) calling
if (2 < func_num_args())
{
$args = func_get_args();
array_shift($args);
}
if (!isset(self::$instances[$class]))
{
$object =
$args
? new $class($args)
: new $class();
if (!($object instanceof Singleton))
{
throw new Exception(
"Class '{$class}' is something not a Singleton's child"
);
}
return self::$instances[$class] = $object;
}
else
{
return self::$instances[$class];
}
}
final public static function getAllInstances()
{
return self::$instances;
}
final private function __clone() {/* do not clone me */}
}