symfony/contracts
Symfony Contracts provides small, domain-focused PHP interfaces, traits, and normative docblocks extracted from Symfony components. Use them as stable type hints for loose coupling, interoperability, and easy DI/autowiring with battle-tested implementations.
Architecture fit
Symfony Contracts provides a highly strategic fit for Laravel applications targeting decoupled, reusable, and framework-agnostic architecture. The package’s domain-specific interfaces (e.g., CacheInterface, EventDispatcherInterface, HttpClientInterface) align perfectly with Laravel’s service container and dependency injection patterns, enabling:
CacheInterface) instead of Laravel’s concrete classes.Integration feasibility Feasibility is moderate-to-high but requires explicit bridging due to Laravel’s lack of native contract implementations. Key considerations:
alias() and bind() methods can map contracts to concrete implementations (e.g., CacheInterface → Illuminate\Cache\Repository).symfony/cache, symfony/http-client) must be installed and configured separately.Target class does not exist errors.Technical risk
symfony/cache-contracts:3.x) and Laravel’s underlying implementations (e.g., symfony/cache:6.x) may introduce runtime errors or behavioral inconsistencies.CacheInterface::get() retry policies or HttpClientInterface middleware). Custom adapters or extensions may be required.AppServiceProvider.CacheManager) with contracts requires careful sequencing to avoid breaking changes in existing codebases.Key questions
symfony/cache, symfony/http-client) or third-party libraries (e.g., predis/predis, guzzlehttp/guzzle)?CacheInterface retry logic or HttpClientInterface middleware)?Cache::store()) with contract-based service calls in new development, and how will we handle legacy facade usage?Stack fit Symfony Contracts is ideal for infrastructure-heavy layers where abstraction reduces vendor lock-in and improves reusability:
Illuminate\Cache\Repository or CacheManager with Symfony\Contracts\Cache\CacheInterface.Symfony\Contracts\EventDispatcher\EventDispatcherInterface alongside Laravel’s Dispatcher for cross-framework compatibility.Symfony\Contracts\HttpClient\HttpClientInterface to replace Guzzle or Illuminate\Http\Client for reusable HTTP logic.Symfony\Contracts\Mailer\MailerInterface or TranslatorInterface for framework-agnostic email and localization.Symfony\Contracts\Messenger\MessageBusInterface for queue-agnostic design (e.g., integrating with Symfony Messenger or Laravel Queues).Symfony\Contracts\EventDispatcher\LoggerInterface for standardized logging contracts.Migration path
Assessment phase:
Cache, Events, Http, Mail).predis/predis implements CacheInterface).Pilot implementation:
composer require symfony/contracts symfony/cache symfony/http-client symfony/event-dispatcher
AppServiceProvider:
$this->app->bind(
Symfony\Contracts\Cache\CacheInterface::class,
Symfony\Component\Cache\Adapter\AdapterInterface::class
);
$this->app->alias(
Symfony\Contracts\Cache\CacheInterface::class,
Illuminate\Cache\Repository::class
);
CacheInterface instead of CacheManager or Repository.MockCache) to ensure compliance.Gradual rollout:
EventDispatcherInterface, HttpClientInterface).Cache::store(), Http::get()) with contract-based service calls in new code.app()->make()).Legacy adaptation:
LaravelCacheAdapter implements CacheInterface).Symfony\Contracts\Cache\CacheItemInterface) for partial compliance in legacy code.Autowiring configuration:
config/app.php:
'providers' => [
// ...
Symfony\Contracts\Cache\CacheInterface::class => Symfony\Component\Cache\Adapter\AdapterInterface::class,
],
config/autowire.php.Compatibility
provide in composer.json (e.g., "symfony/cache-implementation": "1.0"). Examples:
symfony/cache (official implementation of CacheInterface).predis/predis (Redis cache implementing CacheInterface).guzzlehttp/guzzle (implements HttpClientInterface).CacheInterface extends Psr\SimpleCache\CacheInterface) but add Symfony-specific features (e.g., tagging, retry policies).Sequencing
Cache::remember(), Http::post()) with injected services in new features.MockCache, MockHttpClient) and integration tests with real implementations.@param Symfony\Contracts\Cache\CacheInterface $cache).How can I help you explore Laravel packages today?