Skip to content

Instantly share code, notes, and snippets.

@mcrumm
Last active December 10, 2015 22:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcrumm/4500342 to your computer and use it in GitHub Desktop.
Save mcrumm/4500342 to your computer and use it in GitHub Desktop.
A glue object for exposing Phinx's environment-specific db connection settings. I use it to keep from duplicating my db connection settings in Phinx and Idiorm/Paris.
<?php
use Symfony\Component\Yaml\Parser;
class PhinxConfig implements ArrayAccess
{
protected $environment;
protected $path;
protected $config;
public function __construct($path = '', $environment = 'development')
{
$this->environment($environment);
$this->path($path);
$this->parse();
}
protected function config($config = array())
{
$this->config = $config;
return $this;
}
public function environment($env = '')
{
if(empty($env)) {
return $this->environment;
}
$this->environment = $env;
}
public function path($path = '')
{
if(empty($path)) {
return $this->path;
}
$this->path = $path;
}
public function parse()
{
if( $this->path() == '' ) {
throw new Exception('Path to config not set.');
}
$yaml = new Parser();
$config = $yaml->parse(file_get_contents($this->path()));
$this->config($config['environments'][$this->environment()]);
}
public function offsetExists($offset)
{
return array_key_exists($offset, $this->config);
}
public function offsetGet($offset)
{
return $this->config[$offset];
}
public function offsetSet($offset, $value)
{
$this->config[$offset] = $value;
}
public function offsetUnset($offset)
{
unset($this->config[$offset]);
}
}
<?php
$config = new PhinxConfig('/path/to/phinx.yml', 'development');
// Use Phinx's config settings with Idiorm
ORM::configure('mysql:host='.$config['host'].';port='.$config['port'].';dbname='.$config['name']);
ORM::configure('username', $config['user']);
ORM::configure('password', $config['pass']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment