4 This document describes a common interface for dependency injection containers.
6 The goal set by `ContainerInterface` is to standardize how frameworks and libraries make use of a
7 container to obtain objects and parameters (called *entries* in the rest of this document).
9 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
10 "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be
11 interpreted as described in [RFC 2119][].
13 The word `implementor` in this document is to be interpreted as someone
14 implementing the `ContainerInterface` in a depency injection-related library or framework.
15 Users of dependency injections containers (DIC) are refered to as `user`.
17 [RFC 2119]: http://tools.ietf.org/html/rfc2119
24 - The `Interop\Container\ContainerInterface` exposes two methods : `get` and `has`.
26 - `get` takes one mandatory parameter: an entry identifier. It MUST be a string.
27 A call to `get` can return anything (a *mixed* value), or throws an exception if the identifier
28 is not known to the container. Two successive calls to `get` with the same
29 identifier SHOULD return the same value. However, depending on the `implementor`
30 design and/or `user` configuration, different values might be returned, so
31 `user` SHOULD NOT rely on getting the same value on 2 successive calls.
32 While `ContainerInterface` only defines one mandatory parameter in `get()`, implementations
33 MAY accept additional optional parameters.
35 - `has` takes one unique parameter: an entry identifier. It MUST return `true`
36 if an entry identifier is known to the container and `false` if it is not.
40 Exceptions directly thrown by the container MUST implement the
41 [`Interop\Container\Exception\ContainerException`](../src/Interop/Container/Exception/ContainerException.php).
43 A call to the `get` method with a non-existing id should throw a
44 [`Interop\Container\Exception\NotFoundException`](../src/Interop/Container/Exception/NotFoundException.php).
46 ### 1.3 Additional features
48 This section describes additional features that MAY be added to a container. Containers are not
49 required to implement these features to respect the ContainerInterface.
51 #### 1.3.1 Delegate lookup feature
53 The goal of the *delegate lookup* feature is to allow several containers to share entries.
54 Containers implementing this feature can perform dependency lookups in other containers.
56 Containers implementing this feature will offer a greater lever of interoperability
57 with other containers. Implementation of this feature is therefore RECOMMENDED.
59 A container implementing this feature:
61 - MUST implement the `ContainerInterface`
62 - MUST provide a way to register a delegate container (using a constructor parameter, or a setter,
63 or any possible way). The delegate container MUST implement the `ContainerInterface`.
65 When a container is configured to use a delegate container for dependencies:
67 - Calls to the `get` method should only return an entry if the entry is part of the container.
68 If the entry is not part of the container, an exception should be thrown
69 (as requested by the `ContainerInterface`).
70 - Calls to the `has` method should only return `true` if the entry is part of the container.
71 If the entry is not part of the container, `false` should be returned.
72 - If the fetched entry has dependencies, **instead** of performing
73 the dependency lookup in the container, the lookup is performed on the *delegate container*.
75 Important! By default, the lookup SHOULD be performed on the delegate container **only**, not on the container itself.
77 It is however allowed for containers to provide exception cases for special entries, and a way to lookup
78 into the same container (or another container) instead of the delegate container.
83 The interfaces and classes described as well as relevant exception are provided as part of the
84 [container-interop/container-interop](https://packagist.org/packages/container-interop/container-interop) package.
86 3. `Interop\Container\ContainerInterface`
87 -----------------------------------------
91 namespace Interop\Container;
93 use Interop\Container\Exception\ContainerException;
94 use Interop\Container\Exception\NotFoundException;
97 * Describes the interface of a container that exposes methods to read its entries.
99 interface ContainerInterface
102 * Finds an entry of the container by its identifier and returns it.
104 * @param string $id Identifier of the entry to look for.
106 * @throws NotFoundException No entry was found for this identifier.
107 * @throws ContainerException Error while retrieving the entry.
109 * @return mixed Entry.
111 public function get($id);
114 * Returns true if the container can return an entry for the given identifier.
115 * Returns false otherwise.
117 * @param string $id Identifier of the entry to look for.
121 public function has($id);
125 4. `Interop\Container\Exception\ContainerException`
126 ---------------------------------------------------
130 namespace Interop\Container\Exception;
133 * Base interface representing a generic exception in a container.
135 interface ContainerException
140 5. `Interop\Container\Exception\NotFoundException`
141 ---------------------------------------------------
145 namespace Interop\Container\Exception;
148 * No entry was found in the container.
150 interface NotFoundException extends ContainerException