Skip to content

Enable overriding the default model via a config #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"extra":{
"laravel":{
"providers":[
"LaravelPropertyBag\\ServiceProvider"
"LaravelPropertyBag\\PropertyBagServiceProvider"
]
}
}
Expand Down
8 changes: 8 additions & 0 deletions config/property-bag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
/*
* This model will be used to store properties.
*/
'property_bag_model' => \LaravelPropertyBag\Settings\PropertyBag::class,
];
75 changes: 75 additions & 0 deletions src/PropertyBagServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace LaravelPropertyBag;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\ServiceProvider;
use LaravelPropertyBag\Settings\PropertyBag;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class PropertyBagServiceProvider extends ServiceProvider
{
/**
* Register bindings in the container.
*
* @return void
*/
public function register()
{
$this->registerCommands();
}

/**
* Register any other events for your application.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__.'/../config/property-bag.php' => config_path('property-bag.php'),
], 'config');

$this->mergeConfigFrom(__DIR__.'/../config/property-bag.php', 'property-bag');

$this->publishes([
__DIR__.'/Migrations/' => database_path('migrations'),
], 'migrations');
}

/**
* Register Artisan commands.
*/
protected function registerCommands()
{
$this->app->singleton('command.pbag.make', function ($app) {
return $app['LaravelPropertyBag\Commands\PublishSettingsConfig'];
});

$this->app->singleton('command.pbag.rules', function ($app) {
return $app['LaravelPropertyBag\Commands\PublishRulesFile'];
});

$this->commands('command.pbag.make');

$this->commands('command.pbag.rules');
}

public static function determinePropertyBagModel(): string
{
$propertyBagModel = config('property-bag.property_bag_model') ?? PropertyBag::class;

if (!is_a($propertyBagModel, Model::class, true)) {
throw new ModelNotFoundException($propertyBagModel);
}

return $propertyBagModel;
}

public static function getPropertyBagModelInstance(): Model
{
$propertyBagModelClassName = self::determinePropertyBagModel();

return new $propertyBagModelClassName();
}
}
48 changes: 0 additions & 48 deletions src/ServiceProvider.php

This file was deleted.

3 changes: 2 additions & 1 deletion src/Settings/HasSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LaravelPropertyBag\Settings;

use LaravelPropertyBag\Helpers\NameResolver;
use LaravelPropertyBag\PropertyBagServiceProvider;
use LaravelPropertyBag\Exceptions\ResourceNotFound;

trait HasSettings
Expand All @@ -21,7 +22,7 @@ trait HasSettings
*/
public function propertyBag()
{
return $this->morphMany(PropertyBag::class, 'resource');
return $this->morphMany(PropertyBagServiceProvider::determinePropertyBagModel(), 'resource');
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LaravelPropertyBag\Settings;

use Illuminate\Database\Eloquent\Model;
use LaravelPropertyBag\PropertyBagServiceProvider;
use LaravelPropertyBag\Settings\Rules\RuleValidator;
use LaravelPropertyBag\Exceptions\InvalidSettingsValue;

Expand Down Expand Up @@ -333,8 +334,10 @@ public function isSaved($key)
*/
protected function createRecord($key, $value)
{
$propertyBagModel = PropertyBagServiceProvider::determinePropertyBagModel();

return $this->propertyBag()->save(
new PropertyBag([
new $propertyBagModel([
'key' => $key,
'value' => $this->valueToJson($value),
])
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace LaravelPropertyBag\tests;

use Hash;
use LaravelPropertyBag\ServiceProvider;
use Illuminate\Contracts\Console\Kernel;
use LaravelPropertyBag\tests\Classes\Post;
use LaravelPropertyBag\tests\Classes\User;
use LaravelPropertyBag\tests\Classes\Admin;
use LaravelPropertyBag\tests\Classes\Group;
use LaravelPropertyBag\tests\Classes\Comment;
use LaravelPropertyBag\PropertyBagServiceProvider;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
use LaravelPropertyBag\tests\Migrations\CreatePostsTable;
use LaravelPropertyBag\tests\Migrations\CreateUsersTable;
Expand All @@ -34,7 +34,7 @@ public function createApplication()
{
$app = require __DIR__.'/../vendor/laravel/laravel/bootstrap/app.php';

$app->register(ServiceProvider::class);
$app->register(PropertyBagServiceProvider::class);

$app->make(Kernel::class)->bootstrap();

Expand Down