Skip to content

Instantly share code, notes, and snippets.

@SparK-Cruz
Created April 4, 2018 15:49
Show Gist options
  • Save SparK-Cruz/6f117f90ca3084e522b13175077cbeec to your computer and use it in GitHub Desktop.
Save SparK-Cruz/6f117f90ca3084e522b13175077cbeec to your computer and use it in GitHub Desktop.
Connect LessQL to the same database as Phinx
<?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