Defective Code Logo

Total Downloads Latest Stable Version Latest Stable Version

English | العربية | বাংলা | Bosanski | Deutsch | Español | Français | हिन्दी | Italiano | 日本語 | 한국어 | मराठी | Português | Русский | Kiswahili | தமிழ் | తెలుగు | Türkçe | اردو | Tiếng Việt | 中文

はじめに

Faker Storage は、大量のフェイクデータファイルを効率的に生成及び保存するために設計された高性能のPHPパッケージです。並行処理を念頭に置いて構築されており、SwooleまたはPCNTLを活用して、数千のファイルを並行して生成することができます。このため、負荷テスト、開発環境、ストレージシステムのベンチマークに最適です。

このパッケージは、画像(PNG、JPG、GIF、BMP、WEBP、AVIF)、テキストファイル、CSVファイル、バイナリデータ、およびRFC822準拠のメールを含むさまざまなファイルタイプを生成するための流暢なAPIを提供します。各ジェネレーターは、シードを与えると決定論的な出力を生成し、環境間で再現性のあるテストデータを確保します。

主な機能

use DefectiveCode\Faker\Faker;
use League\Flysystem\Filesystem;
use DefectiveCode\Faker\Generators\Png;
use League\Flysystem\Local\LocalFilesystemAdapter;
 
// 1000枚のPNG画像を10の並行ワーカーで生成
Faker::make(Png::class)
->width(800, 1920)
->height(600, 1080)
->toDisk(new Filesystem(new LocalFilesystemAdapter('/path/to/storage')))
->basePath('images')
->count(1000)
->concurrency(10)
->seed(42)
->generate();
 
// CSVファイルを生成
use DefectiveCode\Faker\Generators\Csv;
 
Faker::make(Csv::class)
->columns(5, 10)
->rows(100, 500)
->delimiter(',')
->toDisk(new Filesystem(new LocalFilesystemAdapter('/path/to/storage')))
->count(50)
->generate();
 
// 添付ファイル付きのメールを生成
use DefectiveCode\Faker\Generators\Email;
 
Faker::make(Email::class)
->paragraphs(3, 5)
->sentences(2, 4)
->withAttachment(Png::class, 1, 3)
->toDisk(new Filesystem(new LocalFilesystemAdapter('/path/to/storage')))
->count(100)
->generate();

インストール

Composerを使用してパッケージをインストールします:

composer require defectivecode/faker-storage

要件

オプションの依存関係

最適なパフォーマンスを得るために、Swoole拡張をインストールします:

pecl install swoole

パッケージは、利用可能な場合は自動的にSwooleを使用し、そうでない場合はPCNTLにフォールバックします。

使用法

基本的なワークフロー

すべてのジェネレーターは同じパターンに従います:

  1. ジェネレーターを使ってFakerのインスタンスを作成
  2. ジェネレーターを構成(オプション)
  3. 保存先を設定
  4. 並行性とカウントを設定
  5. ファイルを生成
use DefectiveCode\Faker\Faker;
use League\Flysystem\Filesystem;
use DefectiveCode\Faker\Generators\Text;
use League\Flysystem\Local\LocalFilesystemAdapter;
 
Faker::make(Text::class)
->paragraphs(5, 10) // ジェネレーター特有の構成
->toDisk(new Filesystem(new LocalFilesystemAdapter('/storage')))
->basePath('documents') // ファイルは /storage/documents/ に保存されます
->count(100) // 100ファイルを生成
->concurrency(4) // 4つのワーカーを使用
->seed(123) // 決定論的出力のため
->generate();

ストレージ設定

Flysystemを使用

Faker Storageは、League Flysystemを使用してストレージの抽象化を行います:

use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
 
// ローカルストレージ
$filesystem = new Filesystem(new LocalFilesystemAdapter('/path/to/storage'));
 
Faker::make(Png::class)
->toDisk($filesystem)
->generate();

