Manager.php 9.28 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
doctrine's avatar
doctrine committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
require_once("Configurable.php");
require_once("EventListener.php");
/**
 * @package     Doctrine ORM
 * @url         www.phpdoctrine.com
 * @license     LGPL
 * 
 * Doctrine_Manager is the base component of all doctrine based projects. 
 * It opens and keeps track of all sessions (database connections).
 */
class Doctrine_Manager extends Doctrine_Configurable implements Countable, IteratorAggregate {
    /**
     * @var array $session          an array containing all the opened sessions
     */
    private $sessions   = array();
    /**
     * @var integer $index          the incremented index
     */
    private $index      = 0;
    /**
     * @var integer $currIndex      the current session index
     */
    private $currIndex  = 0;
    /**
     * @var string $root            root directory
     */
    private $root; 
    /**
     * @var Doctrine_Null $null     Doctrine_Null object, used for extremely fast null value checking
     */
    private $null;

    /**
     * constructor
     */
    private function __construct() {
        $this->root = dirname(__FILE__);
        $this->null = new Doctrine_Null;

        Doctrine_Record::initNullObject($this->null);
        Doctrine_Collection::initNullObject($this->null);
62
        Doctrine_Record_Iterator::initNullObject($this->null);
63
        Doctrine_Validator::initNullObject($this->null);
doctrine's avatar
doctrine committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    }
    /**
     * @return Doctrine_Null
     */
    final public function getNullObject() {
        return $this->null;
    }
    /**
     * setDefaultAttributes
     * sets default attributes
     *
     * @return boolean
     */
    final public function setDefaultAttributes() {
        static $init = false;
        if( ! $init) {
            $init = true;
            $attributes = array(
                        Doctrine::ATTR_CACHE_DIR        => "%ROOT%".DIRECTORY_SEPARATOR."cachedir",
doctrine's avatar
doctrine committed
83
                        Doctrine::ATTR_FETCHMODE        => Doctrine::FETCH_IMMEDIATE,
doctrine's avatar
doctrine committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
                        Doctrine::ATTR_CACHE_TTL        => 100,
                        Doctrine::ATTR_CACHE_SIZE       => 100,
                        Doctrine::ATTR_CACHE            => Doctrine::CACHE_NONE,
                        Doctrine::ATTR_BATCH_SIZE       => 5,
                        Doctrine::ATTR_COLL_LIMIT       => 5,
                        Doctrine::ATTR_LISTENER         => new EmptyEventListener(),
                        Doctrine::ATTR_PK_COLUMNS       => array("id"),
                        Doctrine::ATTR_PK_TYPE          => Doctrine::INCREMENT_KEY,
                        Doctrine::ATTR_LOCKMODE         => 1,
                        Doctrine::ATTR_VLD              => false,
                        Doctrine::ATTR_CREATE_TABLES    => true
                        );
            foreach($attributes as $attribute => $value) {
                $old = $this->getAttribute($attribute);
                if($old === null)
                    $this->setAttribute($attribute,$value);
            }
            return true;
        }
        return false;
    }
    /**
     * returns the root directory of Doctrine
     *
     * @return string
     */
    final public function getRoot() {
        return $this->root;
    }
    /**
     * getInstance                  
     * returns an instance of this class
     * (this class uses the singleton pattern)
     *
     * @return Doctrine_Manager
     */
    final public static function getInstance() {
        static $instance;
        if( ! isset($instance))
            $instance = new self();

        return $instance;
    }
127 128 129 130 131 132 133 134 135 136 137 138
    /**
     * install
     * 
     * @return void
     */
    final public function install() {
        $parent = new ReflectionClass('Doctrine_Record');
        $old    = $this->getAttribute(Doctrine::ATTR_CREATE_TABLES);

        $this->attributes[Doctrine::ATTR_CREATE_TABLES] = true;
        foreach(get_declared_classes() as $name) {
            $class = new ReflectionClass($name);
doctrine's avatar
doctrine committed
139

140 141 142 143 144
            if($class->isSubclassOf($parent))
                $obj = new $class();
        }
        $this->attributes[Doctrine::ATTR_CREATE_TABLES] = $old;
    }
doctrine's avatar
doctrine committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    /**
     * openSession                          
     * opens a new session and saves it to Doctrine_Manager->sessions
     *
     * @param PDO $pdo                      PDO database driver
     * @param string $name                  name of the session, if empty numeric key is used
     * @return Doctrine_Session
     */
    final public function openSession(PDO $pdo, $name = null) {
        // initialize the default attributes
        $this->setDefaultAttributes();

        if($name !== null) {
            $name = (string) $name;
            if(isset($this->sessions[$name]))
                throw new InvalidKeyException();
        
        } else {
            $name = $this->index;
            $this->index++;
        }
        switch($pdo->getAttribute(PDO::ATTR_DRIVER_NAME)):
            case "mysql":
                $this->sessions[$name] = new Doctrine_Session_Mysql($this,$pdo);
            break;
            case "sqlite":
                $this->sessions[$name] = new Doctrine_Session_Sqlite($this,$pdo);
            break;
            case "pgsql":
                $this->sessions[$name] = new Doctrine_Session_Pgsql($this,$pdo);
            break;
            case "oci":
                $this->sessions[$name] = new Doctrine_Session_Oracle($this,$pdo);
            break;
            case "mssql":
                $this->sessions[$name] = new Doctrine_Session_Mssql($this,$pdo);
            break;
            case "firebird":
                $this->sessions[$name] = new Doctrine_Session_Firebird($this,$pdo);
            break;
            case "informix":
                $this->sessions[$name] = new Doctrine_Session_Informix($this,$pdo);
            break;
        endswitch;


        $this->currIndex = $name;
        return $this->sessions[$name];
    }
    /**
     * getSession
     * @param integer $index
     * @return object Doctrine_Session
     * @throws InvalidKeyException
     */
    final public function getSession($index) {
        if( ! isset($this->sessions[$index]))
            throw new InvalidKeyException();

        $this->currIndex = $index;
        return $this->sessions[$index];
    }
    /**
     * closes the session
     *
     * @param Doctrine_Session $session
     * @return void
     */
    final public function closeSession(Doctrine_Session $session) {
        $session->close();
        unset($session);
    }
    /**
     * getSessions
     * returns all opened sessions
     *
     * @return array
     */
    final public function getSessions() {
        return $this->sessions;
    }
    /**
     * setCurrentSession
     * sets the current session to $key
     *
     * @param mixed $key                        the session key
     * @throws InvalidKeyException
     * @return void
     */
    final public function setCurrentSession($key) {
        $key = (string) $key;
        if( ! isset($this->sessions[$key]))
            throw new InvalidKeyException();
        
        $this->currIndex = $key;
    }
    /**
     * count
     * returns the number of opened sessions
     *
     * @return integer
     */
    public function count() {
        return count($this->sessions);
    }
    /**
     * getIterator
     * returns an ArrayIterator that iterates through all sessions
     *
     * @return ArrayIterator
     */
    public function getIterator() {
        return new ArrayIterator($this->sessions);
    }
    /**
     * getCurrentSession
     * returns the current session
     *
     * @throws Doctrine_Session_Exception       if there are no open sessions
     * @return Doctrine_Session
     */
    final public function getCurrentSession() {
        $i = $this->currIndex;
        if( ! isset($this->sessions[$i]))
            throw new Doctrine_Session_Exception();

        return $this->sessions[$i];
    }
    /**
     * __toString
     * returns a string representation of this object
     *
     * @return string
     */
    public function __toString() {
        $r[] = "<pre>";
        $r[] = "Doctrine_Manager";
        $r[] = "Sessions : ".count($this->sessions);
        $r[] = "</pre>";
        return implode("\n",$r);
    }
}
?>