diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Application.php | 2 | ||||
-rw-r--r-- | src/Database/DatabaseServiceProvider.php | 38 | ||||
-rw-r--r-- | src/Database/Db.php | 30 |
3 files changed, 39 insertions, 31 deletions
diff --git a/src/Application.php b/src/Application.php index c9023c7b..68ce9e33 100644 --- a/src/Application.php +++ b/src/Application.php @@ -5,6 +5,7 @@ namespace Engelsystem; use Engelsystem\Config\Config; use Engelsystem\Container\Container; use Engelsystem\Container\ServiceProvider; +use Illuminate\Container\Container as IlluminateContainer; use Psr\Container\ContainerInterface; class Application extends Container @@ -44,6 +45,7 @@ class Application extends Container $this->instance('container', $this); $this->instance(Container::class, $this); $this->instance(Application::class, $this); + $this->instance(IlluminateContainer::class, $this); $this->bind(ContainerInterface::class, Application::class); } diff --git a/src/Database/DatabaseServiceProvider.php b/src/Database/DatabaseServiceProvider.php index 49fb4af5..306ffaca 100644 --- a/src/Database/DatabaseServiceProvider.php +++ b/src/Database/DatabaseServiceProvider.php @@ -4,23 +4,39 @@ namespace Engelsystem\Database; use Engelsystem\Container\ServiceProvider; use Exception; -use PDO; +use Illuminate\Database\Capsule\Manager as CapsuleManager; +use Illuminate\Events\Dispatcher as EventsDispatcher; class DatabaseServiceProvider extends ServiceProvider { public function register() { $config = $this->app->get('config'); - Db::connect( - '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); - Db::getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + $capsule = $this->app->make(CapsuleManager::class); + + $dbConfig = $config->get('database'); + $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); } /** diff --git a/src/Database/Db.php b/src/Database/Db.php index 114bd8fc..aa9b1a27 100644 --- a/src/Database/Db.php +++ b/src/Database/Db.php @@ -2,14 +2,14 @@ namespace Engelsystem\Database; +use Illuminate\Database\Capsule\Manager as CapsuleManager; use PDO; -use PDOException; use PDOStatement; class Db { - /** @var PDO */ - protected static $db; + /** @var CapsuleManager */ + protected static $dbManager; /** @var PDOStatement */ protected static $stm = null; @@ -18,23 +18,13 @@ class Db protected static $lastStatus = true; /** - * Connect to database + * Set the database connection manager * - * @param string $dsn - * @param string $username - * @param string $password - * @param array $options - * @return bool + * @param CapsuleManager $dbManager */ - public static function connect($dsn, $username = null, $password = null, $options = []) + public static function setDbManager($dbManager) { - try { - self::$db = new PDO($dsn, $username, $password, $options); - } catch (PDOException $e) { - return false; - } - - return true; + self::$dbManager = $dbManager; } /** @@ -46,7 +36,7 @@ class Db */ 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); return self::$stm; @@ -60,7 +50,7 @@ class Db */ public static function unprepared($query) { - self::$stm = self::$db->query($query); + self::$stm = self::getPdo()->query($query); self::$lastStatus = (self::$stm instanceof PDOStatement); return self::$lastStatus; @@ -175,7 +165,7 @@ class Db */ public static function getPdo() { - return self::$db; + return self::$dbManager->getConnection()->getPdo(); } /** |