AWS S3ストレージ

use Aws\S3\S3Client;
use League\Flysystem\Filesystem;
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
 
$client = new S3Client([
'credentials' => [
'key' => 'your-key',
'secret' => 'your-secret',
],
'region' => 'us-east-1',
'version' => 'latest',
]);
 
$adapter = new AwsS3V3Adapter($client, 'your-bucket-name');
$filesystem = new Filesystem($adapter);
 
Faker::make(Png::class)
->toDisk($filesystem)
->basePath('uploads/images')
->generate();

ディスクオプション

ファイルシステムアダプタに追加オプションを渡します:

Faker::make(Png::class)
->toDisk($filesystem)
->diskOptions([
'visibility' => 'public',
'ACL' => 'public-read',
'CacheControl' => 'max-age=31536000',
])
->generate();

並行処理設定

concurrency() メソッドで並行実行を制御します:

// 10スレッド/プロセスを使用
Faker::make(Png::class)
->concurrency(10)
->generate();
 
// Swoole特有: スレッドごとにスレッドとコルーチンの両方を設定
Faker::make(Png::class)
->concurrency(threads: 4, coroutines: 8) // 4つのワーカー、各8つのコルーチン
->generate();

ファイル名

デフォルトの命名

デフォルトでは、ファイルはUUID v4を使用して命名されます:

// 生成結果: e7f0a8d3-5c2b-4f9e-8a1d-3b4c5d6e7f8a.png
Faker::make(Png::class)->generate();

ビルトインの名前生成

use DefectiveCode\Faker\NameGenerator;
 
// UUIDベース(デフォルト)
NameGenerator::setDefault('uuid'); // 生成結果: e7f0a8d3-5c2b-4f9e-8a1d-3b4c5d6e7f8a.png
 
// 連続番号付け
NameGenerator::setDefault('sequence'); // 生成結果: 1.png, 2.png, 3.png, ...

カスタム名付け

ファイル名をカスタマイズするためにクロージャを提供します:

use DefectiveCode\Faker\NameGenerator;
 
// カスタムクロージャ
Faker::make(Png::class)
->nameGenerator(function (int $seed, int $completedFiles, $generator) {
return "custom-{$completedFiles}-{$seed}.png";
})
->generate();
 
// 日付ベースの命名
Faker::make(Png::class)
->nameGenerator(function (int $seed, int $completedFiles, $generator) {
return date('Y/m/d') . "/image-{$completedFiles}.png";
})
->generate();

再現性のためのシーディング

同じファイルを生成するためにシードを設定します:

Faker::make(Png::class)
->seed(42)
->count(10)
->generate();

各ファイルには、ベースシードとファイルインデックスに基づいた一意の決定論的シードが付与されます。

ジェネレーター

画像ジェネレーター

すべての画像ジェネレーターは、寸法と品質の設定をサポートします。

PNG

use DefectiveCode\Faker\Generators\Png;
 
Faker::make(Png::class)
->width(800, 1920) // 800-1920pxの間のランダムな幅
->height(600, 1080) // 600-1080pxの間のランダムな高さ
->withAlpha(true) // アルファ/透明チャンネルを有効化
->grid(5) // オプション: 5x5の対称パターンを生成
->toDisk($filesystem)
->generate();

JPG

use DefectiveCode\Faker\Generators\Jpg;
 
Faker::make(Jpg::class)
->width(800, 1920) // 800-1920pxの間のランダムな幅
->height(600, 1080) // 600-1080pxの間のランダムな高さ
->grid(5) // オプション: 5x5の対称パターンを生成
->toDisk($filesystem)
->generate();

GIF

use DefectiveCode\Faker\Generators\Gif;
 
Faker::make(Gif::class)
->width(800, 1920) // 800-1920pxの間のランダムな幅
->height(600, 1080) // 600-1080pxの間のランダムな高さ
->withAlpha(true) // アルファ/透明チャンネルを有効化
->grid(5) // オプション: 5x5の対称パターンを生成
->toDisk($filesystem)
->generate();

