Updating MySQL plugin to use PDO, and accept connection parameters from build_settings.mysql

This commit is contained in:
Dan Cryer 2013-05-16 00:51:48 +01:00
parent d4835b46fa
commit 3ac5df9eb3

View file

@ -1,29 +1,51 @@
<?php
namespace PHPCI\Plugin;
use PDO;
class Mysql implements \PHPCI\Plugin
{
protected $phpci;
protected $queries = array();
protected $host;
protected $user;
protected $pass;
public function __construct(\PHPCI\Builder $phpci, array $options = array())
{
$this->phpci = $phpci;
$this->queries = $options;
$db = \b8\Database::getConnection('write')->getDetails();
$this->host = PHPCI_DB_HOST;
$this->user = $db['user'];
$this->pass = $db['pass'];
$buildSettings = $phpci->getConfig('build_settings');
if(isset($buildSettings['mysql'])) {
$sql = $buildSettings['mysql'];
$this->host = !empty($sql['host']) ? $sql['host'] : $this->host;
$this->user = !empty($sql['user']) ? $sql['user'] : $this->user;
$this->pass = array_key_exists('pass', $sql) ? $sql['pass'] : $this->pass;
}
}
public function execute()
{
$rtn = true;
try {
$pdo = new PDO('mysql:host=' . $this->host, $this->user, $this->pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$db = \b8\Database::getConnection('write')->getDetails();
foreach($this->queries as $query)
{
$rtn = !$this->phpci->executeCommand('mysql -h'.PHPCI_DB_HOST.' -u'.$db['user'].(!empty($db['pass']) ? ' -p' . $db['pass'] : '').' -e "'.$query.'"') ? false : $rtn;
foreach($this->queries as $query) {
$pdo->query($query);
}
}
catch(\Exception $ex) {
$this->phpci->logFailure($ex->getMessage());
return false;
}
return $rtn;
return true;
}
}