aedart / config-loader
Requires
- php: >=7.1.0
- aedart/laravel-helpers: ~5.0
- illuminate/config: 5.6.*
- illuminate/filesystem: 5.6.*
- symfony/yaml: ~4.0
Requires (Dev)
- aedart/license: 1.*
- aedart/license-file-manager: ~2.0
- aedart/testing-laravel: ~4.0
README
弃用 - Config Loader
此包已被 aedart/athenaeum 替换
一个用于加载各种类型的配置文件并将它们解析为 Laravel 配置存储库的实用工具。此包包含一组默认文件解析器,可以应用。但是,如果您需要不同类型的解析器,则可以“简单地”创建一个,并将其添加到加载器中。
内容
何时使用此工具
- 当您需要从文件加载配置时
- (当您需要从多个文件加载配置时)
如何安装
composer require aedart/config-loader
此包使用 composer。如果您不知道它是做什么的或它是如何工作的,我建议您在使用此包之前先了解一些相关信息。
快速入门
支持的文件类型
文件扩展名 | 解析器 |
---|---|
*.ini | \Aedart\Config\Loader\Parsers\Ini |
*.json | \Aedart\Config\Loader\Parsers\Json |
*.php (php 数组) | \Aedart\Config\Loader\Parsers\PHPArray |
*.yml (也支持 *.yaml) | \Aedart\Config\Loader\Parsers\Yaml |
Laravel 项目内
服务提供者
首先需要注册服务提供者;
\Aedart\Config\Loader\Providers\ConfigurationLoaderServiceProvider::class
加载配置
指定服务提供者后,您可以使用加载器。在以下示例中,提供了一个目录路径,并将该目录中的文件加载并解析;
use Aedart\Config\Loader\Loaders\ConfigLoader; // Path to some directory that contains the configuration file(s) $path = 'config/'; // Create a new loader instance $loader = new ConfigLoader($path); // Load and parse the configuration files // NB: Is added directly to Laravel's configuration, can be accessed normally via the Config facade... $loader->load(); // Obtain the configuration repository $config = $loader->getConfig();
Laravel 项目外
当在 Laravel 项目外工作,您需要创建更多实例,这是加载器所依赖的;
use Aedart\Config\Loader\Loaders\ConfigLoader; use Aedart\Config\Loader\Factories\DefaultParserFactory; use Illuminate\Config\Repository; use Illuminate\Filesystem\Filesystem; // Path to some directory that contains the configuration file(s) $path = 'config/'; // Create a new loader instance $loader = new ConfigLoader($path); // Specify the required dependencies $loader->setConfig(new Repository()); $loader->setFile(new Filesystem()); $loader->setParserFactory(new DefaultParserFactory()); // Load and parse the configuration files $loader->load(); // Obtain the configuration repository $config = $loader->getConfig();
加载并解析单个文件
您还可以加载和解析单个文件,而不是整个目录;
// Provided you have created an instance of the configuration loader, // simply specify the full path to the configuration file $config = $loader->parse('config/users.yml');
如何访问配置
如果您不熟悉Laravel的配置存储库,请查阅其文档。
PHP数组配置文件示例
(config/users.php)
return [ 'message' => 'Hallo World' ];
访问message
的示例
// ... (loading and parsing not shown) ... // Fetch the message key $message = $config->get('users.message'); echo $message; // Outputs 'Hallo World'
自定义配置文件解析器
如果您需要一个自定义解析器,则可以通过实现\Aedart\Config\Loader\Contracts\Parsers\Parser
接口来创建一个。
但是,您还需要将解析器添加到解析器工厂
中。请查阅\Aedart\Config\Loader\Contracts\Factories\ParserFactory
接口,以及此包中提供的默认工厂:\Aedart\Config\Loader\Factories\DefaultParserFactory
贡献
您是否发现了缺陷(错误或设计缺陷),或者希望改进?在以下章节中,您可能会找到一些有关如何帮助此项目的一些有用信息。无论如何,我感谢您抽出时间帮助我改进这个项目的成果和整体质量。
错误报告
如果您确信您已经发现了一个错误,那么您至少应该创建一个新的问题。在该问题中,您至少应该描述以下内容;
- 缺陷位于何处
- 对缺陷的良好、简短和精确的描述(为什么它是缺陷)
- 如何复现缺陷
- (如何解决缺陷的可能解决方案)
当有时间时,我将审查您的问题并采取行动。
分支代码并发送拉取请求
一个好的、写得好的错误报告会对我有很大帮助。然而,如果您能够或希望自行解决缺陷,以下是如何操作的;
- 分支此项目
- 为给定缺陷修复创建新的本地开发分支
- 编写您的代码/更改
- 创建可执行的测试用例(证明您的更改是可靠的!)
- 提交并将您的更改推送到您的分支存储库
- 发送包含您的更改的拉取请求
- 喝一杯啤酒 - 您应得的 :)
在我收到拉取请求(并且有时间处理)后,我将审查您的更改并将它们合并到这个项目中。如果没有,我将通知您为什么选择不合并。
致谢
- Taylor Otwell,感谢他创建了Laravel,尤其是我每天都在使用的服务容器
- Jeffrey Way,感谢他创建了Laracasts - 一个学习新事物的好地方... 例如,如何使用配置存储库
版本
本包遵循 语义版本控制 2.0.0
许可证
BSD-3-Clause,阅读本包内包含的 LICENSE 文件