Merge branch 'master' of github.com:Block8/PHPCI
This commit is contained in:
commit
aff5b1886e
|
@ -246,6 +246,14 @@ class Builder
|
|||
$buildId = 'project' . $this->build->getProject()->getId() . '-build' . $this->build->getId();
|
||||
$this->ciDir = realpath(dirname(__FILE__) . '/../') . '/';
|
||||
$this->buildPath = $this->ciDir . 'build/' . $buildId . '/';
|
||||
|
||||
// Setup environment vars that will be accessible during exec()
|
||||
putenv("PHPCI=1");
|
||||
putenv("PHPCI_COMMIT=".$commitId);
|
||||
putenv("PHPCI_PROJECT=".$this->build->getProject()->getId());
|
||||
putenv("PHPCI_BUILD=".$this->build->getId());
|
||||
putenv("PHPCI_PROJECT_TITLE=".$this->build->getProject()->getTitle());
|
||||
putenv("PHPCI_BUILD_PATH=".$this->buildPath);
|
||||
|
||||
// Create a working copy of the project:
|
||||
if (!$this->build->createWorkingCopy($this, $this->buildPath)) {
|
||||
|
|
51
PHPCI/Plugin/Env.php
Normal file
51
PHPCI/Plugin/Env.php
Normal file
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2013, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link http://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCI\Plugin;
|
||||
|
||||
/**
|
||||
* Environment variable plugin
|
||||
* @author Steve Kamerman <stevekamerman@gmail.com>
|
||||
* @package PHPCI
|
||||
* @subpackage Plugins
|
||||
*/
|
||||
class Env implements \PHPCI\Plugin
|
||||
{
|
||||
protected $phpci;
|
||||
protected $env_vars;
|
||||
|
||||
public function __construct(\PHPCI\Builder $phpci, array $options = array())
|
||||
{
|
||||
$this->phpci = $phpci;
|
||||
$this->env_vars = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified environment variables to the builder environment
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$success = true;
|
||||
foreach ($this->env_vars as $key => $value) {
|
||||
if (is_numeric($key)) {
|
||||
// This allows the developer to specify env vars like " - FOO=bar" or " - FOO: bar"
|
||||
$env_var = is_array($value)? key($value).'='.current($value): $value;
|
||||
} else {
|
||||
// This allows the standard syntax: "FOO: bar"
|
||||
$env_var = "$key=$value";
|
||||
}
|
||||
|
||||
if (!putenv($env_var)) {
|
||||
$success = false;
|
||||
$this->phpci->logFailure("Unable to set environment variable");
|
||||
}
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
}
|
|
@ -14,17 +14,28 @@ use PDO;
|
|||
/**
|
||||
* MySQL Plugin - Provides access to a MySQL database.
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
* @author Steve Kamerman <stevekamerman@gmail.com>
|
||||
* @package PHPCI
|
||||
* @subpackage Plugins
|
||||
*/
|
||||
class Mysql implements \PHPCI\Plugin
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \PHPCI\Builder
|
||||
*/
|
||||
protected $phpci;
|
||||
protected $queries = array();
|
||||
|
||||
protected $host;
|
||||
protected $user;
|
||||
protected $pass;
|
||||
|
||||
/**
|
||||
* Database Connection
|
||||
* @var PDO
|
||||
*/
|
||||
protected $pdo;
|
||||
|
||||
public function __construct(\PHPCI\Builder $phpci, array $options = array())
|
||||
{
|
||||
|
@ -51,18 +62,75 @@ class Mysql implements \PHPCI\Plugin
|
|||
*/
|
||||
public function execute()
|
||||
{
|
||||
$success = true;
|
||||
|
||||
try {
|
||||
$opts = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
|
||||
$pdo = new PDO('mysql:host=' . $this->host, $this->user, $this->pass, $opts);
|
||||
|
||||
$this->pdo = new PDO('mysql:host=' . $this->host, $this->user, $this->pass, $opts);
|
||||
|
||||
foreach ($this->queries as $query) {
|
||||
$pdo->query($query);
|
||||
if (!is_array($query)) {
|
||||
// Simple query
|
||||
$this->pdo->query($query);
|
||||
} else if (isset($query['import'])) {
|
||||
// SQL file execution
|
||||
$this->executeFile($query['import']);
|
||||
}
|
||||
}
|
||||
} catch (\Exception $ex) {
|
||||
$this->phpci->logFailure($ex->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
protected function executeFile($query)
|
||||
{
|
||||
if (!isset($query['file'])) {
|
||||
throw new \Exception("Import statement must contiain an 'file' key");
|
||||
}
|
||||
|
||||
$import_file = $this->phpci->buildPath . $query['file'];
|
||||
if (!is_readable($import_file)) {
|
||||
throw new \Exception("Cannot open SQL import file: $import_file");
|
||||
}
|
||||
|
||||
$database = isset($query['database'])? $query['database']: null;
|
||||
|
||||
$import_command = $this->getImportCommand($import_file, $database);
|
||||
if (!$this->phpci->executeCommand($import_command)) {
|
||||
throw new \Exception("Unable to execute SQL file");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the MySQL import command required to import/execute the specified file
|
||||
* @param string $import_file Path to file, relative to the build root
|
||||
* @param string $database If specified, this database is selected before execution
|
||||
* @return string
|
||||
*/
|
||||
protected function getImportCommand($import_file, $database=null) {
|
||||
$decompression = array(
|
||||
'bz2' => '| bzip2 --decompress',
|
||||
'gz' => '| gzip --decompress',
|
||||
);
|
||||
|
||||
$extension = strtolower(pathinfo($import_file, PATHINFO_EXTENSION));
|
||||
$decomp_cmd = '';
|
||||
if (array_key_exists($extension, $decompression)) {
|
||||
$decomp_cmd = $decompression[$extension];
|
||||
}
|
||||
|
||||
$args = array(
|
||||
':import_file' => escapeshellarg($import_file),
|
||||
':decomp_cmd' => $decomp_cmd,
|
||||
':user' => escapeshellarg($this->user),
|
||||
':pass' => escapeshellarg($this->pass),
|
||||
':database' => ($database === null)? '': escapeshellarg($database),
|
||||
);
|
||||
return strtr('cat :import_file :decomp_cmd | mysql -u:user -p:pass :database', $args);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue