tasoft/setting

v0.8.0 2022-04-11 06:41 UTC

This package is auto-updated.

Last update: 2024-12-11 12:25:52 UTC


README

The setup package is a simple controller of one PDO table holding dynamical settings for your application.

Installation

$ composer require tasoft/setting

Usage

Fist you need to create an SQL table containing at least the following fields:

  1. id
    Identifies each entry uniquely
  2. name
    the name of the setting
  3. content
    The content of the setting
  4. multiple
    Specifies, if the entry is multiple (contents of settings with the same name are stacked)

You can design the table however you want.

Now in PHP see below how you can access the settings:

<?php
use TASoft\Setting\StaticSetting;
use TASoft\Util\PDO;

/** @var PDO $PDO */

$setup = new StaticSetting($PDO, 'TABLE_NAME');
echo $setup->getSetting('my-setting', /* default value */ 'not-available');

// Defining a setting, see below
$setup->setSetting('my-setting', /* value */ 13, /* multiple */ false, /* temporary */ false);
// Passing true to temporary will only update the value for the current request, while passing false writes the passed value into the database persistently.

// Removes the setting
$setup->removeSetting('my-setting', /* temporary */ false);
// Again passing false to temporary will remove the setting from the database as well.

See below how you can use a custom setting tool:

<?php
use TASoft\Setting\AbstractSetting;
use TASoft\Util\PDO;

class MySetting extends AbstractSetting {
    // Adjust the sql table field names
    const RECORD_ID_KEY = 'customized_id';
    const RECORD_NAME_KEY = 'customized_name';
    const RECORD_CONTENT_KEY = 'customized_content';
    const RECORD_MULTIPLE_KEY = 'customized_multiple';
    
    protected function getTableName() : string{
        return "MY_TABLE_NAME";
    }
    
    protected function getPDO() : PDO {
        // Get PDO from anywhere
        /** @var PDO $PDO */
        return $PDO;
    }
}