Skip to content

Instantly share code, notes, and snippets.

@cxj
Last active August 29, 2015 14:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cxj/1e222c84d122f24e7d1f to your computer and use it in GitHub Desktop.
Abstract super class for boiler plate creation of Aura\SqlMapper_Bundle table-unique classes.
<?php
/**
* @file AbstractPdoSource.php
*/
namespace Clx\DataSources;
require_once($_SERVER['DOCUMENT_ROOT'] . '/s/vendor/autoload.php'); // Composer-generated
// Proprietary stuff.
require_once "pdo_db.inc";
use Clx\Db\Pdo as Db;
use Aura\SqlMapper_Bundle\AbstractMapper;
use Aura\SqlMapper_Bundle\Query\ConnectedQueryFactory;
use Aura\SqlQuery\QueryFactory;
use Aura\Sql\ConnectionLocator;
use Aura\Sql\ExtendedPdo;
use Aura\Sql\Profiler;
/**
* Class AbstractPdoSource
* @package Clx\DataSources
*/
abstract class AbstractPdoSource
{
/**
* @var string
*/
protected $config;
/**
* @var \Aura\Sql\ConnectionLocator
*/
protected $connectionLocator;
/**
* @var \Aura\SqlMapper_Bundle\AbstractGateway
*/
protected $gateway = null;
/**
* @param ConnectedQueryFactory $q
* @return \Aura\SqlMapper_Bundle\AbstractGateway
*/
abstract protected function myGateway(ConnectedQueryFactory $q);
/**
* @return \Aura\SqlMapper_Bundle\AbstractMapper
*/
abstract protected function myMapper();
/**
* @param string $config The lynx.ini section name for DB to be used.
*/
public function __construct($config)
{
$this->config = $config;
}
/**
* Sets the connectionLocator attribute.
* Kludged to use the legacy db.inc/pdo_db.inc DB connection stuff.
*/
protected function getLocator()
{
$database = $this->config;
$connectionLocator = new ConnectionLocator(function() use ($database) {
// Lazily connected when locator is used by mapper through gateway.
$dsn = Db\db_connect($database, 'psql'); // TODO, localize.
if (!is_object($dsn)) {
// Error, cannot connect to DB.
dayfile('debug', "Unable to open database: '$database'"); // TODO, localize.
my_exit('Unable to open database'); // TODO, localize.
}
$pdo = new ExtendedPdo($dsn);
$pdo->setProfiler(new Profiler);
$pdo->getProfiler()->setActive(true);
return $pdo;
});
$this->connectionLocator = $connectionLocator;
}
public function initGateway()
{
$this->getLocator(); // sets $this->connectionLocator
// Set up gateway.
$query = new ConnectedQueryFactory(new QueryFactory('pgsql'));
$this->gateway = $this->myGateway($query);
}
/**
* @return AbstractMapper
*/
public function getMapper()
{
// Make sure gateway is initialized.
if (!is_object($this->gateway)) {
$this->initGateway();
}
// Set up mapper.
$mapper = $this->myMapper();
return $mapper;
}
/**
* @return array - SQL debug info from Aura\Sql\ExtendedPdo
*/
public function getProfile()
{
$profiles = $this->connectionLocator->getDefault()->getProfiler()->getProfiles();
return $profiles;
}
/**
* @return string - Multi-line nicely formatted text for use in log, etc.
*/
public function formatProfile()
{
$profiles = $this->getProfile();
$line = array();
foreach ($profiles as $key => $profile) {
$line[] = "Profile entry # $key";
$line[] = " Statement: {$profile['statement']}";
$line[] = " Bind values: " . print_r($profile['bind_values'], true);
}
return implode(PHP_EOL, $line);
}
/**
* @return \Aura\Sql\ExtendedPdoInterface
*/
public function getExtendedPdo()
{
return $this->connectionLocator->getDefault();
}
/**
* @return array PDO::errorInfo
*/
public function getError()
{
return $this->getExtendedPdo()->errorInfo();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment