nette/utils
Handy PHP utility library from Nette: strings, arrays, filesystem, safe JSON, and more. Includes proven helpers like Strings, Arrays, FileSystem, and Validators to simplify everyday tasks with clean APIs, good performance, and broad compatibility.
Pros:
Strings, Arrays, Process, Image) align well with Laravel’s service-layer architecture. Each utility can be injected as a service provider or facade, reducing boilerplate in controllers/services.Process::runExecutable() (shell-injection-safe) and FileSystem::isValidFilename() mitigate common Laravel vulnerabilities (e.g., file uploads, CLI commands).Iterables::memoize(), Arrays::filter()) can replace manual loops in Eloquent queries or collection transformations, improving TTFB.Type and Validators classes can replace or extend Laravel’s built-in validation (e.g., custom rules for APIs, form requests).Cons:
Html, Json) overlap with Laravel’s native helpers (Str::, Json::). Risk of duplication if not scoped carefully.Strings::webalize() use Nette’s conventions (e.g., PascalCase constants), which may require adaptation for Laravel’s snake_case style.Nette\Utils\Strings) in AppServiceProvider::boot().Utils::slugify()) to mimic Laravel’s Str:: pattern.@slug($text)) using Blade::directive().Validators into Laravel’s pipeline via Validator::extend() or custom rules.Arrays::mapWithKeys() to transform query results (e.g., pivot tables).Iterables::memoize() to cache repetitive subqueries.Artisan::call() with Process::runExecutable() for safer subprocess calls (e.g., Process::runExecutable('php', ['artisan', 'queue:work'])).| Risk Area | Mitigation Strategy |
|---|---|
| PHP Version Mismatch | Enforce PHP 8.2+ in phpunit.xml and composer.json to align with Laravel 10+. |
| Extension Dependencies | Document required extensions (GD, Intl) in README.md and provide fallbacks. |
| API Style Conflicts | Wrap Nette utilities in Laravel-compatible facades (e.g., Utils::slug() instead of Strings::webalize()). |
| Performance Overhead | Benchmark critical paths (e.g., Arrays::filter() vs. Laravel Collections). |
| Testing Gaps | Add PHPUnit tests for Laravel-specific edge cases (e.g., Blade integration). |
Process for CLI workflows, Image for media-heavy apps).Str::) or complement them?Process::runCommand() usage to prevent accidental shell injection?AppServiceProvider:
$this->app->singleton('nette.utils.strings', fn() => new \Nette\Utils\Strings());
Utils facade to expose methods like Utils::slug().Validator::extend('nette_filename', function ($attr, $value, $params) {
return \Nette\Utils\FileSystem::isValidFilename($value);
});
Arrays::mapWithKeys() to transform attributes:
public function getSlugAttribute($value) {
return \Nette\Utils\Strings::webalize($this->attributes['title']);
}
Iterables::memoize().Blade::directive('slug', function ($text) {
return "<?php echo \\Nette\\Utils\\Strings::webalize({$text}); ?>";
});
Artisan::call() with Process::runExecutable() for safer subprocesses:
$process = new \Nette\Utils\Process();
$process->runExecutable('php', ['artisan', 'queue:work', '--once']);
Validators for custom rules:
'filename' => ['nette_filename', 'required'],
Phase 1: Proof of Concept (2 weeks)
Strings, Process, Validators) into a single module.Str::slug() vs. Strings::webalize()).Strings::trim()).Phase 2: Core Integration (4 weeks)
laravel-nette-utils) with Laravel-specific facades.Phase 3: Full Adoption (Ongoing)
slug() functions) with Nette utilities.Str::) via deprecation warnings.| Laravel Component | Integration Strategy |
|---|---|
| Eloquent | Use Arrays::mapWithKeys() for pivot transformations; Iterables::memoize() for caching. |
| Validation | Extend Validator with nette/utils validators (e.g., isTypeDeclaration). |
| Blade | Register directives for templating (e.g., @slug(), @isValidFilename()). |
| Artisan | Replace Artisan::call() with Process::runExecutable() for subprocesses. |
| HTTP Requests | Use Json::decodeFile() for API response parsing. |
| File Uploads | Leverage FileSystem::isValidFilename() and Image::fromFile() for sanitization. |
Critical Path:
Low-Risk Add-ons:
Deprecation Plan:
slug() functions in favor of Strings::webalize().Str:: methods with Nette alternatives (e.g., Str::slug() → Utils::slug()).How can I help you explore Laravel packages today?