Last active
December 10, 2015 22:08
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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