<?php
/*
 * This file is part of the sfPropel13Plugin package.
 * (c) 2007 Joshua May <notjosh@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

class sfPropel13Database extends sfDatabase
{
  static protected $config = array();
  
  /**
   * @see sfDatabase::initialize()
   */
  public function initialize($parameters = null, $name = 'propel')
  {
    parent::initialize($parameters);
    if (!$this->hasParameter('datasource'))
    {
      $this->setParameter('datasource', $name);
    }

    $this->addConfig();

    $is_default = $this->getParameter('is_default', false);

    // first defined if none listed as default
    if ($is_default || count(self::$config['propel']['datasources']) == 1)
    {
      $this->setDefaultConfig();
    }
  	
    // setup propel
    if (null !== $this->getParameter('instance-pooling'))
    {
      $instancePooling = $this->getParameter('instance-pooling');
      if (is_bool($instancePooling))
      {
      	if ($instancePooling)
      	{
      	  Propel::enableInstancePooling();
      	}
      	else
      	{
      	  Propel::disableInstancePooling();
      	}
      }
    }

    Propel::setConfiguration(self::$config);
    Propel::initialize();
  }
  
  public function setDefaultConfig()
  {
    self::$config['propel']['datasources']['default'] = $this->getParameter('datasource');
  }

  public function addConfig()
  {
  	if ($dsn = $this->getParameter('dsn'))
  	{
  	  $params = array();

  		// check for non-pdo dsn
  		if (false !== strpos($dsn, '//'))
  		{
        // derive pdo dsn (etc) from old style dsn
        $params = $this->parseOldDsn($dsn);

        $dsn = $params['phptype'] . ':dbname=' . $params['database'] . ';host=' . $params['hostspec'];
        $this->setParameter('dsn', $dsn);
      }
      else
      {
        $params = $this->parseDsn($dsn);
      }

      $options = array('phptype', 'hostspec', 'database', 'username', 'password', 'port', 'protocol', 'encoding', 'persistent');
      foreach ($options as $option)
      {
        if (!$this->getParameter($option) && isset($params[$option]))
        {
          $this->setParameter($option, $params[$option]);
        }
      }
  	}

    self::$config['propel']['datasources'][$this->getParameter('datasource')] =
      array(
        'adapter'      => $this->getParameter('phptype'),
        'connection'   =>
        array(
          'dsn' => $this->getParameter('dsn'),
          'user' => $this->getParameter('username'),
          'password' => $this->getParameter('password'),

          // not sure which (if any) of these are still relevant now that we use pdo.. doesn't break if i leave them though..?
          'phptype'    => $this->getParameter('phptype'),
          'hostspec'   => $this->getParameter('hostspec'),
          'port'       => $this->getParameter('port'),
          'encoding'   => $this->getParameter('encoding'),
          'persistent' => $this->getParameter('persistent'),
          'protocol'   => $this->getParameter('protocol'),
        ),
      );
  }

  /**
   * parse the new styled dsn, really i only want to grab the 'phptype' out
   *
   * @param string $dsn
   * @return array
   */
  private function parseDsn($dsn)
  {
    // i only really need the phptype, for now!

    return array(
      'phptype' => substr($dsn, 0, strpos($dsn, ':'))
    );
  }

  /**
   * this is the old Creole::parseDSN method, so i can parse old dsn's and connect via pdo still
   *
   * @param string $dsn
   * @return array
   */
  private function parseOldDsn($dsn)
  {
    if (is_array($dsn))
    {
        return $dsn;
    }

    $parsed = array(
        'phptype'  => null,
        'username' => null,
        'password' => null,
        'protocol' => null,
        'hostspec' => null,
        'port'     => null,
        'socket'   => null,
        'database' => null
    );

    $info = parse_url($dsn);

    if (count($info) === 1)
    { // if there's only one element in result, then it must be the phptype
        $parsed['phptype'] = array_pop($info);
        return $parsed;
    }

    // some values can be copied directly
    $parsed['phptype'] = @$info['scheme'];
    $parsed['username'] = @$info['user'];
    $parsed['password'] = @$info['pass'];
    $parsed['port'] = @$info['port'];

    $host = @$info['host'];
    if (false !== ($pluspos = strpos($host, '+')))
    {
      $parsed['protocol'] = substr($host,0,$pluspos);

      if ($parsed['protocol'] === 'unix')
      {
        $parsed['socket'] = substr($host,$pluspos+1);
      }
      else
      {
          $parsed['hostspec'] = substr($host,$pluspos+1);
      }
    }
    else
    {
        $parsed['hostspec'] = $host;
    }

    if (isset($info['path']))
    {
        $parsed['database'] = substr($info['path'], 1); // remove first char, which is '/'
    }

    if (isset($info['query']))
    {
      $opts = explode('&', $info['query']);
      foreach ($opts as $opt)
      {
        list($key, $value) = explode('=', $opt);

        if (!isset($parsed[$key]))
        { // don't allow params overwrite
          $parsed[$key] = urldecode($value);
        }
      }
    }

    return $parsed;
  }

  public static function getConfiguration()
  {
    return self::$config;
  }

  public function setConnectionParameter($key, $value)
  {
    if ($key == 'host')
    {
      $key = 'hostspec';
    }

    self::$config['propel']['datasources'][$this->getParameter('datasource')]['connection'][$key] = $value;
    $this->setParameter($key, $value);
  }
	
  /**
   * Connect to the database.
   * Stores the PDO connection in $connection
   *
   */
  public function connect()
  {
  	$this->connection = Propel::getConnection();
  }

  /**
   * Execute the shutdown procedure.
   *
   * @return void
   *
   * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database.
   */
  public function shutdown()
  {
    if ($this->connection !== null)
    {
      @$this->connection = null;
    }
  }
}
