directorytree/anonymize
Anonymize swaps sensitive Eloquent model attributes with realistic Faker data. Deterministic per model ID, cached for performance, and easy to toggle globally or per instance—ideal for dev, demos, and safely sharing production-like datasets.
Anonymizable interface and Anonymized trait follow Laravel’s conventions, making adoption straightforward for existing models.AnonymizedResource, aligning with Laravel’s API-first architecture. This is critical for modern Laravel applications relying on API-driven workflows.AnonymizeEnabled, AnonymizeDisabled) for extensibility, allowing TPMs to hook into lifecycle changes (e.g., logging, analytics, or conditional logic).getAnonymizableSeed()) to ensure reproducible fake data, which is essential for debugging, testing, and demo environments.fakerphp/faker for realistic data generation, which is a widely adopted standard in PHP ecosystems. No vendor lock-in.JsonResource anonymization (fixed in v1.1.1) may require testing for complex API responses (e.g., deeply nested relationships).getAnonymizableSeed() could lead to collisions or inconsistent data if not carefully designed.Use Case Scope:
Data Sensitivity:
User->Orders) be handled? Will child models also be anonymized recursively?Performance:
Faker instances?Testing:
Faker, or will deterministic seeding suffice?Anonymize::enable() in test suites)?Deployment:
Faker configurations (e.g., locales, custom providers). TPMs can extend Faker globally (e.g., via Faker::extend()) for domain-specific fake data.ApiResource and JsonResource.HttpTests, FeatureTests) for seamless test data generation.User, Product) to validate integration and performance.if (app()->environment('demo'))) to avoid disrupting production./api/demo) via middleware:
public function handle(Request $request, Closure $next) {
if ($request->path() === 'api/demo') {
Anonymize::enable();
}
return $next($request);
}
feature() helper or a package like spatie/laravel-feature-flags to toggle anonymization dynamically:
if (feature('anonymize_api')) {
Anonymize::enable();
}
Faker providers (e.g., Faker\Provider\en_US\Address) must be installed and configured globally.Anonymizable interface and getAnonymizedAttributes() for core models.JsonResource with AnonymizedResource for API responses.UserResource with OrderResource).AnonymizeEnabled/AnonymizeDisabled (e.g., logging, analytics).fakerphp/faker and Laravel version compatibility. The package’s composer.json shows loose version constraints (^11.0|^12.0|^13.0), which may lead to breaking changes.composer.json to avoid surprises:
"require": {
"fakerphp/faker": "~1.20",
"illuminate/support": "13.0.*"
}
getAnonymizableSeed() or conditional anonymization may require updates if business rules change.README or wiki for future maintainers.if (app()->environment('local') && Anonymize::isEnabled()) {
$this->logOriginalData($model->getOriginal());
}
Anonymize::disable() in non-dev environments).public function handle($request, Closure $next) {
if (!app()->isLocal() && Anonymize::isEnabled()) {
throw new \RuntimeException("Anonymization cannot be disabled in non-local environments.");
}
return $next($request);
}
Faker instance per request is expensive. Mitigate by:
Faker instance globally (e.g., in a singleton or container binding).getAnonymizableSeed() is lightweight (e.gHow can I help you explore Laravel packages today?