Show
Ignore:
Timestamp:
02/26/08 01:22:21 (8 months ago)
Author:
mikey
Message:

refactored net::stubbles::rdbms::stubDatabaseConnectionProvider to make use of the new injection provider features

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnectionProvider.php

    r1370 r1373  
    99stubClassLoader::load('net::stubbles::ioc::stubInjectionProvider', 
    1010                      'net::stubbles::rdbms::stubDatabaseConnectionData', 
    11                       'net::stubbles::rdbms::stubDatabaseConnectionPool' 
     11                      'net::stubbles::rdbms::stubDatabaseConnectionPool', 
     12                      'net::stubbles::rdbms::stubDatabaseException' 
    1213); 
    1314/** 
     
    2021{ 
    2122    /** 
    22      * the connection id to be used 
     23     * switch whether to fallback to default connection if no named connection exists 
    2324     * 
    24      * @var  string 
     25     * @var  bool 
    2526     */ 
    26     protected $connectionId
     27    protected $fallback = true
    2728 
    2829    /** 
    2930     * constructor 
    3031     * 
    31      * @param  string  $connectionId  provider should return connections for this id 
     32     * @param  bool  $fallback  whether to fallback to default connection if no named connection exists 
    3233     */ 
    33     public function __construct($connectionId = stubDatabaseConnectionData::DEFAULT_ID
     34    public function __construct($fallback = true
    3435    { 
    35         $this->connectionId = $connectionId
     36        $this->fallback = $fallback
    3637    } 
    3738 
     
    3940     * returns the connection to be injected 
    4041     * 
     42     * If a name is provided and a condition with this name exists this 
     43     * connection will be returned. If fallback is enabled and the named 
     44     * connection does not exist the default connection will be returned, if 
     45     * fallback is disabled a stubDatabaseException will be thrown. 
     46     * 
     47     * If no name is provided the default connection will be returned. 
     48     * 
    4149     * @param   string                  $type 
    42      * @param   string                  $name 
     50     * @param   string                  $name  optional 
    4351     * @return  stubDatabaseConnection 
     52     * @throws  stubDatabaseException 
    4453     */ 
    4554    public function get($type, $name = null) 
    4655    { 
    47         return stubDatabaseConnectionPool::getConnection($this->connectionId); 
     56        if (null !== $name) { 
     57            if (stubDatabaseConnectionPool::hasConnectionData($name) === true) { 
     58                return stubDatabaseConnectionPool::getConnection($name); 
     59            } 
     60             
     61            if (false === $this->fallback) { 
     62                throw new stubDatabaseException('No connection and no dsn known for connection associated with id ' . $name); 
     63            } 
     64        } 
     65         
     66        return stubDatabaseConnectionPool::getConnection(stubDatabaseConnectionData::DEFAULT_ID); 
    4867    } 
    4968}