Simple user settings facade for Hyperf. Settings are stored as JSON in a single database column, so you can easily add it to an existing table.

Overview

hyperf-user-settings

Simple user settings util for hyperf Settings are stored as JSON in a single database column, so you can easily add it to an existing table (users for example).

Installation

  1. Run composer require lysice/hyperf-user-settings to include this in your project.
  2. Run php bin/hyperf.php vendor:publish lysice/hyperf-use-settings to publish the config file and migration.
  3. Run php bin/hyperf.php migrate to add field to your table. Alternatively, use the Laravel migration included in this package to automatically create a settings column in the users table: php bin/hyperf.php migrate.
  4. Modify the published configuration file located at config/user-setting.php.

Configuration

There is a file config/user-setting.php to adjust package configuration. If this file doesn't exist, run php bin/hyperf.php vendor:publish to create the default configuration file.

return array(
  'table' => 'users',
  'column' => 'settings',
  'constraint_key' => 'id',
  'default_constraint_value' => null,
  'custom_constraint' => null,
);

Table

Specify the table on your database that you want to use.

Column

Specify the column in the above table that you want to store the settings JSON data in.

Constraint key

Specify the index column used for the constraint - this is used to differentiate between different users, objects or models (normally id).

Default constraint value

Specify the default constraint value - by default this will be the user's ID you need pass userId to the construct function, and will be superseded by specifying a $constraint_value on any function call.

Custom constraint

Specify a where clause for each query - set this if you do not want to access different rows (for example if your app is single-user only).

Usage

Use the helper function setting($userId) to initial the Setting class, and you can invoke any function in Setting class. The $constraint_value parameter is optional on all functions; if this is not passed, the default_constraint_value from the config file will be used.

Set

setting($userId)->set('key', 'value', $constraint_value);

Use set to change the value of a setting. If the setting does not exist, it will be created automatically. You can set multiple keys at once by passing an associative (key=>value) array to the first parameter.

Get

setting($userId)->get('key', 'default', $constraint_value);

Use get to retrieve the value of a setting. The second parameter is optional and can be used to specify a default value if the setting does not exist (the default default value is null).

Forget

setting($userId)->forget('key', $constraint_value);

Unset or delete a setting by calling forget.

Has

setting($userId)->has('key', $constraint_value);

Check for the existence of a setting, returned as a boolean.

All

setting($userId)->all($constraint_value);

Retrieve all settings as an associative array (key=>value).

Save

setting($userId)->save($constraint_value);

Save all changes back to the database. This will need to be called after making changes; it is not automatic.

Load

setting($userId)->load($constraint_value);

Reload settings from the database. This is called automatically if settings have not been loaded before being accessed or mutated.

call chaining

The functions below return the object of setting so you can invoke other functions. set forget save like this:

setting($userId)->set('key', 'value', constraint_value)->get('key', 'default');

Example

These examples are using the default configuration.

Using the default constraint value

The following sets and returns the currently logged in user's setting "example".

// Set 'example' setting to 'hello world' and save to db
setting($userId)->set('example', 'hello world')->save();

// or use like:
$setting = setting($userId);
$setting->set('example', 'hello world')
$setting->save();

// Get the same setting
return setting($userId)->get('example');

Finally

Contributing

Feel free to create a fork and submit a pull request if you would like to contribute.

Bug reports

Raise an issue on GitHub if you notice something broken.

You might also like...
Reset UI Bookmarks allows admin users to reset their own UI bookmarks such as state of filters, column positions and applied sorting ( e.g Sales > Orders ).
Reset UI Bookmarks allows admin users to reset their own UI bookmarks such as state of filters, column positions and applied sorting ( e.g Sales Orders ).

Reset Ui Bookmarks Reset UI Bookmarks becomes an invaluable tool while working daily in the admin panel, especially on Magento® instances with a large

This extension links MediaWiki to phpBB's user table for authentication, and disallows the creation of new accounts in MediaWiki.

This extension links MediaWiki to phpBB's user table for authentication, and disallows the creation of new accounts in MediaWiki. Users must then log in to the wiki with their phpBB account.

AnsibleBoy aims to use the Asnible `facts` as data, which can then be visualized in a table format
AnsibleBoy aims to use the Asnible `facts` as data, which can then be visualized in a table format

