initial commit

This commit is contained in:
Chris Sewell
2012-11-28 03:55:08 -05:00
parent 7adb399b2e
commit cf140a2e97
3247 changed files with 492437 additions and 0 deletions

View File

@ -0,0 +1,163 @@
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
* (c) 2004-2006 Sean Kerr.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfDatabase is a base abstraction class that allows you to setup any type of
* database connection via a configuration file.
*
* @package symfony
* @subpackage database
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Sean Kerr <skerr@mojavi.org>
* @version SVN: $Id: sfDatabase.class.php 3210 2007-01-10 20:28:16Z fabien $
*/
abstract class sfDatabase
{
protected
$connection = null,
$parameterHolder = null,
$resource = null;
/**
* Connects to the database.
*
* @throws <b>sfDatabaseException</b> If a connection could not be created
*/
abstract function connect();
/**
* Retrieves the database connection associated with this sfDatabase implementation.
*
* When this is executed on a Database implementation that isn't an
* abstraction layer, a copy of the resource will be returned.
*
* @return mixed A database connection
*
* @throws <b>sfDatabaseException</b> If a connection could not be retrieved
*/
public function getConnection()
{
if ($this->connection == null)
{
$this->connect();
}
return $this->connection;
}
/**
* Retrieves a raw database resource associated with this sfDatabase implementation.
*
* @return mixed A database resource
*
* @throws <b>sfDatabaseException</b> If a resource could not be retrieved
*/
public function getResource()
{
if ($this->resource == null)
{
$this->connect();
}
return $this->resource;
}
/**
* Initializes this sfDatabase object.
*
* @param array An associative array of initialization parameters
*
* @return bool true, if initialization completes successfully, otherwise false
*
* @throws <b>sfInitializationException</b> If an error occurs while initializing this sfDatabase object
*/
public function initialize($parameters = array())
{
$this->parameterHolder = new sfParameterHolder();
$this->parameterHolder->add($parameters);
}
/**
* Gets the parameter holder for this object.
*
* @return sfParameterHolder A sfParameterHolder instance
*/
public function getParameterHolder()
{
return $this->parameterHolder;
}
/**
* Gets the parameter associated with the given key.
*
* This is a shortcut for:
*
* <code>$this->getParameterHolder()->get()</code>
*
* @param string The key name
* @param string The default value
* @param string The namespace to use
*
* @return string The value associated with the key
*
* @see sfParameterHolder
*/
public function getParameter($name, $default = null, $ns = null)
{
return $this->parameterHolder->get($name, $default, $ns);
}
/**
* Returns true if the given key exists in the parameter holder.
*
* This is a shortcut for:
*
* <code>$this->getParameterHolder()->has()</code>
*
* @param string The key name
* @param string The namespace to use
*
* @return boolean true if the given key exists, false otherwise
*
* @see sfParameterHolder
*/
public function hasParameter($name, $ns = null)
{
return $this->parameterHolder->has($name, $ns);
}
/**
* Sets the value for the given key.
*
* This is a shortcut for:
*
* <code>$this->getParameterHolder()->set()</code>
*
* @param string The key name
* @param string The value
* @param string The namespace to use
*
* @see sfParameterHolder
*/
public function setParameter($name, $value, $ns = null)
{
$this->parameterHolder->set($name, $value, $ns);
}
/**
* Executes the shutdown procedure.
*
* @return void
*
* @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database
*/
abstract function shutdown();
}

View File

