tasoft/setting

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

This package is auto-updated.

Last update: 2024-09-11 11:59:07 UTC


README

设置包是一个简单的控制器,用于存储应用动态设置的PDO表。

安装

$ composer require tasoft/setting

用法

首先,您需要创建一个包含以下至少字段的SQL表

  1. id
    唯一标识每个条目
  2. name
    设置的名称
  3. content
    设置的正文内容
  4. multiple
    指定条目是否为多个(具有相同名称的设置内容堆叠)

您可以按自己的需求设计表格。

现在,在PHP中,下面是如何访问设置的示例

<?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.

下面是如何使用自定义设置工具的示例

<?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;
    }
}