You are browsing the Symfony 4 documentation, which changes significantly from Symfony 3.x. If your app doesn't use Symfony 4 yet, browse the Symfony 3.4 documentation.

The Contracts Component

4.2 version
Maintained

The Contracts Component

The Contracts component provides a set of abstractions extracted out of the Symfony components. They can be used to build on semantics that the Symfony components proved useful - and that already have battle-tested implementations.

Installation

1
$ composer require symfony/contracts

Note

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.

Usage

The abstractions in this package are useful to achieve loose coupling and interoperability. By using the provided interfaces as type hints, you are able to reuse any implementations that match their contracts. It could be a Symfony component, or another package provided by the PHP community at large.

Depending on their semantics, some interfaces can be combined with autowiring to seamlessly inject a service in your classes.

Others might be useful as labeling interfaces, to hint about a specific behavior that can be enabled when using autoconfiguration or manual service tagging (or any other means provided by your framework.)

Design Principles

  • Contracts are split by domain, each into their own sub-namespaces;
  • Contracts are small and consistent sets of PHP interfaces, traits, normative docblocks and reference test suites when applicable, ...;
  • Contracts must have a proven implementation to enter this repository;
  • Contracts must be backward compatible with existing Symfony components.

Packages that implement specific contracts should list them in the provide section of their composer.json file, using the symfony/*-contracts-implementation convention. For example:

1
2
3
4
5
6
{
    "...": "...",
    "provide": {
        "symfony/cache-contracts-implementation": "1.0"
    }
}

Frequently Asked Questions

How Is this Different From PHP-FIG's PSRs?

When applicable, the provided contracts are built on top of PHP-FIG's PSRs. However, PHP-FIG has different goals and different processes. Symfony Contracts focuses on providing abstractions that are useful on their own while still compatible with implementations provided by Symfony.

Why Isn't this Package Split into Several Packages?

Putting all interfaces in one package eases discoverability and dependency management. Instead of dealing with a myriad of small packages and the corresponding matrix of versions, you only deal with one package and one version. Also when using IDE autocompletion or reading the source code, it makes it easier to figure out which contracts are provided.

There are two downsides to this approach:

  • You may have unused files in your vendor/ directory. This has no impact in practice because the file sizes are very small and there is no performance overhead at all since they are never loaded.
  • In the future, it will be impossible to use two different sub-namespaces in different major versions of the package. However, this package follows the Symfony BC + deprecation policies, with an additional restriction to never remove deprecated interfaces.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.