-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 24ef05f
Showing
9 changed files
with
436 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/build/ | ||
/vendor/ | ||
composer.lock | ||
composer.phar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Yarob Al-Taay | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#STILL IN DEVELOPMENTgit | ||
|
||
# Laravel Model Settings | ||
|
||
Add settings feature to Eloquent models in Laravel 5. | ||
|
||
|
||
## Background | ||
|
||
This has been developed to simplify adding "settings" feature to any eloquent model on your laravel project. Settings WILL be stored in `json` format, which has it's pros and cons! | ||
|
||
## Installation | ||
To install the package via Composer: | ||
|
||
```shell | ||
$ composer require yarob/laravel-model-settings | ||
``` | ||
Then, update `config/app.php` by adding an entry for the service provider. | ||
|
||
```php | ||
'providers' => [ | ||
// ... | ||
Yarob\LaravelModelSettings\ServiceProvider::class, | ||
]; | ||
``` | ||
Finally, via terminal, publish the default configuration file: | ||
|
||
```shell | ||
php artisan vendor:publish --provider="Yarob\LaravelModelSettings\ServiceProvider" | ||
``` | ||
## Updating your Eloquent Models | ||
|
||
Your models should use the `hasSettings` trait. | ||
You must also add `settings` to your `fillable` array, and then cast `settings` to `json` as shown in the example below | ||
|
||
```php | ||
use Yarob\LaravelModelSettings\HasSettings; | ||
|
||
class User extends Model | ||
{ | ||
use hasSettings; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'name', 'email', 'settings' | ||
]; | ||
|
||
|
||
/** | ||
* The attributes that should be casted. | ||
* | ||
* @var array | ||
*/ | ||
protected $casts = [ | ||
'settings' =>'json', | ||
]; | ||
|
||
} | ||
``` | ||
|
||
Your model and database must have column named `settings` in the database to store the settings values. You can add this manually via a migration on the intended model. The column should be text and big enough to store all settings after json encoded. | ||
|
||
## Usage | ||
|
||
Better demonstrated in example | ||
|
||
```php | ||
$user = $user = App\User::first(); | ||
|
||
$user->settings()->save(array( | ||
'address' => 'London', | ||
'phone_number' => '0123456789' | ||
)); | ||
|
||
dd($user->settings); | ||
``` | ||
## Configuration | ||
|
||
Configuration was made to be as flexible as possible. You can add the allowed settings keys per Model basis. | ||
|
||
Global configuration can be set in the `app/config/model-settings.php` file. By default, `phone_number` and `address` settings keys have been added to `User` model. But feel free to change that. | ||
|
||
If a configuration isn't set, then the package defaults from | ||
`vendor/yarob/laravel-model-settings/resources/config/model-settings.php` are used. | ||
Here is an example configuration: | ||
|
||
```php | ||
return [ | ||
'User' => [ | ||
'phone_number', | ||
'address', | ||
], | ||
]; | ||
``` | ||
|
||
|
||
|
||
## Copyright and License | ||
|
||
[laravel-model-settings](https://github.com/EazyServer/laravel-model-settings) | ||
was written by [Yarob Al-Taay](https://twitter.com/TheEpicVoyage) and is released under the | ||
[MIT License](LICENSE.md). | ||
|
||
Copyright (c) 2017 Yarob Al-Taay |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "yarob/laravel-model-settings", | ||
"description": "Laravel 5.0+ package to manage eloquent settings in JSON format", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Yarob Al-Taay", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": {}, | ||
"autoload": { | ||
"psr-4": { | ||
"Yarob\\LaravelModelSettings\\": "src" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
return [ | ||
|
||
/** | ||
* This the array of settings keys acceptable per Model. | ||
* You can add as many keys as you want to each Model. | ||
*/ | ||
|
||
'User' => [ | ||
'phone_number', | ||
'address', | ||
], | ||
|
||
/** | ||
* You can add more Models with allowed keys settings below, | ||
* as per the example above | ||
*/ | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Yarob\LaravelModelSettings; | ||
|
||
use Yarob\LaravelModelSettings\Services\ModelSettingsService; | ||
|
||
/** | ||
* Class HasSettings | ||
* | ||
* @package Yarob\HasSettings | ||
*/ | ||
trait HasSettings | ||
{ | ||
|
||
/** | ||
* Hook into the Eloquent model events to create or | ||
* update the settings as required. | ||
*/ | ||
public static function bootHasSettings() | ||
{ | ||
static::observe(app(HasSettingsObserver::class)); | ||
} | ||
|
||
/** | ||
* Register a saving-settings model event with the dispatcher. | ||
* | ||
* @param \Closure|string $callback | ||
* @return void | ||
*/ | ||
public static function savingSettings($callback) | ||
{ | ||
static::registerModelEvent('saving-settings', $callback); | ||
} | ||
|
||
/** | ||
* Register a settings-saved model event with the dispatcher. | ||
* | ||
* @param \Closure|string $callback | ||
* @return void | ||
*/ | ||
public static function settingsSaved($callback) | ||
{ | ||
static::registerModelEvent('settings-saved', $callback); | ||
} | ||
|
||
/** | ||
* Return the sluggable configuration array for this model. | ||
* Must be implemented at the model class | ||
* | ||
* @return array | ||
*/ | ||
public function settings() | ||
{ | ||
return new ModelSettingsService($this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php namespace Yarob\LaravelModelSettings; | ||
|
||
use Illuminate\Contracts\Events\Dispatcher; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Yarob\LaravelModelSettings\Services\ModelSettingsService; | ||
|
||
/** | ||
* Class HasSettingsObserver | ||
* | ||
* @package Yarob\LaravelModelSettings | ||
*/ | ||
class HasSettingsObserver | ||
{ | ||
|
||
/** | ||
* @var \Yarob\LaravelModelSettings\Services\ModelSettingsService | ||
*/ | ||
private $hasSettingsService; | ||
|
||
/** | ||
* @var \Illuminate\Contracts\Events\Dispatcher | ||
*/ | ||
private $events; | ||
|
||
/** | ||
* HasSettingsObserver constructor. | ||
* | ||
* @param \Yarob\LaravelModelSettings\Services\ModelSettingsService $modelSettingsService | ||
* @param \Illuminate\Contracts\Events\Dispatcher $events | ||
*/ | ||
public function __construct(ModelSettingsService $modelSettingsService, Dispatcher $events) | ||
{ | ||
$this->hasSettingsService = $modelSettingsService; | ||
$this->events = $events; | ||
} | ||
|
||
/** | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return boolean|null | ||
*/ | ||
public function saving(Model $model) | ||
{ | ||
return $this->saveSettings($model, 'saving'); | ||
} | ||
|
||
/** | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @param string $event | ||
* @return boolean|null | ||
*/ | ||
protected function saveSettings(Model $model, $event) | ||
{ | ||
// If the "saving-settings" event returns a value, abort | ||
if ($this->fireSavingSettingsEvent($model, $event) !== null) { | ||
return; | ||
} | ||
$wasSaved = $this->hasSettingsService->save($model); | ||
|
||
$this->fireSettingsSavedEvent($model, $wasSaved); | ||
} | ||
|
||
/** | ||
* Fire the namespaced validating event. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @param string $event | ||
* @return mixed | ||
*/ | ||
protected function fireSavingSettingsEvent(Model $model, $event) | ||
{ | ||
return $this->events->until('eloquent.saving-settings: ' . get_class($model), [$model, $event]); | ||
} | ||
|
||
/** | ||
* Fire the namespaced post-validation event. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @param string $status | ||
* @return void | ||
*/ | ||
protected function fireSettingsSavedEvent(Model $model, $status) | ||
{ | ||
$this->events->fire('eloquent.settings-saved: ' . get_class($model), [$model, $status]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Yarob\LaravelModelSettings; | ||
|
||
use Illuminate\Support\ServiceProvider as Provider; | ||
use Yarob\LaravelModelSettings\Services\ModelSettingsService; | ||
|
||
class ServiceProvider extends Provider | ||
{ | ||
|
||
/** | ||
* Indicates if loading of the provider is deferred. | ||
* | ||
* @var bool | ||
*/ | ||
protected $defer = false; | ||
|
||
/** | ||
* Bootstrap the application events. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
$this->publishes([ | ||
__DIR__ . '/../resources/config/model-settings.php' => config_path('model-settings.php'), | ||
], 'config'); | ||
} | ||
|
||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->mergeConfigFrom(__DIR__ . '/../resources/config/model-settings.php', 'model-settings'); | ||
|
||
$this->app->singleton(HasSettingsObserver::class, function ($app) { | ||
return new HasSettingsObserver(new ModelSettingsService(), $app['events']); | ||
}); | ||
} | ||
} |
Oops, something went wrong.