Skip to content

Commit

Permalink
Add User Settings (#4)
Browse files Browse the repository at this point in the history
* add user settings

* fix codestyle

* use getKey instead of id in user settings

* remove duplication and add abstract method

* remove empty line

* cache on set instead of on get

* cache user settings

* make commands code cleaner

* use lf instead of crlf

* use printer package

* clean repeated code

* update readme

* update readme
  • Loading branch information
merodiro authored Apr 10, 2018
1 parent d8c339d commit a55c73d
Show file tree
Hide file tree
Showing 18 changed files with 627 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ checks:
tools:
external_code_coverage:
timeout: 600
runs: 2
runs: 2
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ script:
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover

notifications:
email: false
email: false
75 changes: 63 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![Quality Score][ico-code-quality]][link-code-quality]
[![Total Downloads][ico-downloads]][link-downloads]

laravel easy `key => value` global settings
laravel easy `key => value` global/user settings

## Install

Expand All @@ -23,67 +23,118 @@ Optional: only if you want to edit cache configurations
```bash
$ php artisan vendor:publish --provider=Merodiro\Settings\SettingsServiceProvider
```
## Setup a Model
```php
use Merodiro\Settings\HasSettings;

class User extends Model
{
use HasSettings;
...
}
```

## Usage

### Set settings
creates a record if the key doesn't exist or update it if the key exists
in addition to updating the cache

in addition to updating the cache

```php
// Global Settings
Settings::set('key', 'value');
Settings::set('key', 'another value');

// User Settings
$user->setSettings('key', 'value');
$user->setSettings('key', 'another value');
```


### Get value from settings
Returns its value if it exists or the second parameter if it doesn't exist
and caches settings automatically

```php
// Global Settings
$name = Settings::get('site-name');
$value = Settings::get('key', 'default');

// User Settings
$user->getSettings('site-name');
$user->getSettings('key', 'value');
```

### Delete key from settings
Remove the key from the cache
Remove the setting with the given key
in addition to removing it from the cache

```php
// Global Settings
Settings::forget('key');

// User Settings
$user->forgetSettings('key');
```

### Delete all settings
Remove all the keys from the cache
Delete all the settings
in addition to removing them from the cache

```php
// Global Settings
Settings::flush();

// User Settings
$user->flushSettings();
```

### Get all settings
Returns all settings stored in key => value array

```php
// Global Settings
$settings = Settings::all();
```

### Get value from blade template

```php
<h1>@settings('site-name')</h1>
<h1>@settings('site-name', 'default name')</h1>
// User Settings
$settings = $user->allSettings();
```

## Artisan Commands

### Cache all settings
Caches all settings for the duration that has been set in settings.php config file

*you can set the duration to a high number or schedule the command to run often to get the best value of it*

```bash
# Global settings only
php artisan settings:cache

# Global and User Settings
php artisan settings:cache --model=App/User
```

### Clear cache for all settings

```bash
php artisan settings:clear
# Global settings only
$ php artisan settings:clear

# Global and User Settings
$ php artisan settings:clear --model=App/User
```

## Blade Directives

### Get value from blade template

```php
<h1>@settings('site-name')</h1>
<h1>@settings('site-name', 'default name')</h1>
```


## Testing

``` bash
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "merodiro/settings",
"type": "library",
"description": "Easy global settings",
"description": "Easy global/user settings",
"keywords": [
"Merodiro",
"Settings"
Expand All @@ -19,6 +19,7 @@
"php": "~7.0"
},
"require-dev": {
"codedungeon/phpunit-result-printer": "^0.6.1",
"graham-campbell/testbench": "^5.0",
"mockery/mockery": "^1.0",
"phpunit/phpunit": "6.*",
Expand All @@ -31,6 +32,7 @@
},
"autoload-dev": {
"classmap": [
"tests/models/User.php",
"tests/TestCase.php"
]
},
Expand Down
Loading

0 comments on commit a55c73d

Please sign in to comment.