Skip to content

Instantly share code, notes, and snippets.

@nbari
Last active December 18, 2015 07:48
Show Gist options
  • Save nbari/5748904 to your computer and use it in GitHub Desktop.
Save nbari/5748904 to your computer and use it in GitHub Desktop.
PPI Framework binding
<?php
namespace PPI\DataSource\Connection;
use DALMP\dalmp;
use PPI\DataSource\ConnectionInferface;
class Dalmp implements ConnectionInferface
{
protected $config = array();
protected $conns = array();
public function __construct(array $config)
{
$this->config = $config;
}
public function getConnectionByName($name)
{
if(!isset($this->config[$name])) {
throw new \Exception('No DALMP db connection found named: ' . $name);
}
if (!isset($this->conns[$name])) {
$this->conns[$name] = DB::connection($this->config[$name]);
}
return $this->conns[$name];
}
public function constructDSN($config)
{
if (!isset($config['port'])) {
$config['port'] = 3306;
}
if (!isset($config['hostname'])) {
$host = is_readable('/tmp/mysql.sock') ? 'unix_socket=\tmp\mysql.sock' : '127.0.0.1' . ':' . $config['port'];
} else {
$host = $config['hostname'] .':'. $config['port']
}
return new DAMP(sprintf('%s://%s:%s@%s/%s', $config['charset'], $config['username'], $config['password'], $host, $config['database']));
}
public function supports($library)
{
return $library === 'dalmp';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment