Skip to content

Commit

Permalink
Develop (#3)
Browse files Browse the repository at this point in the history
Dynamic field and tables name
Laravel 5.8 support
  • Loading branch information
glorand authored Mar 20, 2019
1 parent da797ad commit 38c1cee
Show file tree
Hide file tree
Showing 28 changed files with 707 additions and 83 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to `glorand/laravel-model-settings` will be documented in this file

## 3.0.0 - 2019-03-21
### Added
- Command to create the table for settings
- Dynamic name for the settings table and field

## 2.0.0 - 2019-01-26
### Added
- Console Commands

## 1.0.0 - 2019-01-08
### Added
- Initial release
49 changes: 28 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,50 @@ $ composer require glorand/laravel-model-settings
```
{
"require": {
"glorand/laravel-model-settings": "^1.0"
"glorand/laravel-model-settings": "^3.0"
}
}
```

## Env (config) variables **(.env file)**

Default name for the settings field - when you use the `HasSettingsField`

`MODEL_SETTINGS_FIELD_NAME=settings`

Default name for the settings table - when you use the `HasSettingsTable`

`MODEL_SETTINGS_TABLE_NAME=model_settings`

## Updating your Eloquent Models <a name="update_models"></a>
Your models should use the `HasSettingsField` or `HasSettingsTable` trait.

#### Option 1 - `HasSettingsField` trait <a name="update_models_1"></a>
Run the `php artisan model-settings:model-settings-field` in order to create a migration file for a specified table. This command will create a json field (`settings`) for the mentioned table.
You must also add `settings` to your fillable array as shown in the example below
Run the `php artisan model-settings:model-settings-field` in order to create a migration file for a table.\
This command will create a json field (default name `settings`, from config) for the mentioned table.

You can choose another than default, in this case you have to specify it in you model.
```php
public $settingsFieldName = 'user_settings';
```

Complete example:
```php
use Glorand\Model\Settings\Traits\HasSettingsField;

class User extends Model
{
use HasSettingsField;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'settings'
];
//define only if you select a dirrerent name from the default
public $settingsFieldName = 'user_settings';

}
```
#### Option 2 - `HasSettingsTable` trait <a name="update_models_2"></a>
Run before the command `php artisan model-settings:copy-migrations`; the command will copy for you the migration class to create the table ('model_settings').
Run before the command `php artisan model-settings:model-settings-table`.\
The command will copy for you the migration class to create the table where the setting values will be stored.\
The default name of the table is `model_settings`; change the config or env value `MODEL_SETTINGS_TABLE_NAME` if you want to rewrite the default name (**before you run the command!**)
```php
use Glorand\Model\Settings\Traits\HasSettingsTable;

Expand All @@ -94,42 +107,36 @@ class User extends Model

## Usage <a name="usage"></a>

#### Get all model's settings <a name="get_all"></a>
```php
$user = App\User::first();
```

#### Get all model's settings <a name="get_all"></a>
```php
$user->settings()->all();
$user->settings()->get();
```

#### Get a specific setting <a name="get"></a>
```php
$user = App\User::first();

$user->settings()->get('some.setting');
$user->settings()->get('some.setting', 'default value');
```

#### Add / Update setting <a name="add_update"></a>
```php
$user = App\User::first();

$user->settings()->apply((array)$settings);
$user->settings()->set('some.setting', 'new value');
$user->settings()->update('some.setting', 'new value');
```

#### Check if the model has a specific setting <a name="check"></a>
```php
$user = App\User::first();

$user->settings()->has('some.setting');
```

#### Remove a setting from a model <a name="remove"></a>
```php
$user = App\User::first();

$user->settings()->delete('some.setting');
```

Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
],
"require": {
"php": ">=7.1.3",
"illuminate/support": "5.6.*|5.7.*"
"illuminate/config": "~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/console": "~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/filesystem": "~5.5.0|~5.6.0|~5.7.0|~5.8.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0"
"phpunit/phpunit": "^7.5",
"orchestra/testbench": "~3.8.0"
},
"autoload": {
"psr-4": {
Expand All @@ -33,6 +38,9 @@
"Glorand\\Model\\Settings\\Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit"
},
"extra": {
"laravel": {
"providers": [
Expand Down
1 change: 1 addition & 0 deletions config/model_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

return [
'settings_field_name' => env('MODEL_SETTINGS_FIELD_NAME', 'settings'),
'settings_table_name' => env('MODEL_SETTINGS_TABLE_NAME', 'model_settings'),
];
22 changes: 11 additions & 11 deletions phpunit.xml → phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
stopOnFailure="false">
<testsuites>
<testsuite name="Laravel Model Settings Test Suite">
<directory suffix="Test.php">./tests/</directory>
<testsuite name="Glorand Model Settings Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<logging>
<!--<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/>
</logging>
</logging>-->
<php>

<env name="DB_CONNECTION" value="testing" />
</php>
</phpunit>
</phpunit>
21 changes: 0 additions & 21 deletions src/Console/CopyMigrationsCommand.php

This file was deleted.

15 changes: 11 additions & 4 deletions src/Console/CreateSettingsFieldForModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,26 @@ class CreateSettingsFieldForModel extends Command
*/
public function handle(Filesystem $file)
{
$table = strtolower($this->ask('What is the name of the table?'));
$table = Str::snake(trim($table));
$table = $this->ask('What is the name of the table?');
$table = strtolower(Str::snake(trim($table)));
if (empty($table)) {
$this->error('The name of the table is required!');

return false;
}
$fieldName = 'settings';
if (!Schema::hasTable($table)) {
$this->error('Unable to find table "' . $table . '" on the current DB!');

return false;
}

$defaultFieldName = config('model_settings.settings_field_name');
$fieldName = $this->ask(
'What is the name of the settings field name?',
$defaultFieldName
);
$fieldName = strtolower(Str::snake(trim($fieldName)));

if (Schema::hasColumn($table, $fieldName)) {
$this->error('Field "' . $fieldName . '" already exists on table "' . $table . '"');

Expand All @@ -43,7 +50,7 @@ public function handle(Filesystem $file)

$fileName = date('Y_m_d_His') . '_update_' . $table . '_table_add_' . $fieldName . '.php';
$path = database_path('migrations') . '/' . $fileName;
$className = 'Update' . ucfirst($table) . 'TableAddSettings';
$className = 'Update' . ucfirst($table) . 'TableAdd' . ucfirst(Str::camel($fieldName));


$stub = $file->get(__DIR__ . '/../../stubs/create_settings_field.stub');
Expand Down
53 changes: 53 additions & 0 deletions src/Console/CreateSettingsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Glorand\Model\Settings\Console;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Schema;

class CreateSettingsTable extends Command
{
/** @var string */
protected $signature = 'model-settings:model-settings-table';

/** @var string */
protected $description = 'Create migration for the settings table';

/**
* @param \Illuminate\Filesystem\Filesystem $file
* @return bool
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function handle(Filesystem $file)
{
$table = strtolower(config('model_settings.settings_table_name'));
$table = Str::snake(trim($table));
if (empty($table)) {
$this->error('The name of the table is required!');

return false;
}

if (Schema::hasTable($table)) {
$this->error('Table "' . $table . '" already exists!');

return false;
}

$fileName = date('Y_m_d_His') . '_create_' . $table . '_table.php';
$path = database_path('migrations') . '/' . $fileName;
$className = 'Create' . ucfirst(Str::camel($table)) . 'Table';


$stub = $file->get(__DIR__ . '/../../stubs/create_settings_table.stub');
$stub = str_replace('DummyClass', $className, $stub);
$stub = str_replace('DummyTable', $table, $stub);

$file->put($path, $stub);
$this->line("<info>Created Migration:</info> {$fileName}");

return true;
}
}
9 changes: 9 additions & 0 deletions src/Exceptions/ModelSettingsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Glorand\Model\Settings\Exceptions;

use Exception;

class ModelSettingsException extends Exception
{
}
16 changes: 11 additions & 5 deletions src/Managers/AbstractSettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
namespace Glorand\Model\Settings\Managers;

use Glorand\Model\Settings\Contracts\SettingsManagerContract;
use Glorand\Model\Settings\Exceptions\ModelSettingsException;
use Glorand\Model\Settings\Traits\HasSettings;
use Illuminate\Database\Eloquent\Model;

/**
* Class AbstractSettingsManager
* @package Glorand\Model\Settings\Managers
*/
abstract class AbstractSettingsManager implements SettingsManagerContract
{
/** @var \Illuminate\Database\Eloquent\Model */
Expand All @@ -13,22 +19,22 @@ abstract class AbstractSettingsManager implements SettingsManagerContract
/**
* AbstractSettingsManager constructor.
* @param \Illuminate\Database\Eloquent\Model $model
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function __construct(Model $model)
{
$this->model = $model;
if (!in_array(HasSettings::class, class_uses_recursive($this->model))) {
throw new ModelSettingsException('Wrong model, missing HasSettings trait.');
}
}

/**
* @return array
*/
public function all(): array
{
if (!is_null($this->model->settings)) {
return $this->model->settings;
} else {
return [];
}
return $this->model->getSettingsValue();
}

/**
Expand Down
Loading

0 comments on commit 38c1cee

Please sign in to comment.