minnis/neon-tools
Composer 安装命令:
composer require minnis/neon-tools
包简介
NEON tools. A handy (and super fast) NEON fileloader class for PHP.
关键字:
README 文档
README
NEON tools. A handy (and super fast) NEON fileloader class for PHP.
Jump to:
Install
The package can be installed using composer:
composer require minnis/neon-tools
Usage
We really like YAML but we are in love with NEON.
You probably know Symfony's Yaml component and its parseFile function.
$array = Yaml::parseFile('/path/to/file.yaml');
We don't know why but the nette/neon package is not shipped with a fileloader. So we created our own.
Quick Start
Simply swap Yaml::parseFile() with NeonLoader::parseFile()
Example using the static function:
<?php
use MINNIS\NeonTools\NeonLoader;
require_once __DIR__ . '/../vendor/autoload.php';
$array = NeonLoader::parseFile('/path/to/file.neon');
And for those who prefer the Object Oriented approach:
<?php
use MINNIS\NeonTools\NeonLoader;
require_once __DIR__ . '/../vendor/autoload.php';
$NeonLoader = new NeonLoader();
$array = $NeonLoader->fromFile('/path/to/file.neon');
Cache
When NEON/YAML is used as a way to manage settings/configuration performance becomes important. Especially if it is parsed on every request.
If you are using symfony/yaml, mustangostang/spyc or nette/neon as stand-alone package (without a framework) your need to manage caching yourself. And who has time for that.
We created a simple but fast caching system within the fileloader. It requires no config and is enabled by default. It just works.
If you are using git on your environment we recommend adding *.cache to your .gitignore file.
When the cache file is used the modification timestamp of the original file (and all files it imports) is checked. Simply edit the original files and all changes are detected automaticly.
You can still use fileloader without the caching, like this:
//static
$array = NeonLoader::parseFileWithoutCache('/path/to/file.neon');
//object oriented
$NeonLoader = new NeonLoader(false); //the false parameter disables caching
Performance
NEON has a very good balance between ease of use, features and performance.
It must be said YAML has some cool advanced features. However, that comes at a price when it comes to perfomance.
We did some benchmarking and the results are spectaculair! Not only is NEON (the neon parser) twice as fast YAML (symfony's yaml parser). The caching functionality we created is about 7.5 times faster as runtime parsing.
1000 runs
| PHP 5.6 | PHP 7.1 | PHP 7.3 | |
|---|---|---|---|
| NEON (cached) | 0.5 sec | 0.2 sec | 0.18 sec |
| NEON (uncached) | 2.9 sec | 1.5 sec | 1.03 sec |
| YAML (symfony) | 5.9 sec | 3.1 sec | 2.22 sec |
Want to do benchmarking of your own? We added a benchmarking script and test neon files in the ./tests/ folder.
Imports
A very nice feature of YAML is to link files using the imports array. Since nette/neon has no fileloader this behaviour was not available to NEON. So, we added it to neon-tools.
We made the YAML notation available so no need to alter your files:
imports:
- { resource: second_file.neon }
- { resource: third_file.neon }
And since we like things simple, this just works:
imports:
- second_file.neon
- third_file.neon
The import workds recursive. So you can import a file that imports a file that imports a file that .... well, you get the point.
Options
NEON is, like YAML, often used as a configurations file. In some occasions you do not want (or need) the full contents of the file.
Lets take this example configuration file: example.neon
database:
host: localhost
port: 3306
user: someuser
pass: somepass
charset: utf8mb4
third_party_api:
bearer: abc123abc123abc
userid: 238473
nsa_supersecret_api:
api_key: root
api_token: root
Include
The (optional) second parameter can be used to include only selected root keys. This can either be a string or an array.
$array = NeonLoader::parseFile('/path/to/example.neon', 'database');
//result:
array(1) {
['database']=>
array(5) {
...
}
}
Exclude
The (optional) third parameter can be used to exclude selected root keys. This can either be a string or an array.
$array = NeonLoader::parseFile('/path/to/example.neon', null, ['nsa_supersecret_api', 'cia_alsosecret_api']);
//result:
array(2) {
['database']=>
array(5) {
...
}
['third_party_api']=>
array(2) {
...
}
}
Parameters
Not part of Symfony's Yaml parser but a feature of the Symfony framework itself is the possibility to use placeholders(variables) in config files. When used with imports it keeps your config nice, tidy and seperated. Since we like nice and tidy we added this feature to neon-tools as well.
A placeholder is defined by surrounding it with %, like this: %i_am_a_placeholder%. The actual value is retrieved from the parameters array.
#application.neon
imports:
- environment.neon
database:
user: %db_username%
pass: %db_password%
host: %db_host%
options: %db_options%
#environment.neon
parameters:
db_username: ROOT
db_password: SECRET
db_host: localhost
db_options:
char_set: utf8mb4
backup: false
When parsed the result will look like this:
['database' => [
'user' => "ROOT",
'pass' => "SECRET",
'host' => "localhost",
'options' => [
'charset' => "utf8mb4",
'backup' => false
]
]
The parameters array is merged with every import. So you can use the parameters array in every imported file you want. Just make sure the key is unique. The placeholder itself can be used as often as you like.
Because imports are parsed before parameters you cannot use placeholders within the imports array to create dynamic paths/filenames.
Neon
NEON is very similar to YAML.The main difference is that the NEON supports entities (so can be used e.g. to parse phpDoc annotations, DI services, …) and both spaces and tabs for indentation. NEON syntax is a little simpler and the parsing is faster.
Try NEON in sandbox!
minnis/neon-tools 适用场景与选型建议
minnis/neon-tools 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 09 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「markup」 「nette」 「neon」 「spyc」 「toml」 「config loader」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 minnis/neon-tools 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 minnis/neon-tools 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 minnis/neon-tools 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
A Processor for Markup written in PHP. Allows extraction of Markup into a data structure, orchestrated nested manipulation of said structure, and output as (optimized) Markup.
Load configuration files the easy way. This configuration loader supports PHP, INI, JSON, XML, YAML and NEON file extensions.
A loader that is able to load various types of configuration files
Easy translating of PHP applications
Nette Framework adapter for I18n package
统计信息
- 总下载量: 46
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-17