Skip to content

Commit

Permalink
Add configurable database connection (#68)
Browse files Browse the repository at this point in the history
Add database connection configuration
  • Loading branch information
antonkomarev authored Jun 23, 2019
1 parent 0aad37d commit 9ed3994
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 12 deletions.
33 changes: 33 additions & 0 deletions config/love.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of Laravel Love.
*
* (c) Anton Komarev <anton@komarev.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

return [

/*
|--------------------------------------------------------------------------
| Love Storage Driver
|--------------------------------------------------------------------------
|
| This configuration options determines the storage driver that will
| be used to store Love's data. In addition, you may set any
| custom options as needed by the particular driver you choose.
|
*/

'storage' => [
'database' => [
'connection' => env('DB_CONNECTION', 'mysql'),
],
],

];
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Cog\Laravel\Love\Support\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Cog\Laravel\Love\Support\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Cog\Laravel\Love\Support\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Cog\Laravel\Love\Support\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Cog\Laravel\Love\Support\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Cog\Laravel\Love\Support\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Expand Down
17 changes: 17 additions & 0 deletions src/LoveServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function register(): void
*/
public function boot(): void
{
$this->configure();
$this->registerConsoleCommands();
$this->registerObservers();
$this->registerPublishes();
Expand Down Expand Up @@ -88,6 +89,10 @@ private function registerConsoleCommands(): void
private function registerPublishes(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../config/love.php' => config_path('love.php'),
], 'love-config');

$this->publishes([
__DIR__ . '/../database/migrations' => database_path('migrations'),
], 'love-migrations');
Expand Down Expand Up @@ -116,4 +121,16 @@ private function registerListeners(): void
Event::listen(ReactionHasBeenAdded::class, IncrementAggregates::class);
Event::listen(ReactionHasBeenRemoved::class, DecrementAggregates::class);
}

/**
* Merge Love configuration with the application configuration.
*
* @return void
*/
private function configure(): void
{
if (!$this->app->configurationIsCached()) {
$this->mergeConfigFrom(__DIR__ . '/../config/love.php', 'love');
}
}
}
2 changes: 1 addition & 1 deletion src/Reactant/Models/Reactant.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use Cog\Laravel\Love\Reactant\ReactionTotal\Models\NullReactionTotal;
use Cog\Laravel\Love\Reactant\ReactionTotal\Models\ReactionTotal;
use Cog\Laravel\Love\Reaction\Models\Reaction;
use Illuminate\Database\Eloquent\Model;
use Cog\Laravel\Love\Support\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
Expand Down
2 changes: 1 addition & 1 deletion src/Reactant/ReactionCounter/Models/ReactionCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeContract;
use Cog\Laravel\Love\Reactant\Models\Reactant;
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
use Illuminate\Database\Eloquent\Model;
use Cog\Laravel\Love\Support\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

final class ReactionCounter extends Model implements
Expand Down
2 changes: 1 addition & 1 deletion src/Reactant/ReactionTotal/Models/ReactionTotal.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantContract;
use Cog\Contracts\Love\Reactant\ReactionTotal\Models\ReactionTotal as ReactionTotalContract;
use Cog\Laravel\Love\Reactant\Models\Reactant;
use Illuminate\Database\Eloquent\Model;
use Cog\Laravel\Love\Support\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

final class ReactionTotal extends Model implements
Expand Down
2 changes: 1 addition & 1 deletion src/Reacter/Models/Reacter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Cog\Contracts\Love\Reaction\Exceptions\ReactionNotExists;
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeContract;
use Cog\Laravel\Love\Reaction\Models\Reaction;
use Illuminate\Database\Eloquent\Model;
use Cog\Laravel\Love\Support\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;

Expand Down
2 changes: 1 addition & 1 deletion src/Reaction/Models/Reaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Cog\Laravel\Love\Reactant\Models\Reactant;
use Cog\Laravel\Love\Reacter\Models\Reacter;
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
use Illuminate\Database\Eloquent\Model;
use Cog\Laravel\Love\Support\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

final class Reaction extends Model implements
Expand Down
2 changes: 1 addition & 1 deletion src/ReactionType/Models/ReactionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Cog\Contracts\Love\ReactionType\Exceptions\ReactionTypeInvalid;
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeContract;
use Cog\Laravel\Love\Reaction\Models\Reaction;
use Illuminate\Database\Eloquent\Model;
use Cog\Laravel\Love\Support\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

final class ReactionType extends Model implements
Expand Down
30 changes: 30 additions & 0 deletions src/Support/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of Laravel Love.
*
* (c) Anton Komarev <anton@komarev.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cog\Laravel\Love\Support\Database\Eloquent;

use Illuminate\Database\Eloquent\Model as IlluminateModel;
use Illuminate\Support\Facades\Config;

abstract class Model extends IlluminateModel
{
/**
* Get the current connection name for the model.
*
* @return null|string
*/
public function getConnectionName(): ?string
{
return Config::get('love.storage.database.connection');
}
}
30 changes: 30 additions & 0 deletions src/Support/Database/Migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of Laravel Love.
*
* (c) Anton Komarev <anton@komarev.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cog\Laravel\Love\Support\Database;

use Illuminate\Database\Migrations\Migration as IlluminateMigration;
use Illuminate\Support\Facades\Config;

abstract class Migration extends IlluminateMigration
{
/**
* Get the migration connection name.
*
* @return null|string
*/
public function getConnection(): ?string
{
return Config::get('love.storage.database.connection');
}
}

0 comments on commit 9ed3994

Please sign in to comment.