dragon-code/support
Dragon Code Support is a lightweight helper toolkit for PHP/Laravel projects, providing facades, utilities, and common support classes. Designed to be extended with your own methods, with a clear structure for adding features and tests.
Installation:
composer require dragon-code/support
No additional configuration is required—facades are auto-discoverable.
First Use Case: Replace native PHP/array operations with facade methods for consistency.
use DragonCode\Support\Facades\Arr;
use DragonCode\Support\Facades\Str;
// Array operations
$flattened = Arr::of(['a' => ['b' => 1]])->flattenKeys();
$count = Arr::count(['a', 'b', 'c']); // Returns 3
// String operations
$trimmed = Str::trim(' hello ');
$matches = Str::matchAll('/\d+/', 'Order #123');
Where to Look First:
DragonCode\Support\Facades\Arr, Str, File, Digit, etc.Arr::toInstance(), Str::matchAll()).Tests/Unit/ for usage examples and edge cases.$result = Arr::of($array)
->filter(fn($v) => $v > 0)
->map(fn($v) => $v * 2)
->toArray();
$count = Arr::of($collection)->count(); // Alternative to `count()`
$instance = Arr::of(['key' => 'value'])->toInstance(stdClass::class);
$cleaned = Str::squish(' extra spaces ');
$numbers = Str::matchAll('/\d+/', $text); // Returns array of matches
$trimmed = Str::trim(' hello ', ' !'); // Custom characters
$paths = Directory::allPaths(storage_path('app'));
Directory::copy(storage_path('old'), storage_path('new'));
$content = File::load('file.txt'); // Throws exception if unreadable
$model = Instance::of(User::class)->new(['name' => 'John']);
$isModel = Instance::of($variable)->is(User::class);
Facades\Support::alias('Arr', 'DragonCode\Support\Facades\Arr');
Blade::directive('flatten', function ($expr) {
return "<?php echo Arr::of({$expr})->flattenKeys(); ?>";
});
Usage:
@flatten($array)
DragonCode\Support\Facades\Arr::shouldReceive() in PHPUnit:
Arr::shouldReceive('flattenKeys')->once()->andReturn(['a' => 1]);
ArrFlattenKeysTest) as templates.count()), native PHP may still be faster. Benchmark with:
$start = microtime(true);
Arr::count($array);
$time = microtime(true) - $start;
Facade Initialization:
DragonCode\Support\SupportServiceProvider is registered in config/app.php under providers.Method Naming Conflicts:
Arr::first() vs. Laravel’s collect()->first()).Arr::dragonFirst()) or document conflicts in a README.PHP Version Quirks:
Str::matchAll() require PHP 8.2+. Check composer.json constraints.if (function_exists('preg_match_all')) {
return Str::matchAll($pattern, $subject);
}
return [];
Filesystem Permissions:
File::load() or Directory::copy() may fail silently on restricted paths.File::load('file.txt', true); // $verbose = true
Invokable Helpers:
Arr::of()->toInstance() may throw if the array is malformed.if (!Arr::isAssoc($array)) {
throw new \InvalidArgumentException('Array must be associative.');
}
Facade Calls:
Arr::macro('debug', function () {
\Log::debug('Arr facade called with:', func_get_args());
return $this;
});
Array Methods:
dd() to inspect Arr::flattenKeys() output:
dd(Arr::of(['a' => ['b' => 1]])->flattenKeys());
String Regex:
preg_last_error() to debug Str::matchAll():
$matches = Str::matchAll('/invalid[pattern/', 'text');
if (preg_last_error()) {
\Log::error('Regex error:', preg_last_error());
}
Add Custom Methods:
DragonCode\Support\Facades\Arr):
public static function customMerge(array $array1, array $array2): array {
return array_merge($array1, $array2);
}
Tests/Unit/ArrCustomMergeTest.composer require with a custom fork.Override Existing Methods:
Arr::flattenKeys() in a service provider:
Arr::macro('flattenKeys', function () {
// Custom logic
return $this->array;
});
Publish Config:
php artisan vendor:publish --tag="support-config"
use Arr = DragonCode\Support\Facades\Arr;
const { Arr } = require('dragon-code/support');
mix.webpackConfig({
plugins: [new CustomPlugin(Arr.of(config).flattenKeys())]
});
Arr::toInstance() to convert collections to DTOs:
return response()->json(
Arr::of($users)->mapTo(UserResource::class)->toInstance()
);
How can I help you explore Laravel packages today?