riimu/kit-phpencoder
Export PHP variables as customizable, readable or compact PHP code. A flexible alternative to var_export() with control over whitespace, array syntax, and useful object conversion—ideal for generated config files and optimized cache output.
Use Case Alignment:
export type object conversion (index order stability) directly addresses Laravel-specific pain points:
HasMany, MorphTo, or custom accessors.JsonResponse payloads where array keys must match expected schemas (e.g., OpenAPI/Swagger).DatabaseSeeder or Factory outputs).Illuminate\Support\Collection serialization, critical for bulk operations (e.g., Collection::toArray()).array<int, string> and never return types in Eloquent and API resources.Anti-Patterns:
spatie/fractal for JSON APIs.PHP 8.2-Specific Features:
PhpEncoder (e.g., encode($data, ['preserveIndexOrder' => true])).// Generated via PhpEncoder
enum UserRole {
case ADMIN;
case EDITOR;
}
Customization Hooks:
PhpEncoder to handle:
HasMany/BelongsTo to preserve relationship order.Collection::toArray() index stability.PhpEncoder::setNamespace() to avoid collisions with existing classes (e.g., App\Generated\Models).Output Control:
config/app.php) or test fixtures. Enable via:
$encoder->setOptions(['preserveIndexOrder' => true]);
declare(strict_types=1) for generated code to enforce type safety.Edge Cases:
MorphTo) to ensure no infinite loops in generated code.null in arrays or unsupported types like array<int, string>.Illuminate\Database\Eloquent\Concerns\HasAttributes (e.g., getAttribute()).Illuminate\Support\Traits\ForwardsCalls in generated proxies.Testing Overhead:
php -l or psalm).php artisan test to ensure Eloquent models serialize correctly.php artisan generate:model).Security:
file_put_contents in custom callbacks).symfony/var-exporter) for vulnerabilities.config/cache.php with predictable drivers array order.Illuminate\Support\Collection order preservation?HasManyThrough)?var_export-like code that might break?PhpEncoder where needed.Laravel Ecosystem:
OrderStatus).config/queue.php with array<string, string>).php artisan generate:seeder --php82).Third-Party Synergy:
UserRole).Alternatives:
spatie/fractal: Better for JSON APIs; this package is for PHP codegen.rector/rector: For upgrading existing code to PHP 8.2, but not for dynamic generation.var_export in PHP 8.2-specific contexts (e.g., generating enums or Eloquent models).PhpEncoder to scaffold a UserRole enum instead of hardcoding.composer.json to require PHP 8.2 and Laravel 10+.var_export in Artisan commands with PhpEncoder, enabling preserveIndexOrder.use Riimu\Kit\PhpEncoder\PhpEncoder;
class LaravelPhpEncoder extends PhpEncoder {
public function encodeLaravelModel($model, string $namespace = 'App\\Generated') {
return $this->encode($model, [
'namespace' => $namespace,
'preserveIndexOrder' => true,
'useStrictTypes' => true,
'handleCollections' => true, // Custom callback for Collections
]);
}
protected function handleCollection(array $collection): string {
return $this->encode($collection->toArray(), ['preserveIndexOrder' => true]);
}
}
composer.json:
"require": {
"php": "^8.2",
"laravel/framework": "^10.0",
"riimu/kit-phpencoder": "^2.4.2"
}
Illuminate\Database\Eloquent\Casts (e.g., HasCast for enums).Illuminate\Support\Collection serialization order.App\\Generated (not global).composer validate.| Phase | Task | Dependencies | |----------------|--------------------------------------------------------------------
How can I help you explore Laravel packages today?