Download the PHP package poisa/settings without Composer
On this page you can find all versions of the php package poisa/settings. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download poisa/settings
More information about poisa/settings
Files in poisa/settings
Package settings
Short Description A Laravel multi-tenant database configuration/settings manager
License MIT
Informations about the package settings
Settings: A Laravel 5 multi-tenant settings manager
Package objective
To be able to store custom configuration items in a database in a single or multi-tenant environment either in plain text, encrypted form, or any other customizable format. By configuration I am not referring to Laravel's configuration but rather to your domain-specific configuration.
There are 3 specific scenarios where this package might come in handy:
- Multi-tenant systems where you deploy the code to one server and it connects to a different tenant database depending on domain rules (eg. different users connect to different databases).
- Same as #1 except you add a main database that you always connect to. Take a CMS for example, where you have the CMS's own database (a.k.a. the
system
database) and then you also connect to each of your client's databases (a.k.a thetenant
database). In this scenario you work simultaneously with both databases. - A single-tenant website with just one database.
This package really shines when you need to store odd-ball data for which you would not necessarily want to create a separate table.
Think of this package as a key-value store that knows about data types -even custom ones- that can encrypt data at rest and can also fire events.
Index
- Release notes
- Installation
- Use
- Known data types
- Custom data types <-- the meat :meat_on_bone: and potatoes :fries: (and reason for creating this package)
- Events
- CLI Commands
Release notes
- 0.1.13 - Added config option to ignore migrations. Useful for people who want to manage the migrations themselves.
- 0.1.12 - Initial release.
Installation
Installation can be done automatically using Composer.
composer require poisa/settings
Next you will need to publish the package configuration.
php artisan vendor:publish
You will be asked to chose a package. Find Poisa\Settings\SettingsServiceProvider
and select it. This will create a new configuration file in your project: config/settings.php
. You will need to edit it and choose the config options that suit your project before continuing.
Now you will need to execute the migrations found in the package. These will create the database tables that the package will use to store your settings.
php artisan migrate
Important: The migrations will run in your default database connection. This is fine if you are only using one database but if you will be using the package in many databases then you will need to run the migrations in all your databases:
php artisan migrate --database=<your database>
Alternatively you can use your own SQL manager software to copy the table over to the databases and servers that you need.
Use
The simplest use case assumes that you only have one database and no custom data types.
Just like in any other key-value store, Settings::setKey()
expects a string key as the first parameter and the value to store as the second.
When running in a multi-tenant system (like in scenario #2 described above), Settings provides the following shortcuts:
Alternatively you can pass the connection name as the last parameter:
To check whether a key exists you can use the hasKey()
method:
It may come a point where you may want to query the settings table manually but this might prove difficult because not only the connection name but also the table name can be configured. Settings comes with a method that will give you an Eloquen model properly configured that you may use to query or alter the table manually:
Known data types
By default, Settings can store the following types:
- String
- Boolean
- Double
- Integer
- Null
- Array
This means that Settings will store and retrieve the exact data types that you give it:
Custom data types
Storing simple types might not be enough in all cases. You can also teach Settings to work with your custom data types. For example, let's say you have a class that you use to store user preferences. Let's just use a very minimalist class with no getters/setters and validation of any kind for the sake of brevity:
What would it take so that we could do something like this?
If you do this, you will get an exception Poisa\Settings\Exceptions\UnknownDataType
with the message No serializable registered to work with UserPreferences
. This means that we need to create and register a new Serializer so that Settings can know how to work with this class.
So, to teach Settings how to work with your own data types you need to:
- Create a Serializer class.
- Register it with Settings.
Create a Serializer class
A Serializer class is just a regular class that implements the methods in the Poisa\Settings\Serializers\Serializer
interface. It is recommended that this class be a standalone class whose sole purpose is to serialize and unserialize our own data type but in reality it can be any class, even our own data class.
In this example we'll create a dedicated class. Since our data type class is called UserPreferences
, let's call the Serializer class UserPreferencesSerializer
. We'll put this in the root namespace but you will want to create a namespace for all your Serializer classes just to keeps things tidy.
Note: All the methods in the Serializer interface have been thoroughly documented in the source code. For the sake of brevity, all the method comments have been stripped in the following example and only comments relevant to our own implementation were left.
Now our class looks like this:
Security note: We are serializing with json_encode/json_decode instead of using php's own serialize/unserialize functions. There is a potential security issue when using unserialize which you should be aware http://php.net/manual/en/function.unserialize.php. This might not necessarily affect you but it is importat no know about it. Having said that, you can use whatever mechanism you want as Settings does not impose one on you. As long as you return a string from the Serializer::serialize() method, that's ok.
Register it with Settings
The last step is to register the new serializer class with the Settings package. To do this, edit config/settings.php
and add the new serializer to the serializers
key:
That's it. Settings now knows how to store and retrieve UserPreferences!
As you can see, Settings already knows how to work with many data types. If you would like to change how Settings works with a particular data type, you'd have to de-register its Serializer and register your own.
Note: If you de-register a default data type and you try to set a key with that data type, Settings will throw an exception.
Events
Settings fires different kinds of event depending on what it is doing. You can subscribe to these events to be notified when they happen. This is useful in cases where you need to know what happens in your database. Let's say you need to generate an audit trail of everything that happens in your database (every read, write, and update).
You can listen for Settings events in Laravel's EventServiceProvider:
Note: $event-value can be anything you sent to Settings; a scalar value, array, or any kind of object Settings knows how to work with. Make sure you don't assume anything about the value before working with it. Also note that the value is always unserialized when you receive it in events.
CLI Commands
Settings comes with a few comands that will help you inspect the settings table from the command line. The reason for having these commands is because looking at the database table directly will not be very useful when the data is encrypted. These commands perform the decryption for you.
To list all available commands for this package you can run:
php artisan list settings
Currently the following commands are available:
- settings:get Get a key from the database and dump it to stdout (decrypting it if necessary)
- settings:set Save a key to the database
Note: Saving a key using the CLI will only be able to store strings and numeric values. Storing other types including custom ones will only be possible via code.