hexanet/settings-bundle

此包已被废弃,不再维护。未建议替代包。

设置系统

安装次数: 7,876

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 5

分支: 0

开放问题: 0

类型:symfony-bundle

v2.0.0 2018-03-29 08:05 UTC

This package is not auto-updated.

Last update: 2022-02-01 13:12:36 UTC


README

Build Status Total Downloads Latest stable Version

设置系统。

安装

使用 Symfony Flex 的应用程序

打开命令行控制台,进入您的项目目录并执行

$ composer require hexanet/settings-bundle

不使用 Symfony Flex 的应用程序

步骤 1:下载包

打开命令行控制台,进入您的项目目录并执行以下命令以下载此包的最新稳定版本

$ composer require hexanet/settings-bundle

此命令要求您全局安装了 Composer,如 Composer 文档中的 安装章节 所述。

步骤 2:启用包

然后,通过将其添加到项目 app/AppKernel.php 文件中注册的包列表中来启用包

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Hexanet\SettingsBundle\HexanetSettingsBundle(),
        );

        // ...
    }

    // ...
}

数据库

您需要在数据库中创建表,为此我们生成一个迁移

bin/console doctrine:migrations:diff --filter-expression="/setting$/"
bin/console doctrine:migrations:migrate

用法

定义设置

一个模式允许您通过给它们一个默认值来初始化设置。

首先您需要创建一个从 SchemaInterface 接口扩展的类

<?php

namespace App\Settings\AppSchema;

use Hexanet\SettingsBundle\Schema\SettingsBuilder;
use Hexanet\SettingsBundle\Schema\SchemaInterface;

class AppSchema implements SchemaInterface
{
    public function build(SettingsBuilder $settingsBuilder): void
    {
        $settingsBuilder->addSetting('itemsPerPage', 25);
    }
}

然后将其声明为具有 hexanet.settings_schema 标签的服务

App\Settings\AppSchema:
    tags: [hexanet.settings_schema]

该包为实现 SchemaInterface 的类提供自动配置。

之后,我们可以使用 php bin/console hexanet:settings:setup 命令生成所有设置,如果设置已存在,则命令忽略它。

示例

public function indexAction(SettinsManagerInterface $settingsManager) {
    // set and get
    $settingsManager->set('tva', 19.6);
    $settingsManager->get('tva');

    // check if settign exists
    $settingsManager->has('tva');

    // get all settings
    $settingsManager->all();

    // retrieve a non-existent setting 
    $settingsManager->get('not here');
    //  SettingNotFoundException is throw
}

生产环境

对于生产环境,可以通过修改包的配置来激活缓存

// config/packages/prod/hexanet_settings.yaml
hexanet_settings:
    cache: true

使用 Symfony 缓存 app@cache.app

致谢

Hexanet 开发。

许可

SettingsBundle 基于 MIT 许可证