oddgreg/nine-loaders

容器无关的配置加载器和集合。

dev-master 2016-08-27 17:29 UTC

This package is not auto-updated.

Last update: 2024-09-26 02:36:09 UTC


README

该仓库是一个管理配置的类集合。内部,加载器和配置集合不使用也不需要依赖注入器或服务定位器。可以将容器传递给LoaderSet,它将通过ConfigurationSet传递并继续传递给单个配置器。然后配置器根据需要填充容器。

主要仓库类包括

  1. LoaderSet -- 配置集合集。
  2. ConfigurationSet -- 配置器集合。
  3. Configurator -- 配置器。
  4. ConfigFileReaderConfigFileWriter -- 配置文件读取和写入。

快速概述

以下图表揭示了核心类的类层次结构。

Relationship Diagram

  • LoaderSet是一组ConfigurationSet对象。每个LoaderSet可以向一个ConfigurationSet传递单个容器引用。每个加载集合管理和启动加载和应用程序配置集合。

如果您需要一个特定的标识符(例如:AurynLoaderSet)或需要进一步初始化环境,请子类化LoaderSet类。

  • ConfigurationSet是一组Configurator对象。每个集合集管理插入、加载和应用程序Configurator对象。

ConfigurationSet类处理其包含的Configurators的插入、选择、加载和应用程序。

  • Configurator类处理单个作用域的配置。例如:TwigConfigurator可以处理Twig所需的配置,而DatabaseConfiguratorMiddlewareConfigurator等将处理它们各自的作用域配置。

  • ConfigFileReaderConfigFileWriter处理配置文件的读取和写入。文件可以是.php.yml.json格式。默认情况下不支持XML。

在某些情况下,您可能只需要ConfigFileReader来管理对基于文件的配置值的访问。

示例配置设置文件。

return [
    // example use of a subclassed configuration set
    // which handles its own configurators etc. or
    // it may do something entirely different.
    ExampleConfigurationSet::class => [
        'name'        => 'example.config',
        'config_path' => __DIR__ . '/../config/examples/',
        'priority'    => 'high',
    ],
    // example of a labeled ConfigurationSet
    // note that the label isn't significant. The name
    // parameter defines the name given to the set.
    'views'                        => [
        'name'        => 'example.views',
        'config_path' => __DIR__ . '/../config/',
        'priority'    => 'low',
        'config'      => [
            BladeConfigurator::class    => ['name' => 'blade', 'dataset' => 'view.blade',],
            TwigConfigurator::class     => ['name' => 'twig', 'dataset' => 'view.twig'],
            MarkdownConfigurator::class => ['name' => 'markdown', 'dataset' => 'view.markdown'],
        ],
    ],
];

配置文件处理

这些类中最简单的是处理读取、访问和写入目录中配置文件的类。

配置和加载类。

Nine\Loaders包提供了一种配置依赖注入器、服务提供者和通用类的方法。主要受一个或多个配置文件的驱动,该包还提供了一个配置构建器,可以读取和生成配置文件。

由于该包不依赖于任何特定的容器或框架,因此可以在几乎所有情况下使用。

支持PHPYAMLJSON。默认为PHP。

安装

请注意,此包旨在用于PHP 7

  1. 安装Composer.
  2. type: composer require oddgreg/nine-config --no-dev

可选地,如果您想运行测试,请从上面的命令中删除--no-dev

在不使用集合的情况下使用ConfigFileReader

ConfigFileReader类可以在不使用任何其他配置集合类的情况下使用。在简单的配置情况下,可能不需要或希望以任何其他方式处理配置文件。

由于ConfigFileReader在第一次读取时从文件中缓存配置,因此后续访问速度快。您将只加载所需的内容。

实例化和使用ConfigFileReader类

// instantiate the reader
$config = new ConfigFileReader(__DIR__ . '/../config/');

// nothing much else to do. The class will read config files on demand.
// this will read $basePath . 'view.php' then return the 'twig' index array.
$twigConfig = $config['view.twig'];

// -- or go deeper --
$twigEnabled = $config['view.twig.enabled'];

// multiple requests from the same config source (ie: view.php) do not 
// reload the file. The ConfigFileReader caches requests.

使用ConfigFileWriter修改和写入配置文件

ConfigFileReader 无法修改或写入配置文件。使用它可确保在使用期间配置文件不会发生任何变化。有时在应用程序执行的某些阶段修改一个或多个配置文件是有用的。在这种情况下,提供了 ConfigFileWriter

