php-censor/src/PHPCensor/Plugin/Sqlite.php

85 lines
1.9 KiB
PHP
Raw Normal View History

2014-05-09 18:41:26 +02:00
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2014, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
2014-05-09 18:41:26 +02:00
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Plugin;
2014-05-09 18:41:26 +02:00
use PDO;
2016-07-19 20:28:11 +02:00
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
2014-05-09 18:41:26 +02:00
/**
* SQLite Plugin Provides access to a SQLite database.
* @author Corpsee <poisoncorpsee@gmail.com>
* @package PHPCI
* @subpackage Plugins
*/
2016-05-09 08:20:26 +02:00
class Sqlite implements Plugin
2014-05-09 18:41:26 +02:00
{
/**
2016-07-19 20:28:11 +02:00
* @var \PHPCensor\Builder
2014-05-09 18:41:26 +02:00
*/
protected $phpci;
/**
* @var \PHPCensor\Model\Build
2014-05-09 18:41:26 +02:00
*/
protected $build;
/**
* @var array
*/
2016-04-21 06:58:09 +02:00
protected $queries = [];
2014-05-09 18:41:26 +02:00
/**
* @var string
*/
protected $path;
/**
* @param Builder $phpci
* @param Build $build
* @param array $options
*/
2016-04-21 06:58:09 +02:00
public function __construct(Builder $phpci, Build $build, array $options = [])
2014-05-09 18:41:26 +02:00
{
$this->phpci = $phpci;
$this->build = $build;
$this->queries = $options;
$buildSettings = $phpci->getConfig('build_settings');
if (isset($buildSettings['sqlite'])) {
$sql = $buildSettings['sqlite'];
$this->path = $sql['path'];
}
$this->phpci->logDebug('Plugin options: ' . json_encode($options));
2014-05-09 18:41:26 +02:00
}
/**
* Connects to SQLite and runs a specified set of queries.
* @return boolean
*/
public function execute()
{
try {
2016-04-21 06:58:09 +02:00
$opts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
$pdo = new PDO('sqlite:' . $this->path, $opts);
2014-05-09 18:41:26 +02:00
foreach ($this->queries as $query) {
$pdo->query($this->phpci->interpolate($query));
2014-05-09 18:41:26 +02:00
}
} catch (\Exception $ex) {
$this->phpci->logFailure($ex->getMessage());
return false;
}
return true;
}
}