Installation
Add to composer.json:
{
"require": {
"anglemx/php-utilities": "^1.0"
}
}
Run composer update.
First Use Case Quickly validate a Git repository structure in a Laravel command:
use Angle\Utilities\Git;
public function handle()
{
$git = new Git();
if ($git->isGitRepo()) {
$this->info('Valid Git repo detected.');
}
}
Key Entry Points
Git class: Validate Git repos, check status, or parse .gitignore.File helper: File operations (e.g., File::ensureDirectoryExists()).String helper: String manipulation (e.g., String::slugify()).Array helper: Array utilities (e.g., Array::deepMerge()).Git Operations in Laravel
Git::isGitRepo() in boot() to enforce Git in deployments.Git::getStatus() into a command:deploy hook:
$status = Git::getStatus();
if ($status->hasChanges()) {
$this->error('Uncommitted changes detected!');
return 1;
}
File System Utilities
Storage::makeDirectory() with:
File::ensureDirectoryExists(storage_path('app/cache'));
File::exists() in service providers:
if (File::exists(config_path('custom.php'))) {
$this->mergeConfigFrom(config_path('custom.php'), 'custom');
}
String/Array Helpers
Str::slug() with:
$slug = String::slugify('My Awesome Post');
$merged = Array::deepMerge($defaultConfig, $userConfig);
Integration with Laravel Services
use Angle\Utilities\Validation\Rules\GitRepoExists;
'repo_path' => ['required', new GitRepoExists],
public function handle($request, Closure $next)
{
if (!Git::isGitRepo() || Git::getStatus()->hasChanges()) {
abort(403, 'Git repo required and clean.');
}
return $next($request);
}
Git Path Assumptions
Git class assumes the current working directory is the repo root. Explicitly pass paths:
$git = new Git(__DIR__); // For project-specific repos
Git::setPath() or pass the path to methods.File Permissions
File::ensureDirectoryExists() may fail silently on restricted systems. Add error handling:
try {
File::ensureDirectoryExists($path);
} catch (Exception $e) {
Log::error("Failed to create directory: {$e->getMessage()}");
}
String Slugification
String::slugify() uses a basic regex. For multilingual support, combine with Laravel’s Str::ascii():
$slug = String::slugify(Str::ascii($title));
Array Deep Merge
Array::deepMerge() overwrites arrays by default. Use Array::deepMergeRecursive() for non-destructive merges:
$merged = Array::deepMergeRecursive($a, $b, true); // Preserve arrays
Git::getStatus()->getOutput() to inspect raw CLI output.storage/logs/laravel.log to catch permission errors.null inputs) with:
$result = String::slugify(null); // Returns empty string
Custom Git Commands
Extend the Git class to add commands (e.g., Git::run('branch -a')).
Validation Rules
Create reusable rules by extending GitRepoExists:
class ValidGitignore extends GitRepoExists
{
public function validateAttribute($attribute, $value, $fail)
{
if (!File::exists($value . '/.gitignore')) {
$fail('Gitignore file missing.');
}
}
}
File System Events
Hook into Laravel’s filesystem events to log file changes using File helpers.
String Templates Combine with Laravel’s Blade for dynamic content:
{{ String::template('Hello, {name}!', ['name' => $user->name]) }}
How can I help you explore Laravel packages today?