ConfigFileWriterConfigFileReader 作为依赖项。以下是一个简单的示例

// create and preload a collection of configurations.
$reader = (new ConfigFileReader(CONFIG))->preloadPath();

// create the writer containing pre-loaded configurations.
$writer = new ConfigFileWriter($reader);

// modify the configuration (both methods are equivalent.)
$writer->view_markdown_defaults_debug = false;
// -- or --
$writer['view.markdown.defaults.debug'] = false;

// extract an outer key array and write it to a new file
$writer->exportPHPFile(
    // the path to where the new file will be written
    CONFIG . '../temp/',
    
    // the outer key to extract and write (ie: ['view'] -> 'CONFIG/view.php')
    // optionally, this can be set to '*' to export the entire cache.
    'view',
    
    // optionally, force the filename to something other than the key
    'production.php'
    
    // note that the key to read the new configuration is 'production', and may be read as follows:
    $reader = (new ConfigFileReader(CONFIG . '../temp/'));
    $productionConfig = $reader->read('production');        
);

注意:ConfigFileWriter 类不会以任何方式修改 ConfigFileReader 依赖项缓存。

结果将生成一个新的配置文件('production.php'),它反映了配置的新状态。

最后,可以使用当前的 ConfigFileWriter 配置状态来初始化新的 ConfigFileReader 缓存。

$reader = new ConfigFileReader($writer);

包加载器和配置类

以下类由该包提供

以下支持类包括

加载类的工作方式

概述

  1. LoaderSet 是 ConfigurationSets 的存储库。
  2. ConfigurationSet 是 Configurators 的存储库。
  3. Configurator 是配置项的存储库,并提供加载配置数据和配置服务的方法。

常见的加载器方法

示例 LoaderSet 配置文件

即:config/loaders/container.php

return [
    AurynConfigurationSet::class => [
        // the identifier given to this configuration set.
        'name'        => 'app.di',
        // the path to the folder that contains configuration files
        // for this set.
        'config_path' => CONFIG,
        // the loader priority.
        'priority'    => 'high', # 'high' | 'normal' | 'low' | int
        // the list of Configurators in this set.
        'config'      => [
            // the configurator
            BladeConfigurator::class    => [
                // the identifier for this Configurator
                'name'     => 'blade',
                // the data set loaded by the ConfigFileReader class
                // defaults to '' if not supplied.
                'dataset' => 'view.blade',
                // the set priority. Defaults to 'normal' if not supplied.
                'priority' => 'normal',
                // any settings to add or to override settings from the data set.
                // defaults to [] if not supplied.
                'config'   => ['cargo' => 'shamalam'],
            ],
            TwigConfigurator::class     => ['name' => 'twig', 'dataset' => 'view.twig'],
            MarkdownConfigurator::class => ['name' => 'markdown', 'dataset' => 'view.markdown'],
        ],
    ],
    IlluminateConfigurationSet::class => [
        'name' 			=> 'app.container',
        'config_path' 	=> CONFIG,
        'priority' 		=> 'high',
        'config' 		=> [
            BladeConfigurator::class    => [
                'name'     => 'blade',
                'dataset' => 'view.blade',
            ],
            TwigConfigurator::class     => ['name' => 'twig', 'dataset' => 'view.twig'],
            MarkdownConfigurator::class => ['name' => 'markdown', 'dataset' => 'view.markdown'],
        ],
    ],
];

一个描述性的示例:

$config = new ConfigFileReader(CONFIG . 'loaders/');
$reflector = new LoaderReflector;

// load the dependency injector
(new LoaderSet($reflector, 'container', $config->read('container')))
    ->loadAll()->configure;

// load the configuration for an application
(new LoaderSet($reflector, 'application', $config->read('app')))
    ->loadAll()->configure;

**-- or --**

// load the configuration for an api
(new LoaderSet($reflector, 'api', $config->read('api')))
    ->loadAll()->configure();

要访问单个集合和配置器