BMP

use DefectiveCode\Faker\Generators\Bmp;
 
Faker::make(Bmp::class)
->width(800, 1920) // 800-1920pxの間のランダムな幅
->height(600, 1080) // 600-1080pxの間のランダムな高さ
->grid(5) // オプション: 5x5の対称パターンを生成
->toDisk($filesystem)
->generate();

WEBP

use DefectiveCode\Faker\Generators\Webp;
 
Faker::make(Webp::class)
->width(800, 1920) // 800-1920pxの間のランダムな幅
->height(600, 1080) // 600-1080pxの間のランダムな高さ
->withAlpha(true) // アルファ/透明チャンネルを有効化
->grid(5) // オプション: 5x5の対称パターンを生成
->toDisk($filesystem)
->generate();

AVIF

use DefectiveCode\Faker\Generators\Avif;
 
Faker::make(Avif::class)
->width(800, 1920) // 800-1920pxの間のランダムな幅
->height(600, 1080) // 600-1080pxの間のランダムな高さ
->withAlpha(true) // アルファ/透明チャンネルを有効化
->grid(5) // オプション: 5x5の対称パターンを生成
->toDisk($filesystem)
->generate();

ランダム画像

ランダムな画像フォーマットを生成します:

use DefectiveCode\Faker\Generators\RandomImage;
 
Faker::make(RandomImage::class)
->width(800, 1920)
->height(600, 1080)
->withAlpha(false) // ランダムに: AVIF, BMP, GIF, JPEG, PNG, WEBPから
->toDisk($filesystem)
->generate();
 
Faker::make(RandomImage::class)
->width(800, 1920)
->height(600, 1080)
->withAlpha(true) // ランダムに: AVIF, GIF, PNG, WEBPから
->toDisk($filesystem)
->generate();

テキストジェネレーター

段落を含むプレーンテキストファイルを生成します:

use DefectiveCode\Faker\Generators\Text;
 
Faker::make(Text::class)
->paragraphs(5, 10) // 1ファイルあたり5-10段落
->sentences(3, 6) // 1段落あたり3-6文
->toDisk($filesystem)
->generate();

出力例:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
 
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

CSVジェネレーター

ランダムデータを含むCSVファイルを生成します:

use DefectiveCode\Faker\Generators\Csv;
 
Faker::make(Csv::class)
->columns(5, 10) // 5-10列
->rows(100, 500) // 100-500行
->delimiter(',') // 列の区切り
->enclosure('"') // フィールドの囲み
->escape('\\') // エスケープ文字
->eol("\n") // 行末
->toDisk($filesystem)
->generate();

出力例:

"John Doe","john@example.com","555-1234","New York","Engineer"
"Jane Smith","jane@example.com","555-5678","Los Angeles","Designer"
"Bob Johnson","bob@example.com","555-9012","Chicago","Manager"

バイナリジェネレーター

ランダムなバイナリデータを生成します:

use DefectiveCode\Faker\Generators\Binary;
 
Faker::make(Binary::class)
->length(1024, 1048576) // 1KB - 1MB
->toDisk($filesystem)
->generate();

メールジェネレーター

RFC822準拠のメールファイルを生成します:

use DefectiveCode\Faker\Generators\Email;
 
Faker::make(Email::class)
->paragraphs(3, 5) // メール本文の段落
->sentences(2, 4) // 段落ごとの文
->withAttachment(Png::class, 1, 3) // 1-3枚のPNG添付ファイルを追加
->toDisk($filesystem)
->generate();

メールヘッダー

生成されたメールには以下が含まれます:

添付ファイル付きのメール

ジェネレータークラス名またはインスタンスを使用してファイルを添付します:

