# DBPool.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    00009  *                                                                         *
    00010  ***************************************************************************/
    00011
    00017     final class DBPool extends Singleton implements Instantiatable
    00018     {
    00019         private $default = null;
    00020
    00021         private $pool = array();
    00022
    00026         public static function me()
    00027         {
    00028             return Singleton::getInstance(__CLASS__);
    00029         }
    00030
    00034         public static function getByDao(GenericDAO $dao)
    00035         {
    00036             return self::me()->getLink($dao->getLinkName());
    00037         }
    00038
    00042         public function setDefault(DB $db)
    00043         {
    00044             $this->default = $db;
    00045
    00046             return $this;
    00047         }
    00048
    00052         public function dropDefault()
    00053         {
    00054             $this->default = null;
    00055
    00056             return $this;
    00057         }
    00058
    00063         public function addLink($name, DB $db)
    00064         {
    00065             if (isset($this->pool[$name]))
    00066                 throw new WrongArgumentException(
    00067                     "already have '{$name}' link"
    00068                 );
    00069
    00070             $this->pool[$name] = $db;
    00071
    00072             return $this;
    00073         }
    00074
    00079         public function dropLink($name)
    00080         {
    00081             if (!isset($this->pool[$name]))
    00082                 throw new MissingElementException(
    00083                     "link '{$name}' not found"
    00084                 );
    00085
    00086             unset($this->pool[$name]);
    00087
    00088             return $this;
    00089         }
    00090
    00095         public function getLink($name = null)
    00096         {
    00097             $link = null;
    00098
    00099             // single-DB project
    00100             if (!$name) {
    00101                 if (!$this->default)
    00102                     throw new MissingElementException(
    00103                         'i have no default link and requested link name is null'
    00104                     );
    00105
    00106                 $link = $this->default;
    00107             } elseif (isset($this->pool[$name]))
    00108                 $link = $this->pool[$name];
    00109
    00110             if ($link) {
    00111                 if (!$link->isConnected())
    00112                     $link->connect();
    00113
    00114                 return $link;
    00115             }
    00116
    00117             throw new MissingElementException(
    00118                 "can't find link with '{$name}' name"
    00119             );
    00120         }
    00121
    00125         public function shutdown()
    00126         {
    00127             $this->disconnect();
    00128
    00129             $this->default = null;
    00130             $this->pool = array();
    00131
    00132             return $this;
    00133         }
    00134
    00138         public function disconnect()
    00139         {
    00140             if ($this->default)
    00141                 $this->default->disconnect();
    00142
    00143             foreach ($this->pool as $db)
    00144                 $db->disconnect();
    00145
    00146             return $this;
    00147         }
    00148     }
    00149 ?>