|
| 1 | +.. index:: |
| 2 | + single: Contracts |
| 3 | + single: Components; Contracts |
| 4 | + |
| 5 | +The Contracts Component |
| 6 | +======================= |
| 7 | + |
| 8 | + The Contracts component provides a set of abstractions extracted out of the |
| 9 | + Symfony components. They can be used to build on semantics that the Symfony |
| 10 | + components proved useful - and that already have battle-tested implementations. |
| 11 | + |
| 12 | +Installation |
| 13 | +------------ |
| 14 | + |
| 15 | +.. code-block:: terminal |
| 16 | +
|
| 17 | + $ composer require symfony/contracts |
| 18 | +
|
| 19 | +.. include:: /components/require_autoload.rst.inc |
| 20 | + |
| 21 | +Usage |
| 22 | +----- |
| 23 | + |
| 24 | +The abstractions in this package are useful to achieve loose coupling and |
| 25 | +interoperability. By using the provided interfaces as type hints, you are able |
| 26 | +to reuse any implementations that match their contracts. It could be a Symfony |
| 27 | +component, or another package provided by the PHP community at large. |
| 28 | + |
| 29 | +Depending on their semantics, some interfaces can be combined with |
| 30 | +:doc:`autowiring </service_container/autowiring>` to seamlessly inject a service |
| 31 | +in your classes. |
| 32 | + |
| 33 | +Others might be useful as labeling interfaces, to hint about a specific behavior |
| 34 | +that can be enabled when using :ref:`autoconfiguration <services-autoconfigure>` |
| 35 | +or manual :doc:`service tagging </service_container/tags>` (or any other means |
| 36 | +provided by your framework.) |
| 37 | + |
| 38 | +Design Principles |
| 39 | +----------------- |
| 40 | + |
| 41 | +* Contracts are split by domain, each into their own sub-namespaces; |
| 42 | +* Contracts are small and consistent sets of PHP interfaces, traits, normative |
| 43 | + docblocks and reference test suites when applicable, ...; |
| 44 | +* Contracts must have a proven implementation to enter this repository; |
| 45 | +* Contracts must be backward compatible with existing Symfony components. |
| 46 | + |
| 47 | +Packages that implement specific contracts should list them in the ``provide`` |
| 48 | +section of their ``composer.json`` file, using the |
| 49 | +``symfony/*-contracts-implementation`` convention. For example: |
| 50 | + |
| 51 | +.. code-block:: javascript |
| 52 | +
|
| 53 | + { |
| 54 | + "...": "...", |
| 55 | + "provide": { |
| 56 | + "symfony/cache-contracts-implementation": "1.0" |
| 57 | + } |
| 58 | + } |
| 59 | +
|
| 60 | +Frequently Asked Questions |
| 61 | +-------------------------- |
| 62 | + |
| 63 | +How Is this Different From PHP-FIG's PSRs? |
| 64 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 65 | + |
| 66 | +When applicable, the provided contracts are built on top of `PHP-FIG`_'s PSRs. |
| 67 | +However, PHP-FIG has different goals and different processes. Symfony Contracts |
| 68 | +focuses on providing abstractions that are useful on their own while still |
| 69 | +compatible with implementations provided by Symfony. |
| 70 | + |
| 71 | +Why Isn't this Package Split into Several Packages? |
| 72 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 73 | + |
| 74 | +Putting all interfaces in one package eases discoverability and dependency |
| 75 | +management. Instead of dealing with a myriad of small packages and the |
| 76 | +corresponding matrix of versions, you only deal with one package and one |
| 77 | +version. Also when using IDE autocompletion or reading the source code, it makes |
| 78 | +it easier to figure out which contracts are provided. |
| 79 | + |
| 80 | +There are two downsides to this approach: |
| 81 | + |
| 82 | +* You may have unused files in your ``vendor/`` directory. This has no impact in |
| 83 | + practice because the file sizes are very small and there is no performance |
| 84 | + overhead at all since they are never loaded. |
| 85 | +* In the future, it will be impossible to use two different sub-namespaces in |
| 86 | + different major versions of the package. However, this package follows the |
| 87 | + :doc:`Symfony BC + deprecation </contributing/code/bc>` policies, with an |
| 88 | + additional restriction to never remove deprecated interfaces. |
| 89 | + |
| 90 | +.. _`PHP-FIG`: https://www.php-fig.org/ |
0 commit comments