AnsibleBoy - Ansible Frontend Hub About AnsibleBoy aims to use the Ansible facts as data, which can then be visualized as a table ToDo (note that this

Magento 2 Module Experius Page Not Found 404. This module saves all 404 url to a database table

Magento 2 Module Experius Page Not Found 404 This module saves all 404 urls to a database table. Adds an admin grid with 404s It includes a count so y

run user analytics within your system and track user data inside your database.

WP Local Analytics plugin. run user analytics within your system and track user data inside your database. Installing Go to the plugin page from the W

WP Local Analytics plugin. - run user analytics within your system and track user data inside your database.

WP Local Analytics plugin. - run user analytics within your system and track user data inside your database.

High-performance, low-memory-footprint, single-file embedded database for key/value storage

LDBA - a fast, pure PHP, key-value database. Information LDBA is a high-performance, low-memory-footprint, single-file embedded database for key/value

🌏 Webnux 🌏 , a videos streaming website where you can watch what you want when you want.
🌏 Webnux 🌏 , a videos streaming website where you can watch what you want when you want.

🎥 WEBNUX 🎥 Welcome to 🌏 Webnux 🌏 , a videos streaming website where you can watch what you want when you want. ☀️ ☀️ OBJECTIFS ☀️ ☀️ - A sessi

Releases(v1.0)
Owner
lysice
The wind arises among the duckweed.
lysice
JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

Eboubaker Eboubaker 2 Jul 31, 2022
YCOM Impersonate. Login as selected YCOM user 🧙‍♂️in frontend.

YCOM Impersonate Login as selected YCOM user in frontend. Features: Backend users with admin rights or YCOM[] rights, can be automatically logged in v

Friends Of REDAXO 17 Sep 12, 2022
Stores the customer_user for WooCommerce orders and subscriptions in the post_author column of posts table.

Post Author Optimization for WooCommerce Requires PHP: 7.0 WP requires at least: 5.7 WP tested up to: 5.7 WC requires at least: 5.6.0 WC tested up to:

Devin Price 9 Apr 2, 2022
Minimalistic bookmark manager for your own server written in PHP. Bookmarks are stored in a sqlite database.

b - Bookmark manager b is a minimalistic bookmark manager for your own server. Written in PHP. Bookmarks are stored in a sqlite database. Features: fi

Sebastian Volland 48 Jan 6, 2023
This package makes it easy to add early access mode to your existing application.

This package makes it easy to add early access mode to your existing application. This is useful for when you want to launch a product and need to gat

Neo 174 Nov 26, 2022
Add scalar type hints and return types to existing PHP projects using PHPDoc annotations

PHPDoc to Type Hint Archived! This repository is now archived. Consider using PHP CS Fixer (and especially the phpdoc_to_param_type and phpdoc_to_retu

Kévin Dunglas 228 May 22, 2022
Ip2region is a offline IP location library with accuracy rate of 99.9% and 0.0x millseconds searching performance. DB file is ONLY a few megabytes with all IP address stored. binding for Java,PHP,C,Python,Nodejs,Golang,C#,lua. Binary,B-tree,Memory searching algorithm

Ip2region是什么? ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nodejs,golang,c#等查询绑定和Binary,B树,内存三种查询算法。 Ip2region特性

Lion 12.6k Dec 30, 2022
Integrates the ClassicPress Plugin Directory and any plugin stored in GitHub (tagged with classicpress-plugin) in the ClassicPress Admin

ClassicPress Plugin Directory Adds a new screen in a ClassicPress Install where you can browse, install, activate, deactivate, update, delete and pagi

TukuToi 3 Dec 27, 2022
It is a simple blog application coded with PHP, HTML, CSS. You can develop, edit. You can see it as a skeleton. ⚡

PHP-BLOG-SYSTEM Simple blog system Features Adding Text Update Text Text Deletion User Login and register Bootstrap Design Profile Page How to use blo

Selçuk 2 Aug 21, 2022
Guest to Customer for Magento2 - Quickly and easily convert existing guest checkout customers to registered customers.

Guest to Customer for Magento 2.0 For Magento 2.0.x, 2.1.x, 2.2.x, 2.3.x and 2.4.x In general E-commerce, shoppers do not like to create an account du

MagePal :: Magento Extensions 66 Oct 7, 2022