use DefectiveCode\Faker\Generators\Email;
use DefectiveCode\Faker\Generators\Png;
use DefectiveCode\Faker\Generators\Pdf;
 
Faker::make(Email::class)
->withAttachment(Png::class, 1, 3) // 1-3枚のPNG添付ファイルを追加
->generate();
 
// 設定したジェネレーターインスタンスを使用して添付
$pngGenerator = new Png(Png::getDefaultConfig());
$pngGenerator->width(400, 800)->height(300, 600);
 
Faker::make(Email::class)
->withAttachment($pngGenerator, 2, 5)
->generate();

出力例:

To: John Doe <john.doe@example.com>
From: Jane Smith <jane.smith@example.com>
Subject: Important meeting tomorrow
Date: Fri, 03 Jan 2026 10:30:00 +0000
Message-ID: <3e92e5c2b0d632b3a36fbbb17484b7fe@example.com>
Content-Type: multipart/mixed; boundary="----=_Part_123"
 
------=_Part_123
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
 
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
 
------=_Part_123
Content-Type: image/png; name="attachment.png"
Content-Disposition: attachment; filename="attachment.png"
Content-Transfer-Encoding: base64
 
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==
------=_Part_123--

高度な使用法

カスタムジェネレーター

Generatorインターフェースを実装することで独自のジェネレーターを作成できます:

use DefectiveCode\Faker\Configs\Config;
use DefectiveCode\Faker\Concerns\SetsSeed;
use DefectiveCode\Faker\Generators\Generator;
use DefectiveCode\Faker\Concerns\PreparesFaker;
 
class MyCustomGenerator implements Generator
{
use PreparesFaker;
use SetsSeed;
 
public function __construct(public Config $config) {}
 
public static function getDefaultConfig(): Config
{
return new MyCustomConfig([
'contentType' => 'application/x-custom',
'nameGenerator' => NameGenerator::default('extension'),
]);
}
 
public function generate(): mixed
{
// ここに生成ロジックを記述
$data = $this->faker->randomElement(['foo', 'bar', 'baz']);
 
$stream = fopen('php://temp', 'w+');
fwrite($stream, $data);
 
return $stream;
}
}
 
// カスタムジェネレーターを使用
Faker::make(MyCustomGenerator::class)
->toDisk($filesystem)
->generate();

設定クラス

各ジェネレーターは、Configを拡張した設定クラスを使用します:

use DefectiveCode\Faker\Configs\Config;
 
class MyCustomConfig extends Config
{
public int $minValue = 1;
public int $maxValue = 100;
}

パフォーマンステips

  1. Swooleの使用: Swoole拡張をインストールして、最高のパフォーマンスを得る
  2. 並行性の調整: スレッド数をCPUコア数に合わせて最適なスループットを実現
  3. バッチ処理: 複数の小さな実行を行うのではなく、大きなバッチを生成
  4. ストレージ場所: アップロード前の一時ファイル用に高速ストレージ(SSD、ローカルディスク)を使用
  5. ネットワークI/O: S3を使用する際は、帯域幅利用を最大化するために並行性を増やす

設定

グローバル設定メソッド

これらのメソッドは、すべての Faker インスタンスで利用可能です:

make(string $generator): Faker

指定されたジェネレーターで新しいFakerインスタンスを作成します:

Faker::make(Png::class)

toDisk(Filesystem $filesystem): Faker

保存先を設定します(必須):

Faker::make(Png::class)
->toDisk(new Filesystem(new LocalFilesystemAdapter('/storage')))

basePath(string $basePath): Faker

ファイルシステム内のベースパスを設定します:

Faker::make(Png::class)
->basePath('images/uploads') // ファイルは /storage/images/uploads/ に保存されます

count(int $count): Faker

生成するファイルの数を設定します:

Faker::make(Png::class)
->count(1000)

concurrency(int $threads, ?int $coroutines = null): Faker

並行処理を設定します:

