nette/utils
Handy PHP utility classes from the Nette Framework: strings, arrays, JSON, dates/times, file system helpers, safe reflection, and more. Lightweight, well-tested, and framework-agnostic—useful building blocks for everyday PHP and Laravel projects.
Strings, Arrays, Image, Process) align well with Laravel’s service-layer architecture, where domain-specific logic is encapsulated in services. For example:
Str/Arr with typed, documented alternatives (e.g., Strings::webalize() for SEO-friendly URLs).Image utilities into Laravel’s filesystem disk drivers or media library packages (e.g., Spatie Media Library) for consistent image handling.Process facade (if used) or custom shell exec calls with Process::runExecutable() for secure subprocess handling (e.g., in queued jobs or console commands).Type/Validators utilities (e.g., Validators::isPhpIdentifier() for custom rule logic).Filesystem with FileSystem::isValidFilename() for upload sanitization (e.g., in Illuminate\Http\Request middleware).Iterables::memoize() or Arrays::filter() in Laravel’s testing utilities (e.g., Illuminate/Testing assertions).composer require nette/utils integration with zero Laravel-specific conflicts.Nette\Utils\* namespace avoids collisions with Laravel’s Illuminate\* or Laravel\* namespaces.| Laravel Component | Nette Utils Utility | Use Case |
|---|---|---|
Str::slug() |
Strings::webalize() |
SEO-friendly URLs with INTL support. |
File::exists() |
FileSystem::isValidFilename() |
Sanitize user uploads in UploadedFile. |
Process::run() |
Process::runExecutable() |
Secure CLI tool execution in jobs. |
Image facade |
Image::fromFile() (with $warnings) |
Log GD errors in media processing. |
Validator rules |
Type::fromValue()/Validators |
Custom validation logic (e.g., PHP identifiers). |
Reflection methods marked deprecated).DateTime strict behavior or Strings::webalize() requiring INTL may need polyfills for environments without extensions.nette/utils may pull in nette/di (though utils is standalone in v4+).Image utilities require GD; Strings::webalize() needs INTL. Document these as runtime dependencies.Reflection or Type classes (e.g., in validation) could impact boot time. Benchmark critical paths.Process instances in Laravel’s service container for reuse.Process for jobs or Image for media more critical?)Str::slug()) or extend them as wrappers?webalize()) in production?Process or Image utilities in PHPUnit tests? (Nette provides mockable interfaces.)AppServiceProvider:
public function register(): void {
$this->app->singleton('nette.utils.process', fn() => new \Nette\Utils\Process());
$this->app->alias('nette.utils.process', \Nette\Utils\Process::class);
}
Facade\Utils::slug()) to maintain consistency with Laravel’s patterns.MediaLibrary to use Image::fromFile() with warning handling.NetteValidator class extending Laravel’s Validator to use Type/Validators utilities.Process::run() with Process::runExecutable() in queued jobs or Artisan commands.| Phase | Action | Risks | Mitigation |
|---|---|---|---|
| Assessment | Audit Laravel codebase for ad-hoc string/array/image/process logic. | Missed opportunities for refactoring. | Use static analysis (PHPStan) to find patterns. |
| Pilot | Replace 1–2 high-impact components (e.g., Str::slug() → Strings::webalize()). |
BC breaks in pilot features. | Feature flags for gradual rollout. |
| Core | Integrate Process and Image utilities into Laravel’s filesystem/media layers. |
Performance regression in I/O-heavy ops. | Benchmark before/after; optimize caching. |
| Validation | Extend Laravel’s validator with Type/Validators utilities. |
Complexity in custom rule logic. | Document use cases (e.g., "Validate PHP identifiers"). |
| Testing | Add test cases for Nette utilities in Laravel’s test suite. | Flaky tests with subprocesses. | Use Process::runCommand() with timeouts. |
Illuminate\Http\UploadedFile to use FileSystem::isValidFilename():
public function getClientOriginalName(): string {
$name = parent::getClientOriginalName();
if (!FileSystem::isValidFilename($name)) {
throw new \InvalidArgumentException("Invalid filename: {$name}");
}
return $name;
}
Str facade extension:
Str::shouldUseNetteUtils(true);
Str::slug('Nette Utils'); // Uses Strings::webalize()
webalize() implementation).if (PHP_VERSION_ID >= 80200) guards for v4.1.0+ features.Str::slug() → Strings::webalize().FileSystem::isValidFilename() to upload middleware.Process utilities into Laravel’s Process facade.Image handling in media libraries.Type/Validators to Laravel’s validator.How can I help you explore Laravel packages today?