9f609674c80108f35b3362200200ee5f641d9582
[GitHub/WoltLab/WCF.git] /
1 Container interface
2 ===================
3
4 This document describes a common interface for dependency injection containers.
5
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).
8
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][].
12
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`.
16
17 [RFC 2119]: http://tools.ietf.org/html/rfc2119
18
19 1. Specification
20 -----------------
21
22 ### 1.1 Basics
23
24 - The `Interop\Container\ContainerInterface` exposes two methods : `get` and `has`.
25
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.
34
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.
37
38 ### 1.2 Exceptions
39
40 Exceptions directly thrown by the container MUST implement the
41 [`Interop\Container\Exception\ContainerException`](../src/Interop/Container/Exception/ContainerException.php).
42
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).
45
46 ### 1.3 Additional features
47
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.
50
51 #### 1.3.1 Delegate lookup feature
52
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.
55
56 Containers implementing this feature will offer a greater lever of interoperability
57 with other containers. Implementation of this feature is therefore RECOMMENDED.
58
59 A container implementing this feature:
60
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`.
64
65 When a container is configured to use a delegate container for dependencies:
66
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*.
74
75 Important! By default, the lookup SHOULD be performed on the delegate container **only**, not on the container itself.
76
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.
79
80 2. Package
81 ----------
82
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.
85
86 3. `Interop\Container\ContainerInterface`
87 -----------------------------------------
88
89 ```php
90 <?php
91 namespace Interop\Container;
92
93 use Interop\Container\Exception\ContainerException;
94 use Interop\Container\Exception\NotFoundException;
95
96 /**
97 * Describes the interface of a container that exposes methods to read its entries.
98 */
99 interface ContainerInterface
100 {
101 /**
102 * Finds an entry of the container by its identifier and returns it.
103 *
104 * @param string $id Identifier of the entry to look for.
105 *
106 * @throws NotFoundException No entry was found for this identifier.
107 * @throws ContainerException Error while retrieving the entry.
108 *
109 * @return mixed Entry.
110 */
111 public function get($id);
112
113 /**
114 * Returns true if the container can return an entry for the given identifier.
115 * Returns false otherwise.
116 *
117 * @param string $id Identifier of the entry to look for.
118 *
119 * @return boolean
120 */
121 public function has($id);
122 }
123 ```
124
125 4. `Interop\Container\Exception\ContainerException`
126 ---------------------------------------------------
127
128 ```php
129 <?php
130 namespace Interop\Container\Exception;
131
132 /**
133 * Base interface representing a generic exception in a container.
134 */
135 interface ContainerException
136 {
137 }
138 ```
139
140 5. `Interop\Container\Exception\NotFoundException`
141 ---------------------------------------------------
142
143 ```php
144 <?php
145 namespace Interop\Container\Exception;
146
147 /**
148 * No entry was found in the container.
149 */
150 interface NotFoundException extends ContainerException
151 {
152 }
153 ```