1. Go to this page and download the library: Download illuminatech/config library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
illuminatech / config example snippets
use Illuminate\Config\Repository;
use Illuminate\Support\Facades\App;
use Illuminatech\Config\PersistentRepository;
use Illuminatech\Config\StorageDb;
$sourceConfigRepository = new Repository([
'foo' => [
'name' => 'Foo',
],
'bar' => [
'enabled' => false,
],
'other' => [
'value' => 'Some',
],
]);
$storage = new StorageDb(App::make('db.connection'));
$persistentConfigRepository = (new PersistentRepository($sourceConfigRepository, $storage))
->setItems([
'foo.name',
'bar.enabled',
]);
echo $persistentConfigRepository->get('foo.name'); // returns value from database if present, otherwise the one from source repository, in this case - 'Foo'
echo $persistentConfigRepository->get('other.value'); // keys, which are not specified as "items" always remain intact, in this case - always return 'Some'
namespace App\Providers;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Support\ServiceProvider;
use Illuminatech\Config\PersistentRepository;
use Illuminatech\Config\StorageDb;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
$this->app->extend('config', function (Repository $originConfig) {
$storage = new StorageDb($this->app->make('db.connection'));
$newConfig = (new PersistentRepository($originConfig, $storage))
->setItems([
'mail.contact.address' => [
'label' => __('Email address receiving contact messages'),
'rules' => ['sometimes', '
namespace App\Providers;
use Illuminatech\Config\Providers\AbstractPersistentConfigServiceProvider;
use Illuminatech\Config\StorageContract;
use Illuminatech\Config\StorageDb;
class PersistentConfigServiceProvider extends AbstractPersistentConfigServiceProvider
{
protected function storage(): StorageContract
{
return (new StorageDb($this->app->make('db.connection')));
}
protected function items(): array
{
return [
'mail.contact.address' => [
'label' => __('Email address receiving contact messages'),
'rules' => ['sometimes', '
namespace App\Models;
use Illuminate\Config\Repository;
use Illuminate\Database\Eloquent\Model;
use Illuminatech\Config\PersistentRepository;
use Illuminatech\Config\StorageDb;
class User extends Model
{
/**
* @var \Illuminatech\Config\PersistentRepository configuration repository specific to this model.
*/
private $config;
/**
* Returns configuration associated with this particular model.
*
* @return \Illuminatech\Config\PersistentRepository config repository.
*/
public function getConfig(): PersistentRepository
{
if ($this->config === null) {
if (empty($this->id)) {
throw new \InvalidArgumentException('Unable to get config for model without ID.');
}
$repository = new Repository($this->defaultConfigData());
$storage = (new StorageDb($this->getConnection()))
->setFilter(['user_id' => $this->id]); // ensure configuration varies per each model
$this->config = (new PersistentRepository($repository, $storage))
->setItems($this->persistentConfigItems());
}
return $this->config;
}
/**
* Defines default configuration for the model instance.
*
* @return array config.
*/
private function defaultConfigData()
{
return [
'sidebar' => [
'enabled' => true,
],
'color' => [
'primary' => '#4099de',
'sidebar' => '#b3c1d1',
],
];
}
/**
* Defines the config items, which should be manageable from web interface and stored in the database.
*
* @return array config items.
*/
private function persistentConfigItems(): array
{
return [
'sidebar.enabled' => [
'label' => 'Sidebar enabled',
'rules' => ['sometimes', '
use Illuminatech\Config\Item;
use Illuminatech\Config\PersistentRepository;
$persistentConfigRepository = (new PersistentRepository(...))
->setItems([
'some.config.value',
'another.config.value' => [
'label' => 'Custom label',
'rules' => ['
use Illuminatech\Config\PersistentRepository;
$persistentConfigRepository = (new PersistentRepository(...))
->setItems([
'some.config',
]);
$value = $persistentConfigRepository->get('some.config'); // loads data from persistent storage automatically.
use Illuminatech\Config\PersistentRepository;
$persistentConfigRepository = (new PersistentRepository(...))
->setItems([
'some.config',
]);
$persistentConfigRepository->restore(); // loads/re-loads data from persistent storage
use Illuminatech\Config\PersistentRepository;
$persistentConfigRepository = (new PersistentRepository(...))
->setItems([
'some.config',
'another.config',
]);
$persistentConfigRepository->set('some.config', 'new value'); // no changes at the persistent storage at this point
$persistentConfigRepository->synchronize(); // save values to the persistent storage
use Illuminate\Config\Repository;
use Illuminatech\Config\PersistentRepository;
$sourceConfigRepository = new Repository([
'some' => [
'config' => 'original value',
],
]);
$persistentConfigRepository = (new PersistentRepository($sourceConfigRepository, ...))
->setItems([
'some.config',
]);
$persistentConfigRepository->save([
'some.config' => 'new value',
]);
echo $persistentConfigRepository->get('some.config'); // outputs 'new value'
$persistentConfigRepository->reset(); // clears data in the persistent storage
echo $persistentConfigRepository->get('some.config'); // outputs 'original value'
use Illuminate\Config\Repository;
use Illuminate\Support\Facades\App;
use Illuminatech\Config\PersistentRepository;
$sourceConfigRepository = new Repository([
'some' => [
'config' => 'original value',
],
]);
$persistentConfigRepository = (new PersistentRepository($sourceConfigRepository, ...))
->setItems([
'some.config',
])
->setCache(App::make('cache.store'))
->setCacheKey('global-config')
->setCacheTtl(3600 * 24);
namespace App\Http\Controllers;
use Illuminate\Contracts\Container\Container;
use Illuminate\Http\Request;
class ConfigController extends Controller
{
/**
* @var \Illuminatech\Config\PersistentRepository persistent config repository, which is set at `AppServiceProvider`.
*/
private $config;
public function __construct(Container $app)
{
$this->config = $app->get('config');
}
public function index()
{
$this->config->restore(); // ensure config values restored from database
return view('config.form', ['items' => $this->config->getItems()]);
}
public function update(Request $request)
{
$validatedData = $this->config->validate($request->all());
$this->config->save($validatedData);
return back()->with('status', 'success');
}
public function restoreDefaults()
{
$this->config->reset();
return back()->with('status', 'success');
}
}
use Illuminate\Config\Repository;
use Illuminatech\Config\PersistentRepository;
$sourceConfigRepository = new Repository([
'some' => [
'array' => ['one', 'two', 'three'],
],
]);
$persistentConfigRepository = (new PersistentRepository($sourceConfigRepository, ...))
->setItems([
'some.array' => [
'cast' => 'array', // cast value from persistent storage to array
'rules' => ['sometimes', '
use Illuminate\Config\Repository;
use Illuminatech\Config\PersistentRepository;
$sourceConfigRepository = new Repository([
'some' => [
'apiKey' => 'secret',
],
]);
$persistentConfigRepository = (new PersistentRepository($sourceConfigRepository, ...))
->setItems([
'some.apiKey' => [
'encrypt' => true, // encrypt value before placing it into the persistent storage
],
]);
use Illuminate\Config\Repository;
use Illuminatech\Config\PersistentRepository;
use Illuminatech\Config\StorageDb;
$sourceConfigRepository = new Repository([
'some' => [
'config' => 'original value',
],
]);
$storage = new StorageDb(...);
$storage->save([
'some.config' => 'some value',
'obsolete.config' => 'obsolete value',
]);
$persistentConfigRepository = (new PersistentRepository($sourceConfigRepository, $storage))
->setItems([
'some.config',
]);
$persistentConfigRepository->gc(); // removes 'obsolete.config' from storage