承接 hello-motto/config-service-provider 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hello-motto/config-service-provider

Composer 安装命令:

composer require hello-motto/config-service-provider

包简介

A service provider to configurate silex ~2.0 with yaml, json and php files

README 文档

README

This is a Service Provider that permits to use YAML, JSON and PHP config files in a Silex app.

Requirements

  • silex/silex: 2.X

Suggested

  • symfony/yaml: 3.X

Installation

Install with Composer:

$ composer require hello-motto/config-service-provider dev-master

Or add to composer.json

"require": {
    "hello-motto/config-service-provider": "dev-master"
}

Usage

This Provider will parse your YAML, JSON and PHP files to set config variables. Those config variables are available through $app['config']['myVariable']. Some special variables you can define (parameters variable) also are available through $app['parameters']. All the variables that are contained in the several config files are recursively merged in a array.

##Add files

You can pass one or several files to the ServiceProvider in the 'config.files' argument. No matter if it's a json, yaml or php file

$app->register(new HelloMotto\Silex\Config\ConfigServiceProvider(), [
    'config.files' => [
        PATH_TO_FIRST_FILE,
        PATH_TO_SECOND_FILE,
        PATH_TO_THIRD_FILE,
        etc.
    ]
]);

where PATH_TO_CONFIG_FILE is location of YML, JSON or PHP file with configuration for example

__DIR__. "/config/config.yml",
__DIR__. "/config/config.json",
__DIR__. "/config/config.php",

Files example :

config/config.php

<?php
return [
    'parameters' => [
        'dbhost' => 'localhost',
        'dbuser' => 'user',
        'dbpass' => 't@rt1fl3tt3',
        'dbport' => '~' // tilde will be replaced by null value
    ],
    'twig' => [
        'twig.form.templates' => [
            'formage.html.twig'
        ]
    ]
];

config/config.json

{
    "parameters": {
        "dbhost": "localhost",
        "dbuser": "user",
        "dbpass": "t@rt1fl3tt3",
        "dbport": "~"
    },
    "twig": {
        "twig.form.templates": [
            "formage.html.twig"
        ]
    }
}

config/config.yml

parameters:
    dbhost: "localhost"
    dbuser: "user"
    dbpass: "t@rt1fl3tt3"
    dbport: "~" #tilde will be replaced by null value
twig:
    twig.form.templates:
        -"formage.html.twig"

##Add constants

You can also pass to the ServiceProvider constants parameters to replace in your config files. It permits to use the PHP functions within the configuration files. Constants name must begin and end with % character. You can set the constants in the 'config.constants' array :

$app->register(new HelloMotto\Silex\Config\ConfigServiceProvider(), [
    'config.files' => [],
    'config.constants' => [
        '%web.dir%' => __DIR__.'/../web',
        '%need%' => 'tartiflette',
        etc.
    ]
]);

config/config.json

{
    "mySubDirectory": "%web.dir%/reblochon",
    "ourMotto": "In %need% we trust"
}

config/config.yml

    mySubDirectory: "%web.dir%/reblochon",
    ourMotto: "In %need% we trust"

The app['config']['ourMotto'] variable will contain In tartiflette we trust.

##Add closures

YAML and JSON files don't allow to use dynamic code. But some Providers like Security Provider are more powerful with closure parameters. That's why it's possible to add some closures with the 'config.closures' parameter. To use the closure parameter, your array must have the same tree structure.

$app->register(new HelloMotto\Silex\Config\ConfigServiceProvider(), [
    'config.files' => [],
    'config.constants' => [],
    'config.closures' => [
        'security' => [
            'security.firewalls' => [
                'main' => [
                    'users' => function() {
                        return new UserProvider();
                    }
                ]
            ]
        ]
    ]
]);

##Import files It's possible to import files from a YAML or JSON config file. You just need to use the object "import" and set an array of resources. The files are always imported with a relative path from the current file. Notice that PHP config files don't support the import method.

config/config_dev.json

{
    "imports": [
        {
          "resource": "config.json"
        },
        {
          "resource": "parameters.json"
        }
    ]
}

config/config_dev.yml

imports:
    - { resource: config.yml }
    - { resource: parameters.yml }
# config.yml and parameters.yml are in the same directory as config_dev.yml

#Traits

###config() method Adding this trait to your Application, you can use the config variables as object. $app['config'] will be available using $app->config(). You can also access to the first dimension of the array passing the parameter to the method. $app['config']['twig'] is available through $app->config('twig').

###parameters() method This shortcut also exists for the parameters variables. $app['parameters']['dbhost'] will be the same that $app->parameters('dbhost')

###loadFile() method This shortcut use the ConfigLoader to parse a JSON, YAML or PHP file into an array, replacing the constants that are already set.

###loadManyFiles() method This method does the same as loadFile() with an array of files as argument.

Licence

GPL 3.0

Message from the President

Beef...

统计信息

  • 总下载量: 23
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2017-03-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固