Added Illuminate/Database

This commit is contained in:
Igor Scheller 2018-01-14 01:48:50 +01:00
parent e0b58d2a7d
commit d4b36e9bad
7 changed files with 81 additions and 45 deletions

View File

@ -17,6 +17,7 @@
"php": ">=7.0.0", "php": ">=7.0.0",
"erusev/parsedown": "^1.6", "erusev/parsedown": "^1.6",
"illuminate/container": "5.5.*", "illuminate/container": "5.5.*",
"illuminate/database": "5.5.*",
"illuminate/support": "^5.5", "illuminate/support": "^5.5",
"psr/container": "^1.0", "psr/container": "^1.0",
"psr/log": "^1.0", "psr/log": "^1.0",

View File

@ -6,9 +6,9 @@ return [
// MySQL-Connection Settings // MySQL-Connection Settings
'database' => [ 'database' => [
'host' => env('MYSQL_HOST', (env('CI', false) ? 'mariadb' : 'localhost')), 'host' => env('MYSQL_HOST', (env('CI', false) ? 'mariadb' : 'localhost')),
'user' => env('MYSQL_USER', 'root'), 'database' => env('MYSQL_DATABASE', 'engelsystem'),
'pw' => env('MYSQL_PASSWORD', ''), 'username' => env('MYSQL_USER', 'root'),
'db' => env('MYSQL_DATABASE', 'engelsystem'), 'password' => env('MYSQL_PASSWORD', ''),
], ],
// For accessing stats // For accessing stats

View File

@ -5,6 +5,7 @@ namespace Engelsystem;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Container\Container; use Engelsystem\Container\Container;
use Engelsystem\Container\ServiceProvider; use Engelsystem\Container\ServiceProvider;
use Illuminate\Container\Container as IlluminateContainer;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
class Application extends Container class Application extends Container
@ -44,6 +45,7 @@ class Application extends Container
$this->instance('container', $this); $this->instance('container', $this);
$this->instance(Container::class, $this); $this->instance(Container::class, $this);
$this->instance(Application::class, $this); $this->instance(Application::class, $this);
$this->instance(IlluminateContainer::class, $this);
$this->bind(ContainerInterface::class, Application::class); $this->bind(ContainerInterface::class, Application::class);
} }

View File

