定制 imjoehaines/flowder 二次开发

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

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

imjoehaines/flowder

Composer 安装命令:

composer require --dev imjoehaines/flowder

包简介

A simple and extensible fixture loader for PHP 7.3+, supporting SQLite and MySQL

README 文档

README

Flowder is a (really) simple fixture loader for PHP 7.3+, supporting SQLite and MySQL.

Using Flowder in PHP 7.2 or below? Try version 1 instead!

NB: If you're looking to use Flowder in a project, you probably want to use an exisiting framework integration:

Basic Concepts

Flowder is built with three basic building blocks; Loaders, Truncators and Persisters.

A Loader is responsible for taking a thing to load (such as a file or directory) and converting it into an array of data that can be persisted.

A Truncator is responsible for ensuring all the database tables where data will be persisted are empty before persisting the data.

A Persister is responsible for taking the data provided by a Loader and inserting it into the database.

These three concepts are represented by the following interfaces:

Building your own Loaders, Truncators and Persisters is as easy as creating a class that implements ones of these interfaces.

Usage

Loading fixtures with Flowder takes a few lines of code — it just requires a Loader, Truncator and Persister.

For example, to load a single PHP file into a SQLite in-memory database, the following file could be used

$db = new PDO('sqlite::memory:');

$flowder = new Imjoehaines\Flowder\Flowder(
    new Imjoehaines\Flowder\Loader\PhpFileLoader(),
    new Imjoehaines\Flowder\Truncator\SqliteTruncator($db),
    new Imjoehaines\Flowder\Persister\SqlitePersister($db)
);

$flowder->loadFixtures('test_data.php');

Provided Classes

Flowder

Flowder is the main class you will be using. It is responsible for orchestrating the loading, truncating and persisting processes.

As seen in the example above, it is constructed with three arguments — an instance of LoaderInterface, an instance of TruncatorInterface and an instance of PersisterInterface.

After construction, call loadFixtures and pass it a thing to load in order to persist the data. For example, using the PhpFileLoader you would pass loadFixtures the path to a PHP file.

Loaders

Flowder comes bundled with three Loaders:

PhpFileLoader

This Loader takes a PHP file name, requires it and uses a returned array of data as the data to be persisted. The file name itself is used as the table name (ignoring the file extension).

For example, given the following example_table.php file

return [
    [
        'column1' => 1,
        'column2' => 2,
    ],
    [
        'column1' => 4,
        'column2' => 5,
    ],
];

Then when loaded with the PhpFileLoader, it would return the following PHP array

[
    'example_table' => [
        [
            'column1' => 1,
            'column2' => 2,
        ],
        [
            'column1' => 4,
            'column2' => 5,
        ],
    ],
]

DirectoryLoader

The DirectoryLoader is a decorator around another Loader instance that will run the Loader's load method for each file in the directory provided to DirectoryLoader::load.

For example, the following code will load all files in /some/directory using the PhpFileLoader

$loader = new DirectoryLoader(
    new PhpFileLoader()
);

$data = $loader->load('/some/directory');

CachingLoader

The CachingLoader is another decorator that caches the result of it's load method so that repeated calls to load the same thing will return the same result as it did on the first call to load.

Extending the above example, we can use the following code to load all files in /some/directory using the PhpFileLoader, but only actually hit the disk on the first time through the for loop. All other iterations will simply return the cached value

$loader = new CachingLoader(
    new DirectoryLoader(
        new PhpFileLoader()
    )
);

for ($i = 0; $i < 100; $i++) {
    $data = $loader->load('/some/directory');
}

It is usually a good idea to use the CachingLoader whenever you are loading the same resource more than once as it can dramatically speed up fixture loading.

Additional Loaders

For loading file formats other than PHP, take a look at the JSON or YAML loaders:

Truncators

Flowder comes with two Truncator classes:

MySqlTruncator

This Truncator takes a table name and runs a MySQL TRUNCATE TABLE query on it. It will disable foreign key checks before the truncate and enable them afterwards, to ensure ordering of truncation does not matter. It is your responsibility to make sure this does not leave your database in an inconsistent state after all of your fixtures run.

SqliteTruncator

This Truncator takes a table name and runs a DELETE FROM query on it. Like the MySqlTruncator, it will disable foreign key checks before the truncate and enable them afterwards.

Persisters

MySqlPersister

The MySqlPersister takes a table name and array of data and converts it into a single INSERT query. Like the MySqlTruncator it will disable foreign key checks before the insert and enable them afterwards to ensure the ordering of inserts does not matter. It is your responsibility to make sure this does not leave your database in an inconsistent state after all of your fixtures run.

SqlitePersister

The SqlitePersister is functionally identical to the MySqlPersister, but inserts data a row at a time inside a transaction instead of building a single INSERT query. This is to get around SQLite's SQLITE_MAX_VARIABLE_NUMBER limitation.

imjoehaines/flowder 适用场景与选型建议

imjoehaines/flowder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 629.47k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2017 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 629.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 21
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: Unlicense
  • 更新时间: 2017-03-21