mr-luke/settings-manager

Laravel 5 Package that provides settings manager with type casting.

2.0.0 2023-05-10 21:46 UTC

README

Latest Stable Version Total Downloads License

Tests Workflow Quality Gate Status Security Rating Reliability Rating

This package provides settings manamgement to your Laravel app with support of multiple setting bags with typed value.

Getting Started

Setting Manager supported versions:

  • Laravel 10
  • Laravel 9
  • Laravel 8

Installation

To install through composer, simply put the following in your composer.json file and run composer update

{
    "require": {
        "mr-luke/settings-manager": "~2.0"
    }
}

Or use the following command

composer require "mr-luke/settings-manager"

Next, add the service provider to app/config/app.php

Mrluke\Settings\SettingsServiceProvider::class,

Note: Package is auto-discoverable!

Configuration

To use SettingsManager you need to setup your Bags first. Add your own one to the config file bags:

'bags' => [
    'general' => [ // Given key is used as a Bag name
        'driver'   => 'database', // One of available drivers
        'cache'    => true, // Should be Cached
        'lifetime' => 60 // Cache lifetime in minutes
	],
],

You can setup different database connections or tables by new driver or different json files in:

'drivers' => [
    'database' => [
        'class'      => \Mrluke\Settings\Drivers\Database::class,
        'connection' => 'mysql',
        'table'      => 'settings',
    ],

    'json' => [
        'class' => \Mrluke\Settings\Drivers\Json::class,
        'path'  => base_path('storage/app/settings/'),
        'file'  => 'settings.json',
    ]
],

You can also publish config file via command:

php artisan vendor:publish

Usage

Facade

You can access to Manager and your Bags using Mrluke\Settings\Facades\Settings.

Type

SettingsManager is a type casted tool that care about it during the whole process.

Example: You can pass string 5.567 to set method for ratio setting (float) and SettingsManager will cast the value to float 5.567 behind the scean.

Whereever you ask for certain key, it will always be correct type. But all begins at the point of registering...

Registering new index

To register new value use:

$value = Settings::register(string $key, $value, string $type);

This method returns given $value casted to given $type.

Accessing index

To get value use:

$value = Settings::get(string $key, $default = null);

This method returns $default in case given $key is not present in the Bag. Otherwise $value is casted to registered type.

Setting new value of index

To set new value use:

$value = Settings::set(string $key, $value);

This method returns $value casted to registered type. If given $key is not present, it will automaticaly call register method with auto-detected type.

This method shoud not be use to register settings in general! Use Eventing to detect all needed settings during development.

Forgeting an index

To forget an index use:

Settings::forget(string $key);

Default Bag vs specified

To access specific Bag use bag accessor method:

$value = Settings::bag(string $name)->get(string $key, $default = null);

Helper

You can access to SettingsManager via helper function:

// Get index
settings(string $key);
// Set value
settings([string $key => $value]);
// Get instance and perform action
settings()->forget(string $key);

Events

SettingsManager provides you a list of pre and post action events to help you handle different situations.

Example: You can use Mrluke\Settings\Events\Registered event to prepare full list of production settings.

Namespace Mrluke\Settings\Events:

  • Forgeting
  • Forgot
  • Loading
  • Loaded
  • Registering
  • Registered
  • Updating
  • Updated

JSON Structure

SettingsManager provides JSON Driver for configurations that can be share with front-end apps. The structure looks like this:

{
  "key": {
    "type": "<type>",
    "value": "<value>"
  },
  "another": {
    "type": "<type>",
    "value": "<value>"
  }
}

Plans

  • Artisan Commands
  • Blade helpers for bool keys
  • Additional option to store default value from get() method