dragon-code/contracts
Dragon Code Contracts provides a lightweight set of PHP interfaces (contracts) you can reuse across projects and packages. Use it to standardize common behaviors and keep implementations decoupled and consistent throughout your codebase.
composer require dragon-code/contracts
Dtoable contract for a Data Transfer Object (DTO).
use TheDragonCode\Contracts\Dto\Dtoable;
class UserDto implements Dtoable
{
public function toArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
];
}
}
vendor/the-dragon-code/contracts/src/ for all available contracts.Dto, Cache, Queue, and Http namespaces for Laravel-specific patterns.DtoableDtoable for consistent serialization across APIs or services.use TheDragonCode\Contracts\Dto\Dtoable;
class ApiResponseDto implements Dtoable
{
public function toArray(): array
{
return [
'success' => true,
'data' => $this->data->toArray(),
];
}
}
dragon-code/helpers for advanced serialization (e.g., nested DTOs).Cache\Storeuse TheDragonCode\Contracts\Cache\Store;
class RedisCache implements Store
{
public function get(string $key, $default = null): mixed
{
return cache()->get($key, $default);
}
public function rememberForever(string $key, callable $callback): mixed
{
return cache()->rememberForever($key, $callback);
}
}
Store into services instead of Laravel’s Illuminate\Contracts\Cache\Store.ShouldQueue and ShouldBeUniqueuse TheDragonCode\Contracts\Queue\ShouldQueue;
use TheDragonCode\Contracts\Queue\ShouldBeUnique;
class ProcessPaymentJob implements ShouldQueue, ShouldBeUnique
{
public function handle(): void
{
// Job logic
}
public function uniqueFor(): string
{
return $this->paymentId;
}
}
dispatch() or dispatchSync() for consistency.Http\Builderuse TheDragonCode\Contracts\Http\Builder;
class ApiClient implements Builder
{
public function get(string $uri, array $query = []): array
{
return Http::get($uri, $query)->json();
}
public function getBaseDomain(): string
{
return config('services.api.base_url');
}
}
Migrate\Contractuse TheDragonCode\Contracts\Migrate\Contract;
class CreateUsersTable implements Contract
{
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
});
}
}
register():
$this->app->bind(
TheDragonCode\Contracts\Cache\Store::class,
App\Services\RedisCache::class
);
class UserService
{
public function __construct(
private Store $cache,
private Builder $httpClient
) {}
}
Laravel Version Mismatches:
laravel/framework version matches the package’s release notes.dragon-code/contracts to a compatible version (e.g., ^2.24.0 for Laravel 12).Overriding Laravel’s Built-in Contracts:
Illuminate\Contracts\Cache\Store). Use TheDragonCode\Contracts\Cache\Store for extended functionality (e.g., rememberForever).Mocking in Tests:
ShouldQueue) rely on Laravel’s queue system. Mock interfaces carefully:
$mockCache = Mockery::mock(TheDragonCode\Contracts\Cache\Store::class);
$mockCache->shouldReceive('get')->andReturn('cached_value');
ShouldQueue trait conflicts with the ShouldQueue interface. Use the interface for testing.Symfony Dependency Conflicts:
symfony/http-kernel). Ensure your composer.json aligns with the package’s dependencies.composer update after installation to resolve version conflicts.Interface Not Found:
use TheDragonCode\Contracts\Dto\Dtoable).composer dump-autoload if the interface isn’t recognized.Method Not Implemented:
Dtoable::toArray() must return array).Queue Job Deduplication Fails:
ShouldBeUnique::uniqueFor() returns a consistent string (e.g., userId-paymentType).uniqueFor() value to verify uniqueness.Custom Contracts:
namespace App\Contracts;
use TheDragonCode\Contracts\Dto\Dtoable;
interface ExtendedDtoable extends Dtoable
{
public function toJson(): string;
}
Laravel Facades:
Facades\Cache::get('key'); // Uses TheDragonCode\Contracts\Cache\Store
Cache facade to use the contract:
Cache::shouldUseContractStore(true);
Dynamic Method Binding:
Macroable trait to add dynamic methods to contract implementations:
class CustomCache implements Store
{
use \Illuminate\Contracts\Macroable;
public function __construct()
{
$this->macro('rememberDays', function ($key, $days, $callback) {
return $this->remember($key, $days * 24 * 60, $callback);
});
}
}
Cache\Store contract’s rememberForever method is optimized for Laravel’s cache drivers (e.g., Redis, database).ShouldBeUnique adds minimal overhead (~1ms per job) for deduplication checks.config/cache.php if needed:
'default' => env('CACHE_DRIVER', 'dragon'), // Custom driver
ShouldQueue, specify the queue connection in the job’s connection property:
class ProcessPaymentJob implements ShouldQueue
{
public $connection = 'redis';
}
How can I help you explore Laravel packages today?