$loader['app.di'] 返回包含第一个集合的数组。(AurynConfigurationSet

$loader['app.di']['set'] 返回 AurynConfigurationSet 的实例。

$loader['app.di']['set']['blade'] 返回 BladeConfigurator 的实例。

$loader['app.di']['set']['blade']->getSettings() 返回加载的 BladeConfigurator 设置。

$loader['app.di']['set']->getConfigurators() 返回包含元信息的 Configurators 数组。以下是基于上述示例的结果样本

   |  blade => array (4)
   |  |  configurator => BladeConfigurator #c6f0
   |  |  |  dataset protected => "view.blade" (10)
   |  |  |  key protected => "blade" (5)
   |  |  |  priority protected => 100
   |  |  |  settings protected => array (2)
   |  |  |  |  enabled => TRUE
   |  |  |  |  defaults => array (2)
   |  |  |  |  |  cache => ".../cache/blade" (42)
   |  |  |  |  |  template_paths => array (7)
   |  |  |  |  |  |  0 => ".../views/" (51)
   |  |  |  |  |  |  1 => ".../views/assets/" (58)
   |  |  |  |  |  |  2 => ".../views/templates/default/" (69)
   |  |  |  |  |  |  3 => ".../views/templates/default/forms/" (75)
   |  |  |  |  |  |  4 => ".../views/templates/default/pages/" (75)
   |  |  |  |  |  |  5 => ".../views/templates/" (61)
   |  |  |  |  |  |  6 => ".../views/debug/" (57)
   |  |  configured => TRUE
   |  |  loaded => TRUE
   |  |  profile => array (3)
   |  |  |  added => 1469154406.5784
   |  |  |  loaded => 1469154423.0254
   |  |  |  configured => 1469154423.0259
   |  twig => array (4)
   |  |  configurator => TwigConfigurator #f348
   |  |  |  dataset protected => "view.twig" (9)
   |  |  |  key protected => "twig" (4)
   |  |  |  priority protected => 100
   |  |  |  settings protected => array (2)
   |  |  |  |  enabled => TRUE
   |  |  |  |  defaults => array (5)
   |  |  |  |  |  type => 4
   |  |  |  |  |  filesystem => array (7)
   |  |  |  |  |  |  0 => ".../views/" (51)
   |  |  |  |  |  |  1 => ".../views/assets/" (58)
   |  |  |  |  |  |  2 => ".../views/templates/default/" (69)
   |  |  |  |  |  |  3 => ".../views/templates/default/forms/" (75)
   |  |  |  |  |  |  4 => ".../views/templates/default/pages/" (75)
   |  |  |  |  |  |  5 => ".../views/templates/" (61)
   |  |  |  |  |  |  6 => ".../views/debug/" (57)
   |  |  |  |  |  options => array (4)
   |  |  |  |  |  |  cache => ".../cache/twig" (41)
   |  |  |  |  |  |  debug => NULL
   |  |  |  |  |  |  auto_reload => NULL
   |  |  |  |  |  |  strict_variables => NULL
   |  |  |  |  |  templates => array ()
   |  |  |  |  |  form => array (1)
   |  |  |  |  |  |  templates => array (1)
   |  |  |  |  |  |  |  0 => "bootstrap_3_horizontal_layout.html.twig" (39)
   |  |  configured => TRUE
   |  |  loaded => TRUE
   |  |  profile => array (3)
   |  |  |  added => 1469154406.5786
   |  |  |  loaded => 1469154423.0255
   |  |  |  configured => 1469154423.0259
   |  markdown => array (4)
   |  |  configurator => MarkdownConfigurator #4e55
   |  |  |  dataset protected => "view.markdown" (13)
   |  |  |  key protected => "markdown" (8)
   |  |  |  priority protected => 100
   |  |  |  settings protected => array (1)
   |  |  |  |  defaults => array (5)
   |  |  |  |  |  class => "MarkdownExtra" (13)
   |  |  |  |  |  template_paths => array (4)
   |  |  |  |  |  |  0 => ".../views/templates" (60)
   |  |  |  |  |  |  1 => ".../views/templates/forms" (66)
   |  |  |  |  |  |  2 => ".../views/templates/default" (68)
   |  |  |  |  |  |  3 => ".../views/templates/default/hello" (74)
   |  |  |  |  |  debug => TRUE
   |  |  |  |  |  html5 => TRUE
   |  |  |  |  |  keepListStartNumber => TRUE
   |  |  configured => TRUE
   |  |  loaded => TRUE
   |  |  profile => array (3)
   |  |  |  added => 1469154406.5787
   |  |  |  loaded => 1469154423.0255
   |  |  |  configured => 1469154423.0259

LoaderSet 配置定义

LoaderSet 配置文件的结构如下

[ <class name of ConfigurationSet> => [ <name>, <config_path>, <priority>, <config> ]
    where priority is optional.

ConfigurationSet 配置定义(<config>

许可证:MIT

版权所有 © 2016,Greg Truesdell - Formula Nine 框架的一部分。