@ -0,0 +1,79 @@
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
* (c) 2004-2006 Sean Kerr.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfDatabaseManager allows you to setup your database connectivity before the
* request is handled. This eliminates the need for a filter to manage database
* connections.
*
* @package symfony
* @subpackage database
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Sean Kerr <skerr@mojavi.org>
* @version SVN: $Id: sfDatabaseManager.class.php 3210 2007-01-10 20:28:16Z fabien $
*/
class sfDatabaseManager
{
protected
$databases = array();
/**
* Retrieves the database connection associated with this sfDatabase implementation.
*
* @param string A database name
*
* @return mixed A Database instance
*
* @throws <b>sfDatabaseException</b> If the requested database name does not exist
*/
public function getDatabase($name = 'default')
{
if (isset($this->databases[$name]))
{
return $this->databases[$name];
}
// nonexistent database name
$error = 'Database "%s" does not exist';
$error = sprintf($error, $name);
throw new sfDatabaseException($error);
}
/**
* Initializes this sfDatabaseManager object
*
* @return bool true, if initialization completes successfully, otherwise false
*
* @throws <b>sfInitializationException</b> If an error occurs while initializing this sfDatabaseManager object
*/
public function initialize()
{
// load database configuration
require(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_config_dir_name').'/databases.yml'));
}
/**
* Executes the shutdown procedure
*
* @return void
*
* @throws <b>sfDatabaseException</b> If an error occurs while shutting down this DatabaseManager
*/
public function shutdown()
{
// loop through databases and shutdown connections
foreach ($this->databases as $database)
{
$database->shutdown();
}
}
}

View File

@ -0,0 +1,166 @@
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
* (c) 2004-2006 Sean Kerr.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfMySQLDatabase provides connectivity for the MySQL brand database.
*
* <b>Optional parameters:</b>
*
* # <b>database</b> - [none] - The database name.
* # <b>host</b> - [localhost] - The database host.
* # <b>method</b> - [normal] - How to read connection parameters.
* Possible values are normal, server, and
* env. The normal method reads them from
* the specified values. server reads them
* from $_SERVER where the keys to retrieve
* the values are what you specify the value
* as in the settings. env reads them from
* $_ENV and works like $_SERVER.
* # <b>password</b> - [none] - The database password.
* # <b>persistent</b> - [No] - Indicates that the connection should be
* persistent.
* # <b>username</b> - [none] - The database username.
*
* @package symfony
* @subpackage database
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Sean Kerr <skerr@mojavi.org>
* @version SVN: $Id: sfMySQLDatabase.class.php 3329 2007-01-23 08:29:34Z fabien $
*/
class sfMySQLDatabase extends sfDatabase
{
/**
* Connects to the database.
*
* @throws <b>sfDatabaseException</b> If a connection could not be created
*/
public function connect()
{
// determine how to get our
$method = $this->getParameter('method', 'normal');
switch ($method)
{
case 'normal':
// get parameters normally
$database = $this->getParameter('database');
$host = $this->getParameter('host', 'localhost');
$password = $this->getParameter('password');
$username = $this->getParameter('username');
break;
case 'server':
// construct a connection string from existing $_SERVER values
// and extract them to local scope
$parameters =& $this->loadParameters($_SERVER);
extract($parameters);
break;
case 'env':
// construct a connection string from existing $_ENV values
// and extract them to local scope
$string =& $this->loadParameters($_ENV);
extract($parameters);
break;
default:
// who knows what the user wants...
$error = 'Invalid MySQLDatabase parameter retrieval method "%s"';
$error = sprintf($error, $method);
throw new sfDatabaseException($error);
}
// let's see if we need a persistent connection
$persistent = $this->getParameter('persistent', false);
$connect = ($persistent) ? 'mysql_pconnect' : 'mysql_connect';
if ($password == null)
{
if ($username == null)
{
$this->connection = @$connect($host);
}
else
{
$this->connection = @$connect($host, $username);
}
}
else
{
$this->connection = @$connect($host, $username, $password);
}
// make sure the connection went through
if ($this->connection === false)
{
// the connection's foobar'd
$error = 'Failed to create a MySQLDatabase connection';
throw new sfDatabaseException($error);
}
// select our database
if ($database != null && !@mysql_select_db($database, $this->connection))
{
// can't select the database
$error = 'Failed to select MySQLDatabase "%s"';
$error = sprintf($error, $database);
throw new sfDatabaseException($error);
}
// since we're not an abstraction layer, we copy the connection
// to the resource
$this->resource = $this->connection;
}
/**
* Loads connection parameters from an existing array.
*
* @return array An associative array of connection parameters
*/
protected function & loadParameters(&$array)
{
// list of available parameters
$available = array('database', 'host', 'password', 'user');
$parameters = array();
foreach ($available as $parameter)
{
$$parameter = $this->getParameter($parameter);
$parameters[$parameter] = ($$parameter != null) ? $array[$$parameter] : null;
}
return $parameters;
}
/**
* 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)
{
@mysql_close($this->connection);
}
}
}

View File

@ -0,0 +1,88 @@
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
* (c) 2004-2006 Sean Kerr.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfPDODatabase provides connectivity for the PDO database abstraction layer.
*
* @package symfony
* @subpackage database
* @author Daniel Swarbrick (daniel@pressure.net.nz)
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Sean Kerr <skerr@mojavi.org>
* @version SVN: $Id: sfPDODatabase.class.php 3210 2007-01-10 20:28:16Z fabien $
*/
class sfPDODatabase extends sfDatabase
{
/**
* Connects to the database.
*
* @throws <b>sfDatabaseException</b> If a connection could not be created
*/
public function connect()
{
// determine how to get our parameters
$method = $this->getParameter('method', 'dsn');
// get parameters
switch ($method)
{
case 'dsn':
$dsn = $this->getParameter('dsn');
if ($dsn == null)
{
// missing required dsn parameter
$error = 'Database configuration specifies method "dsn", but is missing dsn parameter';
throw new sfDatabaseException($error);
}
break;
}
try
{
$pdo_username = $this->getParameter('username');
$pdo_password = $this->getParameter('password');
$this->connection = new PDO($dsn, $pdo_username, $pdo_password);
}
catch (PDOException $e)
{
throw new sfDatabaseException($e->getMessage());
}
// lets generate exceptions instead of silent failures
if (defined('PDO::ATTR_ERRMODE'))
{
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
else
{
$this->connection->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);
}
}
/**
* Executes 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;
}
}
}

View File

@ -0,0 +1,147 @@
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
* (c) 2004-2006 Sean Kerr.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfPostgreSQLDatabase provides connectivity for the PostgreSQL brand database.
*
* <b>Optional parameters:</b>
*
* # <b>database</b> - [none] - The database name.
* # <b>host</b> - [localhost] - The database host.
* # <b>method</b> - [normal] - How to read connection parameters.
* Possible values are normal, server, and
* env. The normal method reads them from
* the specified values. server reads them
* from $_SERVER where the keys to retrieve
* the values are what you specify the value
* as in the settings. env reads them from
* $_ENV and works like $_SERVER.
* # <b>password</b> - [none] - The database password.
* # <b>persistent</b> - [No] - Indicates that the connection should be
* persistent.
* # <b>port</b> - [none] - TCP/IP port on which PostgreSQL is
* listening.
* # <b>username</b> - [none] - The database username.
*
* @package symfony
* @subpackage database
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Sean Kerr <skerr@mojavi.org>
* @version SVN: $Id: sfPostgreSQLDatabase.class.php 3210 2007-01-10 20:28:16Z fabien $
*/
class sfPostgreSQLDatabase extends sfDatabase
{
/**
* Connects to the database.
*
* @throws <b>sfDatabaseException</b> If a connection could not be created
*/
public function connect()
{
// determine how to get our parameters
$method = $this->getParameter('method', 'normal');
// get parameters
switch ($method)
{
case 'normal':
// get parameters normally
$database = $this->getParameter('database');
$host = $this->getParameter('host');
$password = $this->getParameter('password');
$port = $this->getParameter('port');
$username = $this->getParameter('username');
// construct connection string
$string = ($database != null ? (' dbname=' .$database) : '').
($host != null ? (' host=' .$host) : '').
($password != null ? (' password=' .$password) : '').
($port != null ? (' port=' .$port) : '').
($username != null ? (' user=' .$username) : '');
break;
case 'server':
// construct a connection string from existing $_SERVER values
$string = $this->loadParameters($_SERVER);
break;
case 'env':
// construct a connection string from existing $_ENV values
$string = $this->loadParameters($_ENV);
break;
default:
// who knows what the user wants...
$error = 'Invalid PostgreSQLDatabase parameter retrieval method "%s"';
$error = sprintf($error, $method);
throw new sfDatabaseException($error);
}
// let's see if we need a persistent connection
$persistent = $this->getParameter('persistent', false);
$connect = $persistent ? 'pg_pconnect' : 'pg_connect';
$this->connection = @$connect($string);
// make sure the connection went through
if ($this->connection === false)
{
// the connection's foobar'd
$error = 'Failed to create a PostgreSQLDatabase connection';
throw new sfDatabaseException($error);
}
// since we're not an abstraction layer, we copy the connection
// to the resource
$this->resource = $this->connection;
}
/**
* Loads connection parameters from an existing array.
*
* @return string A connection string
*/
protected function loadParameters(&$array)
{
$database = $this->getParameter('database');
$host = $this->getParameter('host');
$password = $this->getParameter('password');
$port = $this->getParameter('port');
$username = $this->getParameter('username');
// construct connection string
$string = ($database != null ? (' dbname=' .$array[$database]) : '').
($host != null ? (' host=' .$array[$host]) : '').
($password != null ? (' password='.$array[$password]) : '').
($port != null ? (' port=' .$array[$port]) : '').
($username != null ? (' user=' .$array[$username]) : '');
return $string;
}
/**
* Executes the shutdown procedure.
*
* @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database
*/
public function shutdown()
{
if ($this->connection != null)
{
@pg_close($this->connection);
}
}
}