diff options
Diffstat (limited to 'src/Database')
-rw-r--r-- | src/Database/Database.php | 98 | ||||
-rw-r--r-- | src/Database/DatabaseServiceProvider.php | 16 | ||||
-rw-r--r-- | src/Database/Db.php | 1 | ||||
-rw-r--r-- | src/Database/Migration/Migrate.php | 40 | ||||
-rw-r--r-- | src/Database/Migration/MigrationServiceProvider.php | 11 |
5 files changed, 140 insertions, 26 deletions
diff --git a/src/Database/Database.php b/src/Database/Database.php new file mode 100644 index 00000000..407a8bf9 --- /dev/null +++ b/src/Database/Database.php @@ -0,0 +1,98 @@ +<?php + +namespace Engelsystem\Database; + +use Illuminate\Database\Connection as DatabaseConnection; +use PDO; + +class Database +{ + /** @var DatabaseConnection */ + protected $connection; + + /** + * @param DatabaseConnection $connection + */ + public function __construct(DatabaseConnection $connection) + { + $this->connection = $connection; + } + + /** + * Run a select query + * + * @param string $query + * @param array $bindings + * @return object[] + */ + public function select($query, array $bindings = []) + { + return $this->connection->select($query, $bindings); + } + + /** + * Run a select query and return only the first result or null if no result is found. + * + * @param string $query + * @param array $bindings + * @return object|null + */ + public function selectOne($query, array $bindings = []) + { + return $this->connection->selectOne($query, $bindings); + } + + /** + * Run an insert query + * + * @param string $query + * @param array $bindings + * @return bool + */ + public function insert($query, array $bindings = []) + { + return $this->connection->insert($query, $bindings); + } + + /** + * Run an update query + * + * @param string $query + * @param array $bindings + * @return int + */ + public function update($query, array $bindings = []) + { + return $this->connection->update($query, $bindings); + } + + /** + * Run a delete query + * + * @param string $query + * @param array $bindings + * @return int + */ + public function delete($query, array $bindings = []) + { + return $this->connection->delete($query, $bindings); + } + + /** + * Get the PDO instance + * + * @return PDO + */ + public function getPdo() + { + return $this->connection->getPdo(); + } + + /** + * @return DatabaseConnection + */ + public function getConnection() + { + return $this->connection; + } +} diff --git a/src/Database/DatabaseServiceProvider.php b/src/Database/DatabaseServiceProvider.php index 7328bc4e..b3c33588 100644 --- a/src/Database/DatabaseServiceProvider.php +++ b/src/Database/DatabaseServiceProvider.php @@ -5,6 +5,7 @@ namespace Engelsystem\Database; use Engelsystem\Container\ServiceProvider; use Exception; use Illuminate\Database\Capsule\Manager as CapsuleManager; +use Illuminate\Database\Connection as DatabaseConnection; use PDOException; class DatabaseServiceProvider extends ServiceProvider @@ -30,14 +31,25 @@ class DatabaseServiceProvider extends ServiceProvider $capsule->bootEloquent(); $capsule->getConnection()->useDefaultSchemaGrammar(); + $pdo = null; try { - $capsule->getConnection()->getPdo(); + $pdo = $capsule->getConnection()->getPdo(); } catch (PDOException $e) { $this->exitOnError(); } - $this->app->instance('db', $capsule); + $this->app->instance(CapsuleManager::class, $capsule); + $this->app->instance(Db::class, $capsule); Db::setDbManager($capsule); + + $connection = $capsule->getConnection(); + $this->app->instance(DatabaseConnection::class, $connection); + + $database = $this->app->make(Database::class); + $this->app->instance(Database::class, $database); + $this->app->instance('db', $database); + $this->app->instance('db.pdo', $pdo); + $this->app->instance('db.connection', $connection); } /** diff --git a/src/Database/Db.php b/src/Database/Db.php index c0871e68..d007f1e5 100644 --- a/src/Database/Db.php +++ b/src/Database/Db.php @@ -6,6 +6,7 @@ use Illuminate\Database\Capsule\Manager as CapsuleManager; use Illuminate\Database\Connection as DatabaseConnection; use PDO; +/** @deprecated */ class Db { /** @var CapsuleManager */ diff --git a/src/Database/Migration/Migrate.php b/src/Database/Migration/Migrate.php index 3a08bb6e..9c6d3e43 100644 --- a/src/Database/Migration/Migrate.php +++ b/src/Database/Migration/Migrate.php @@ -18,7 +18,7 @@ class Migrate protected $app; /** @var SchemaBuilder */ - protected $scheme; + protected $schema; /** @var callable */ protected $output; @@ -29,13 +29,13 @@ class Migrate /** * Migrate constructor * - * @param SchemaBuilder $scheme + * @param SchemaBuilder $schema * @param Application $app */ - public function __construct(SchemaBuilder $scheme, Application $app) + public function __construct(SchemaBuilder $schema, Application $app) { $this->app = $app; - $this->scheme = $scheme; + $this->schema = $schema; $this->output = function () { }; } @@ -77,6 +77,21 @@ class Migrate } /** + * Setup migration tables + */ + public function initMigration() + { + if ($this->schema->hasTable($this->table)) { + return; + } + + $this->schema->create($this->table, function (Blueprint $table) { + $table->increments('id'); + $table->string('migration'); + }); + } + + /** * Get all migrated migrations * * @return Collection @@ -156,28 +171,13 @@ class Migrate } /** - * Setup migration tables - */ - protected function initMigration() - { - if ($this->scheme->hasTable($this->table)) { - return; - } - - $this->scheme->create($this->table, function (Blueprint $table) { - $table->increments('id'); - $table->string('migration'); - }); - } - - /** * Init a table query * * @return Builder */ protected function getTableQuery() { - return $this->scheme->getConnection()->table($this->table); + return $this->schema->getConnection()->table($this->table); } /** diff --git a/src/Database/Migration/MigrationServiceProvider.php b/src/Database/Migration/MigrationServiceProvider.php index 15d06eaf..310b2114 100644 --- a/src/Database/Migration/MigrationServiceProvider.php +++ b/src/Database/Migration/MigrationServiceProvider.php @@ -3,16 +3,19 @@ namespace Engelsystem\Database\Migration; use Engelsystem\Container\ServiceProvider; -use Engelsystem\Database\Db; +use Engelsystem\Database\Database; use Illuminate\Database\Schema\Builder as SchemaBuilder; class MigrationServiceProvider extends ServiceProvider { public function register() { - $schema = Db::connection()->getSchemaBuilder(); - $this->app->instance('db.scheme', $schema); - $this->app->bind(SchemaBuilder::class, 'db.scheme'); + /** @var Database $database */ + $database = $this->app->get(Database::class); + $schema = $database->getConnection()->getSchemaBuilder(); + + $this->app->instance('db.schema', $schema); + $this->app->bind(SchemaBuilder::class, 'db.schema'); $migration = $this->app->make(Migrate::class); $this->app->instance('db.migration', $migration); |