// 基本的な並行性
Faker::make(Png::class)
->concurrency(4)
 
// Swoole特有: スレッドとコルーチンの設定
Faker::make(Png::class)
->concurrency(threads: 4, coroutines: 8)

seed(int $seed): Faker

決定論的生成のためのシードを設定します:

Faker::make(Png::class)
->seed(42)

nameGenerator(Closure $generator): Faker

ファイル名をカスタマイズします:

Faker::make(Png::class)
->nameGenerator(function (int $seed, int $completedFiles, $generator) {
return "file-{$completedFiles}.png";
})

diskOptions(array $diskOptions): Faker

ファイルシステムアダプタにオプションを渡します:

Faker::make(Png::class)
->diskOptions([
'visibility' => 'public',
'ACL' => 'public-read',
])

generate(): void

ファイル生成を実行します:

Faker::make(Png::class)->generate();

サポートガイドライン

私たちのオープンソースパッケージを選んでいただきありがとうございます!これらのサポートガイドラインを確認するための時間を取ってください。プロジェクトを最大限に活用する助けになるでしょう。

コミュニティ主導のサポート

私たちのオープンソースプロジェクトは、素晴らしいコミュニティによって支えられています。質問がある場合や支援が必要な場合は、StackOverflowやその他のオンラインリソースが最良の選択です。

バグと機能の優先順位付け

オープンソースプロジェクトを管理する現実として、報告されたすべてのバグや機能リクエストに即座に対応することはできません。以下の順序で問題を優先します。

1. 有料製品に影響を与えるバグ

有料製品に影響を与えるバグは常に最優先事項となります。場合によっては、私たちに直接影響を与えるバグのみを扱うことがあります。

2. コミュニティのプルリクエスト

バグを特定し、解決策を持っている場合は、プルリクエストを提出してください。私たちの製品に影響を与える問題の次に、これらのコミュニティ主導の修正に対して最も高い優先順位を与えます。レビューと承認が行われた後、あなたの解決策をマージし、貢献を認めます。

3. 財政的サポート

上記のカテゴリー以外の問題については、その解決に資金を提供することができます。各オープンイシューには、財政的に貢献できる注文フォームへのリンクがあります。提供された資金額に基づいて、これらの問題を優先します。

コミュニティの貢献

オープンソースは、そのコミュニティが活発であるときに栄えます。バグを修正していない場合でも、コード改善、ドキュメントの更新、チュートリアルの作成、またはコミュニティチャネルで他の人を支援することで貢献を考えてください。私たちは、コミュニティとしてオープンソースの仕事を支援することを皆さんに強く奨励します。

繰り返しになりますが、DefectiveCodeは、有料製品にどのように影響を与えるか、コミュニティのプルリクエスト、そして問題に対して受け取った財政的サポートに基づいてバグを優先します.

ライセンス - MITライセンス

著作権 © Defective Code, LLC. 全著作権を保有します。

本ソフトウェア及び関連する文書ファイル(以下「ソフトウェア」)のコピーを取得したすべての人に対して、無償で本ソフトウェアを制限なく利用する権利、使用、複製、修正、統合、出版、配布、サブライセンス、及び/またはソフトウェアのコピーを販売する権利を許可し、ソフトウェアが提供される人々にもそれを行うことを許可します。ただし、以下の条件に従います:

上記の著作権表示とこの許可通知は、ソフトウェアのすべてのコピーまたは重要な部分に含まれるものとします。

ソフトウェアは「現状のまま」提供され、いかなる種類の保証もなく、明示または暗示を問わず、商業性、特定の目的への適合性および非侵害の保証を含むが、これに限定されない保証はありません。著作者または著作権者は、契約、不法行為、またはその他の方法によるいかなる請求、損害、またはその他の責任についても、ソフトウェアまたはソフトウェアの使用またはその他の取り扱いに起因する場合において、一切の責任を負わないものとします。