php-censor/src/Plugin/Sqlite.php

70 lines
1.5 KiB
PHP
Raw Permalink Normal View History

2014-05-09 18:41:26 +02:00
<?php
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.
*
2017-03-04 16:39:56 +01:00
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
2016-07-11 18:00:04 +02:00
class Sqlite extends Plugin
2014-05-09 18:41:26 +02:00
{
/**
* @var array
*/
2016-04-21 06:58:09 +02:00
protected $queries = [];
2014-05-09 18:41:26 +02:00
/**
* @var string
*/
protected $path;
2017-01-11 16:15:54 +01:00
/**
* @return string
*/
public static function pluginName()
{
return 'sqlite';
}
2014-05-09 18:41:26 +02:00
/**
2016-07-11 18:00:04 +02:00
* {@inheritdoc}
2014-05-09 18:41:26 +02:00
*/
public function __construct(Builder $builder, Build $build, array $options = [])
2014-05-09 18:41:26 +02:00
{
parent::__construct($builder, $build, $options);
2016-07-11 18:00:04 +02:00
$buildSettings = $this->builder->getConfig('build_settings');
2014-05-09 18:41:26 +02:00
if (isset($buildSettings['sqlite'])) {
2016-07-11 18:00:04 +02:00
$sql = $buildSettings['sqlite'];
2014-05-09 18:41:26 +02:00
$this->path = $sql['path'];
}
}
/**
* 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->builder->interpolate($query));
2014-05-09 18:41:26 +02:00
}
} catch (\Exception $ex) {
$this->builder->logFailure($ex->getMessage());
2014-05-09 18:41:26 +02:00
return false;
}
return true;
}
}