worksome/foggy 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

worksome/foggy

Composer 安装命令:

composer require worksome/foggy

包简介

Foggy is a tool for making database dumps with some data removed/changed.

README 文档

README

Foggy is a package which creates a database dump of your database, but with scrubbed data. Perfect for taking a production database and use locally.

Install

For installation via Composer

composer require worksome/foggy

For installation in a Laravel application

composer require worksome/foggy-laravel

Usage

For usage with Laravel, read more in Laravel Foggy docs.

Configuration

The configuration lies in a JSON file, which has to adhere to the schema.json file. You can validate your configuration file on jsonschemavalidator.net or directly in an IDE such as PhpStorm.

For the most basic configuration, where all tables are dumped, but no data is scrubbed, we simply do:

{
  "database": {
    "*": {
      "withData": true
    }
  }
}

Here we have specified that all tables and views (*) should be dumped with data by default. A more secure default would be to set withData to false, so only schema definitions are exported, if nothing specific is specified.

Defining rules for a table

All table definitions live inside database key in the JSON object. Each table can have an array of rules. A rule consists of the following:

  • column - Which column to apply the rule to.
  • type - Which rule type to use.
  • value - The value supplied to the rule type.
  • times (optional) - The number of times this rule is allowed to be applied.
  • params (optional) - A list of static parameters supplied to the rule.
  • condition (optional) - Add a condition which has to pass before the rule is applied.

In the following snippet we have added some rules for the users table. It shows a quick example of some rules and parameters.

{
  "database": {
    "users": {
      "withData": true,
      "rules": [
        {
          "$comment": "Generate a fake name for all users, except if they are our own employees",
          "column": "name",
          "type": "faker",
          "value": "name",
          "condition": "!str_contains($row['email'], '@worksome.com')"
        },
        {
          "$comment": "Replace all avatars with a fake image based on their user id.",
          "column": "avatar",
          "type": "php",
          "value": "\"https://api.adorable.io/avatars/285/{$row['id']}.png\""
        },
        {
          "$comment": "Replace all passwords with `secret`",
          "column": "password",
          "type": "replace",
          "value": "$2y$10$xmVOYC1DUte0oG86Zz8oeeKc3UXZNrdSKMoZGrCElup6VexStFh22"
        }
      ]
    }
  }
}

Rules

Each table can have an array of rules. Rules are applied to a specific column and can modify the values in that column per row.

Faker

The faker rule is to replace the value with a new fake value. It uses the faker library underneath, so all formatters available in faker can be used here.

For calling a simple faker property, simply specify value as the property you want to use. In the following example we are calling the email faker.

{
  "column": "some_column",
  "type": "faker",
  "value": "email"
}

Sometimes you might want to use Faker formatters that take arguments. Arguments can be supplied by using the params key in the JSON object. In the following example we specify that we only want to generate male names.

{
  "column": "some_column",
  "type": "faker",
  "value": "firstName",
  "params": "male"
}

Replacer

The replacer rule replaces a column with the given value. It's a simple rule for when you just want all entries to have the same value. A great use-case is for setting all passwords to the same value, so when using the scrubbed database, you can log in on all users with the same password. In the following example we replace all passwords with secret, but a hashed edition of it.

{
  "column": "password",
  "type": "replace",
  "value": "$2y$10$xmVOYC1DUte0oG86Zz8oeeKc3UXZNrdSKMoZGrCElup6VexStFh22"
}

PHP

The PHP rule is a basic, but really powerful rule. It allows you to define a PHP string which will be applied to the column. This string has a few variables which can be accessed.

  • value - this variable will hold the current value of the column which the rule should be applied to.
  • row - This variable will hold the current values of the whole row which the rule should be applied to.

The PHP string will be evaluated, and the value returned from it will be the new value of the column. It is not needed to write return, as the statement is wrapped in a return automatically.

{
  "column": "ip",
  "type": "php",
  "value": "sha1($value)"
}

Conditions

Times

It is possible to limit a column to only be applied x amount of times, by supplying an argument named times. This will limit, so the rule is only applied until the times are hit.

SQL Views

All views definitions live inside database key in the JSON object. In opposition to tables, views do not have any particular rules applicable to them.

The only requirement is for them to be listed in the JSON object to be included in the import. The wildcard configuration * will include them all.

{
  "database": {
      "stock_quantity_view": {}
  }
}

worksome/foggy 适用场景与选型建议

worksome/foggy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 596.34k 次下载、GitHub Stars 达 26, 最近一次更新时间为 2020 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 worksome/foggy 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 596.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 26
  • 点击次数: 14
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 26
  • Watchers: 12
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-14