azjezz/psl
PSL is a modern, well-typed standard library for PHP 8.4+, inspired by HHVM’s HSL. It offers safer, predictable APIs for async, collections, networking, I/O, crypto, terminal UI, and robust data validation—replacing brittle built-ins with consistent alternatives.
PSL (PHP Standard Library) introduces a modern, composable, and type-safe alternative to PHP's native primitives, aligning well with Laravel's evolving architecture. Key fits:
Swoole, ReactPHP).Type\shape() for request DTOs).Vec\map() for Eloquent collections).Misalignment:
Illuminate\Queue) may conflict with PSL’s structured concurrency model (e.g., Async\main). Requires careful abstraction.PDO, cURL) lack PSL wrappers, necessitating hybrid code paths.| Component | Feasibility | Notes |
|---|---|---|
| Type Validation | High | Replace Request::validate() with Type\shape() for DTOs. |
| Collections | High | Replace Collection facade with Vec, Dict, or Map for purity. |
| Async | Medium | Requires fiber-compatible Laravel extensions (e.g., laravel-fiber). |
| Networking | Low | PSL’s TCP, HTTP are low-level; prefer Laravel’s HttpClient. |
| Graph/Tree | Low | Niche use cases (e.g., dependency resolution); not core Laravel needs. |
Critical Path:
Type and Vec in domain layers.Bus or Jobs for compatibility.| Risk | Impact | Mitigation |
|---|---|---|
| Fiber compatibility | High | Test with laravel-fiber; avoid mixing fibers with Laravel’s event loop. |
| Performance overhead | Medium | Benchmark Vec vs. native arrays; PSL’s immutability may add memory usage. |
| Learning curve | Medium | Requires training on functional patterns (e.g., map, filter). |
| Dependency bloat | Low | PSL is ~10MB; minimal compared to Laravel’s ecosystem. |
| Tooling gaps | Low | Missing Laravel-specific IDE support (e.g., psalm-plugin needs configuration). |
Blockers:
psl-eloquent).Swoole/ReactPHP in Laravel.Async Strategy:
Async or Laravel’s Bus/Jobs for async workflows?Illuminate\Events)?Type Safety Trade-offs:
Type system globally, or use it only for DTOs?Performance:
Vec vs. native arrays in bulk operations?Tooling:
Psalm/PHPStan to treat PSL types as first-class citizens?psl-eloquent)?Migration Path:
ServiceProvider) to use PSL, or adopt gradually?Cache, Filesystem, etc.?PSL aligns with Laravel’s modern PHP stack but requires selective adoption:
Request validation, FormRequest, and DTOs with Type\shape().Vec/Dict in domain layers (e.g., repository patterns).Async\concurrently for batch processing).TCP/HTTP; use Laravel’s HttpClient/Queue.Stack Conflicts:
Swoole/ReactPHP integrations.PDO, Redis, etc.; require adapters.| Phase | Components | Effort | Risks |
|---|---|---|---|
| Pilot | Type + Vec in DTOs |
Low | Minimal; isolated to new features. |
| Domain Layer | Replace Collection facade |
Medium | Breaking changes to service logic. |
| Async | Wrap Async in Job classes |
High | Fiber compatibility issues. |
| Core | Rewrite ServiceProvider |
High | Risk of regressions; requires testing. |
Recommended Order:
Request validation with Type\shape().Vec/Dict in repositories and services.Psalm/PHPStan for PSL types.| Laravel Component | PSL Compatibility | Workaround |
|---|---|---|
Illuminate\Support\Collection |
Medium (functional overlap) | Use Vec::fromArray() for conversion. |
Illuminate\Http\Request |
High (type validation) | Replace validate() with Type\shape()->coerce(). |
Illuminate\Queue |
Low (async model conflict) | Wrap PSL async in Job classes. |
Illuminate\Database\Eloquent |
Low (no ORM integration) | Use PSL for query results (e.g., Vec from get()). |
Illuminate/Events |
Low (fiber vs. event loop) | Avoid mixing; use PSL async only in jobs. |
PslCollection::fromLaravel()).Collection facade with Vec in repositories.Request validation with Type in controllers.| Aspect | Impact | Notes |
|---|---|---|
| Codebase Complexity | Increased | Functional patterns (e.g., map, filter) require discipline. |
| Debugging | Medium | Immutable data structures reduce side effects but may complicate state tracking. |
| Testing | Improved | Pure functions and immutability simplify unit tests. |
| Dependency Updates | Low | PSL is actively maintained; Laravel compatibility is a concern. |
Key Tasks:
Type\shape() for all DTOs.| Area | Impact | Mitigation |
|---|---|---|
| Developer Onboarding | High | Requires training on functional programming and fibers. |
| Legacy Code | Medium | Hybrid adapters (e.g., PslCollection) ease transition |
How can I help you explore Laravel packages today?