定制 clabonte/faker-config 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

clabonte/faker-config

Composer 安装命令:

composer require clabonte/faker-config

包简介

Faker extension to populate entities via a simple JSON configuration file

README 文档

README

FakerConfig is a Faker extension to populate entities via a simple JSON configuration file.

FakerConfig provides an easy way to configure the format to use when generating data for a given entity/property combination.

Build Status

With this extension, one can create a simple JSON configuration file to describe how to format various Entities and their properties. FakerConfig will parse Faker's Generator PHPDoc used to identify the list of valid formats that can be used by the configuration file and will validate the configuration file against to to reject any format that won't be understood by Faker

Table of Contents

Configuration File

The configuration is done via a very simply JSON file that lists out entities to populate as JSON objects ('*' = wildcard). For each entity, you simply list out the properties you want to populate along with the format to use.

Here is a sample configuration file:

{
  "*": {
    "id": "uuid"
  },

  "Book": {
    "id": "isbn10"
  },

  "Entity": {
    "property1": "url",
    "property2": "numberBetween(0,10)"
  },

  "Package\\Entity": {
    "property1": "city",
    "property2": "words(5, true)"
  }
}

Sample configuration files are available in the project:

Step 1. Create the ConfigGuesser

The first step consists in creating a ConfigGuesser object with the generator to use:

$generator = \Faker\Factory::create();
$guesser = new \FakerConfig\ConfigGuesser($generator);

Step 2. Load the Configuration File

Then, you need to tell the guesser the list of entities/properties that need to be formatted when populating data.

The easiest way to do so is by loading your JSON configuration file:

\FakerConfig\ConfigGuesserLoader::loadFile($guesser, 'path_to_your_config.json');

Alternate Solution: Configure the Guesser Programmatically

Alternatively, you can also configure the guesser programmatically using the FormatParser:

$parser = new \FakerConfig\Parser\FormatParser();
$parser->load($guesser->getGenerator());

// You can use any property defined in the Generator's PhpDoc
$format = $parser->parse("firstName");
$guesser->addFormat('Entity', 'property1', $format);

// Or any method defined in the Generator's PhpDoc
$format = $parser->parse("numberBetween(0,10)");
$guesser->addFormat('Entity', 'property2', $format);

// Wildcard define format to use for a given property for any entity
$format = $parser->parse("uuid");
$guesser->addFormat(\Faker\Guesser\ConfigGuesser::WILDCARD, 'id', $format);

// Specific entity/property format will always take precedence over a wildcard format
$format = $parser->parse("isbn10");
$guesser->addFormat('Book', 'id', $format);

Step 3. Populate Your Entity

Once the ConfigGuesser has been properly configured, you can use it with a populator to fill your entity. FakeConfig provides 2 populators to do so:

  • ObjectPopulator: To populate an object entity
  • ArrayPopulator: To populate an associative array entity

Populate an Object Entity

The ObjectPopulator can be used to populate any object automatically based on its class hierarchy. The populator will scan the object class and all of its ancestor to identify the list of properties that must be populated and apply the format defined in your configuration.

/* Assuming the Book class has the following properties:
   - id
   - property1
   - property2

  And the ConfigGuesser has been configured with the following JSON:
  { 
    "Book": {
        "id": "isbn10",
        "property1": "words(5, true)"
    }
  }
 */

// The following would populate the Book object as follow:
// - 'id' = random ISBN 
// - 'property1' = random string of 5 words
// - 'property2' = no update

$populator = new \FakerConfig\Populator\ObjectPopulator($generator, $guesser);
$book = new Book();
$populator->populate($book); 

Populate an Array Entity

You can also populate any associative array using a similar approach:

$array = array(
    'id' => null,
    'property1' => null,
    'property2' => null,
    'property3' => null);

/*
  Assuming the ConfigGuesser has been configured with the following JSON:
  { 
    "*": {
      "id": "uuid",
    },
    "Entity": {
        "property1": "name",
        "property2": "numberBetween(0,10)"
     }
  }
 */
 
// The following would populate the array as an 'Entity' entity as follow:
// - 'id' = random UUID 
// - 'property1' = random name
// - 'property2' = random number between 0 and 10
// - 'property3' = no update

$populator = new \FakerConfig\Populator\ArrayPopulator($generator, $guesser);
$populator->populate($array, 'Entity'); 

clabonte/faker-config 适用场景与选型建议

clabonte/faker-config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 172 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 09 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「faker」 「fixtures」 「data」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 clabonte/faker-config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 clabonte/faker-config 我们能提供哪些服务?
定制开发 / 二次开发

基于 clabonte/faker-config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-07