@ -4,23 +4,39 @@ namespace Engelsystem\Database;
use Engelsystem\Container\ServiceProvider; use Engelsystem\Container\ServiceProvider;
use Exception; use Exception;
use PDO; use Illuminate\Database\Capsule\Manager as CapsuleManager;
use Illuminate\Events\Dispatcher as EventsDispatcher;
class DatabaseServiceProvider extends ServiceProvider class DatabaseServiceProvider extends ServiceProvider
{ {
public function register() public function register()
{ {
$config = $this->app->get('config'); $config = $this->app->get('config');
Db::connect( $capsule = $this->app->make(CapsuleManager::class);
'mysql:host=' . $config->get('database')['host']
. ';dbname=' . $config->get('database')['db']
. ';charset=utf8',
$config->get('database')['user'],
$config->get('database')['pw']
) || $this->exitOnError();
Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbConfig = $config->get('database');
Db::getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $capsule->addConnection(array_merge([
'driver' => 'mysql',
'host' => '',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], $dbConfig));
$capsule->setAsGlobal();
$capsule->bootEloquent();
try {
$capsule->getConnection()->getPdo();
} catch (\PDOException $e) {
$this->exitOnError();
}
$this->app->instance('db', $capsule);
Db::setDbManager($capsule);
} }
/** /**

View File

@ -2,14 +2,14 @@
namespace Engelsystem\Database; namespace Engelsystem\Database;
use Illuminate\Database\Capsule\Manager as CapsuleManager;
use PDO; use PDO;
use PDOException;
use PDOStatement; use PDOStatement;
class Db class Db
{ {
/** @var PDO */ /** @var CapsuleManager */
protected static $db; protected static $dbManager;
/** @var PDOStatement */ /** @var PDOStatement */
protected static $stm = null; protected static $stm = null;
@ -18,23 +18,13 @@ class Db
protected static $lastStatus = true; protected static $lastStatus = true;
/** /**
* Connect to database * Set the database connection manager
* *
* @param string $dsn * @param CapsuleManager $dbManager
* @param string $username
* @param string $password
* @param array $options
* @return bool
*/ */
public static function connect($dsn, $username = null, $password = null, $options = []) public static function setDbManager($dbManager)
{ {
try { self::$dbManager = $dbManager;
self::$db = new PDO($dsn, $username, $password, $options);
} catch (PDOException $e) {
return false;
}
return true;
} }
/** /**
@ -46,7 +36,7 @@ class Db
*/ */
public static function query($query, array $bindings = []) public static function query($query, array $bindings = [])
{ {
self::$stm = self::$db->prepare($query); self::$stm = self::getPdo()->prepare($query);
self::$lastStatus = self::$stm->execute($bindings); self::$lastStatus = self::$stm->execute($bindings);
return self::$stm; return self::$stm;
@ -60,7 +50,7 @@ class Db
*/ */
public static function unprepared($query) public static function unprepared($query)
{ {
self::$stm = self::$db->query($query); self::$stm = self::getPdo()->query($query);
self::$lastStatus = (self::$stm instanceof PDOStatement); self::$lastStatus = (self::$stm instanceof PDOStatement);
return self::$lastStatus; return self::$lastStatus;
@ -175,7 +165,7 @@ class Db
*/ */
public static function getPdo() public static function getPdo()
{ {
return self::$db; return self::$dbManager->getConnection()->getPdo();
} }
/** /**

View File

@ -25,9 +25,9 @@ class DatabaseServiceProviderTest extends ServiceProviderTest
$this->setExpects($app, 'get', ['config'], $config); $this->setExpects($app, 'get', ['config'], $config);
$this->setExpects($config, 'get', ['database'], [ $this->setExpects($config, 'get', ['database'], [
'host' => 'localhost', 'host' => 'localhost',
'db' => 'database', 'database' => 'database',
'user' => 'user', 'username' => 'user',
'pw' => 'password', 'password' => 'password',
], $this->atLeastOnce()); ], $this->atLeastOnce());
$this->expectException(Exception::class); $this->expectException(Exception::class);

View File

@ -3,24 +3,46 @@
namespace Engelsystem\Test\Unit\Database; namespace Engelsystem\Test\Unit\Database;
use Engelsystem\Database\Db; use Engelsystem\Database\Db;
use Illuminate\Database\Capsule\Manager as CapsuleManager;
use Illuminate\Database\Connection as DatabaseConnection;
use PDO; use PDO;
use PDOStatement; use PDOStatement;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
use ReflectionObject; use ReflectionObject;
use Throwable; use Throwable;
class DbTest extends TestCase class DbTest extends TestCase
{ {
/** /**
* @covers \Engelsystem\Database\Db::connect() * @covers \Engelsystem\Database\Db::setDbManager()
*/ */
public function testConnect() public function testSetDbManager()
{ {
$result = Db::connect('mysql:host=localhost;dbname=someTestDatabaseThatDoesNotExist;charset=utf8'); /** @var MockObject|Pdo $pdo */
$this->assertFalse($result); $pdo = $this->getMockBuilder(Pdo::class)
->disableOriginalConstructor()
->getMock();
/** @var MockObject|CapsuleManager $dbManager */
$dbManager = $this->getMockBuilder(CapsuleManager::class)
->disableOriginalConstructor()
->getMock();
/** @var MockObject|DatabaseConnection $dbManager */
$databaseConnection = $this->getMockBuilder(DatabaseConnection::class)
->disableOriginalConstructor()
->getMock();
$result = Db::connect('sqlite::memory:'); $dbManager
$this->assertTrue($result); ->expects($this->atLeastOnce())
->method('getConnection')
->willReturn($databaseConnection);
$databaseConnection
->expects($this->atLeastOnce())
->method('getPdo')
->willReturn($pdo);
Db::setDbManager($dbManager);
$this->assertEquals($pdo, Db::getPdo());
} }
/** /**
@ -167,7 +189,12 @@ class DbTest extends TestCase
*/ */
protected function setUp() protected function setUp()
{ {
Db::connect('sqlite::memory:'); $dbManager = new CapsuleManager();
$dbManager->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
$dbManager->setAsGlobal();
$dbManager->bootEloquent();
Db::setDbManager($dbManager);
Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Db::query( Db::query(
' '