Created
April 4, 2018 15:49
-
-
Save SparK-Cruz/6f117f90ca3084e522b13175077cbeec to your computer and use it in GitHub Desktop.
Connect LessQL to the same database as Phinx
This file contains hidden or 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\Config\FileLocator; | |
use Phinx\Config\Config; | |
use Phinx\Db\Adapter\AdapterFactory; | |
class Database extends \LessQL\Database { | |
const PHINX_FILE = 'phinx.yml'; | |
public function __construct($env = null) { | |
$config = $this->readPhinxConfig($env); | |
$connection = $this->savagePhinxConnection($config); | |
parent::__construct($connection); | |
} | |
private function readPhinxConfig($env) { | |
$cwd = getcwd(); | |
$locator = new FileLocator([ | |
$cwd . DIRECTORY_SEPARATOR | |
]); | |
$path = $locator->locate(static::PHINX_FILE, $cwd, $first = true); | |
$config = Config::fromYaml($path); | |
if (empty($env)) { | |
$env = $config->getDefaultEnvironment(); | |
} | |
return $config->getEnvironment($env); | |
} | |
private function savagePhinxConnection($config) { | |
return AdapterFactory::instance()->getAdapter($config['adapter'], $config)->getConnection(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment