[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/src/ -> lmbDbDSN.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/net/src/lmbUri.class.php');
  10  lmb_require('limb/core/src/lmbObject.class.php');
  11  
  12  /**
  13   * class lmbDbDSN.
  14   *
  15   * @package dbal
  16   * @version $Id: lmbDbDSN.class.php 6002 2007-06-19 21:09:36Z pachanga $
  17   */
  18  class lmbDbDSN extends lmbObject
  19  {
  20    protected $uri;
  21    protected $extra = array();
  22  
  23    function __construct($args)
  24    {
  25      if(is_array($args))
  26      {
  27        foreach($args as $key => $value)
  28        {
  29          if(is_numeric($key) && is_array($value))
  30            $this->extra = $value;
  31          else
  32            $this->$key = $value;
  33        }
  34      }
  35      elseif(is_string($args))
  36        $this->_parseUri($args);
  37    }
  38  
  39    function _parseUri($str)
  40    {
  41      try
  42      {
  43        $this->uri = new lmbUri($str);
  44      }
  45      catch(lmbException $e)
  46      {
  47        throw new lmbException("Database DSN '$str' is not valid");
  48      }
  49  
  50      $this->driver = $this->uri->getProtocol();
  51      $this->host = $this->uri->getHost();
  52      $this->user = $this->uri->getUser();
  53      $this->password = $this->uri->getPassword();
  54      $this->database = substr($this->uri->getPath(), 1);//removing only first slash
  55      $this->port = $this->uri->getPort();
  56      $this->extra = $this->uri->getQueryItems();
  57    }
  58  
  59    function _getUri()
  60    {
  61      if(is_object($this->uri))
  62        return $this->uri;
  63  
  64      $this->uri = new lmbUri();
  65      $this->uri->setProtocol($this->driver);
  66      $this->uri->setHost($this->host);
  67      $this->uri->setUser($this->user);
  68      $this->uri->setPassword($this->password);
  69      $this->uri->setPath('/' . $this->database);
  70  
  71      if(isset($this->port))
  72        $this->uri->setPort($this->port);
  73      if(count($this->extra))
  74        $this->uri->setQueryItems($this->extra);
  75  
  76      return $this->uri;
  77    }
  78  
  79    function get($name)
  80    {
  81      $value = parent :: get($name);
  82  
  83      if(isset($value))
  84        return $value;
  85  
  86      if(isset($this->extra[$name]))
  87        return $this->extra[$name];
  88    }
  89  
  90    function toString()
  91    {
  92      return $this->_getUri()->toString();
  93    }
  94  }
  95  
  96  ?>


Generated: Sat Nov 22 03:48:54 2008 Cross-referenced by PHPXref 0.7