butschster/entity-faker
Composer 安装命令:
composer require butschster/entity-faker
包简介
Create fake entities based on your classes
README 文档
README
This package will help you generate fake entities and persist them to your ORM.
<?php use Butschster\EntityFaker\LaminasEntityFactory; use Laminas\Hydrator\ReflectionHydrator; use Faker\Factory as Faker; $factory = new \Butschster\EntityFaker\Factory( new LaminasEntityFactory( new ReflectionHydrator() ), Faker::create() ); class User { private string $id; private string $username; private string $email; public function __construct(string $id, string $username, string $email) { $this->id = $id; $this->username = $username; $this->email = $email; } } class SuperUser extends User { private bool $isAdmin = false; public function __construct(string $id, string $username, string $email, bool $isAdmin) { parent::__construct($id, $username, $email); $this->isAdmin = $isAdmin; } } $factory->define(User::class, function (Faker $faker, array $attributes) { return [ 'id' => $faker->uuid, 'username' => $faker->username, 'email' => $faker->email ]; }); $factory->define(SuperUser::class, function (Faker $faker, array $attributes) use($factory) { $userAttributes = $factory->raw(User::class); return $userAttributes + [ 'isAdmin' => $faker->boolean ]; });
Create and persist an entity
$user = $factory->of(User::class)->create(); //class User { // private string $id = "0b13e52d-b058-32fb-8507-10dec634a07c"; // private string $username = "zetta86"; // private string $email = "tsteuber@hotmail.com"; //}
Create and persist multiply entities
$users = $factory->of(User::class)->times(10)->create(); //[ // class User { // private string $id = "0b13e52d-b058-32fb-8507-10dec634a07c"; // private string $username = "zetta86"; // private string $email = "tsteuber@hotmail.com"; // }, // ... //]
Create and persist an entity with predefined attributes
$user = $factory->of(User::class)->create([ 'email' => 'admin@site.com' ]); //class User { // private string $id = "0b13e52d-b058-32fb-8507-10dec634a07c"; // private string $username = "zetta86"; // private string $email = "admin@site.com"; //}
Create an entity
$user = $factory->of(User::class)->make(); //class User { // private string $id = "0b13e52d-b058-32fb-8507-10dec634a07c"; // private string $username = "zetta86"; // private string $email = "tsteuber@hotmail.com"; //}
Create multiply entities
$users = $factory->of(User::class)->times(10)->make(); //[ // class User { // private string $id = "0b13e52d-b058-32fb-8507-10dec634a07c"; // private string $username = "zetta86"; // private string $email = "tsteuber@hotmail.com"; // }, // ... //]
Create an entity with predefined attributes
$user = $factory->of(User::class)->make([ 'email' => 'admin@site.com' ]); //class User { // private string $id = "0b13e52d-b058-32fb-8507-10dec634a07c"; // private string $username = "zetta86"; // private string $email = "admin@site.com"; //}
Get raw attributes for entity
$attributes = $factory->of(SuperUser::class)->raw(); //[ // 'id' => "0b13e52d-b058-32fb-8507-10dec634a07c", // 'username' => 'zetta86', // 'email' => 'tsteuber@hotmail.com', //]
Get raw attributes for entity with predefined values
$attributes = $factory->of(SuperUser::class)->raw([ 'email' => 'test@site.com' ]); //[ // 'id' => "0b13e52d-b058-32fb-8507-10dec634a07c", // 'username' => 'zetta86', // 'email' => 'test@site.com', //]
Generate array of all defined entities
$repository = $factory->make(1000); $seeds = $repository->get(User::class)->random(100); $seeds = $repository->get(SuperUser::class)->take(50);
Generate array of raw data for all defined entities
$repository = $factory->raw(1000); $seeds = $repository->get(User::class)->random(100); $seeds = $repository->get(SuperUser::class)->take(50);
Export array of raw data to a file for given entity to php file
$path = $factory->of(SuperUser::class)->times(1000)->export('path/to/store'); // path/to/store/SuperUser.php
Export array of raw data to a files for all defined entities
$repository = $factory->export('path/to/store', 1000); $seeds = $repository->get(User::class)->random(100);
Custom entity builder
You can define your own EntityBuilder class with custom persist logic.
use Butschster\EntityFaker\EntityFactoryInterface; use Faker\Factory as Faker; use Cycle\ORM\ORMInterface; use Cycle\ORM\TransactionInterface; class CycleOrmEntityFactory implements EntityFactoryInterface { private array $afterCreation = []; private array $beforeCreation = []; protected ORMInterface $orm; protected Transaction $transaction; public function __construct(ORMInterface $orm) { $this->orm = $orm; $this->beforeCreation(function () { $this->transaction = new Transaction($this->orm); }); $this->afterCreation(function () { $this->transaction->run(); }); } public function store(object $entity): void { $this->transaction->persist($entity); } public function hydrate(object $entity, array $data): object { return $this->orm->getMapper($entity)->hydrate($entity, $data); } /** * Add a callback to run after creating an entity or array of entities. * @param callable $callback */ public function afterCreation(callable $callback): void { $this->afterCreation[] = $callback; } public function afterCreationCallbacks(): array { return $this->afterCreation; } /** * Add a callback to run before creating an entity or array of entities. * @param callable $callback */ public function beforeCreation(callable $callback): void { $this->beforeCreation[] = $callback; } public function beforeCreationCallbacks(): array { return $this->beforeCreation; } } $factory = new \Butschster\EntityFaker\Factory( new CycleOrmEntityFactory(...), Faker::create() );
butschster/entity-faker 适用场景与选型建议
butschster/entity-faker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49.65k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2021 年 07 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「faker」 「seeder」 「entity-generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 butschster/entity-faker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 butschster/entity-faker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 butschster/entity-faker 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Philippines region, province, cities/municipalities and barangays Laravel migration and table seeder.
Symfony bundle to manage fixtures with Alice and Faker.
Versioned, environment-based Seeders in Laravel. Used while original does not update to Laravel 5.7
Additional plugin for fakerphp/faker that allows you to generate a random animal
Faker Japanese is a Faker provider that generates fake Japanese related data for you.
A model factory package for Laravel 4 with expressive API for creating custom tailored dummy objects
统计信息
- 总下载量: 49.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 13
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-15