cwmoss/doda

从文件中加载特定领域数据。快速且简单

v1.0.0 2022-09-21 14:40 UTC

This package is not auto-updated.

Last update: 2024-09-19 00:42:25 UTC


README

从文件或函数中加载您的特定领域数据,懒加载或不懒加载

为什么?

我需要一个快速简单的方法来加载我的应用程序中更静态的只读数据。这种数据我不想放在数据库中。也许应用程序本身就不需要数据库。

功能

  • 多种数据格式:yaml、json、ini、序列化php、php
  • 从文件中懒加载
  • 从匿名函数中懒加载
  • 缓存(序列化php作为最快的反序列化格式)

安装

此包依赖于 "symfony/yaml"

composer require cwmoss/doda

使用方法

require_once("vendor/autoload.php");
use cwmoss\doda;

$domain = new doda(__DIR__.'/config/domain-data.yaml');

$country = $domain->get('country_codes.fr'); // "France"

示例yaml文件

contact_options:
    - via email
    - via phone
contact_phone: 555 321654
country_codes: !file cc.json

API

constructor()

new doda($entrypoint_file, (optional)[array_of_callback_functions]);

entrypoint_file 应该是yaml文件或缓存文件。

get($path, $default=null)

$path 是点分路径到您数据或路径段数组

# "country_code.fr" is the same as ['country_code', 'fr']

如果路径中的键在您的数据中不存在,将返回默认值。

write_cache()

您可以php序列化之前解析的文件,然后将其用作 entrypoint_file

您可以从命令行编译

php vendor/cwmoss/doda/src/doda.php config/your-domain-data-file.yaml

yaml特定

import

您的yaml文件可以包含顶层魔术键 import。在这里,您可以列出所有希望加载并合并到文件中的数据。

import:
    - imports/categories.yaml
    - imports/countries.db
    - ../fruits-folder/fruits.php

!file

此yaml标签在需要访问数据时从文件加载数据(懒加载)。

country_codes: !file cc.json

请参阅 tests/data 文件夹中的更多示例

!fun

此yaml标签在需要访问数据时从函数调用加载数据(懒加载)。函数名称应在实例化期间位于 $functions 数组中。

country_codes: !fun load_country_codes

请参阅 tests/data 文件夹中的更多示例

测试

运行php单元测试

phpunit

许可证

cwmoss/doda 在MIT公共许可证下发布。有关详细信息,请参阅附带的 LICENSE 文件。