1+ <?php
2+
3+
4+ namespace TheCodingMachine \GraphQLite \Laravel ;
5+
6+
7+ use function class_exists ;
8+ use Illuminate \Contracts \Container \Container ;
9+ use Psr \Container \ContainerExceptionInterface ;
10+ use Psr \Container \ContainerInterface ;
11+ use Psr \Container \NotFoundExceptionInterface ;
12+
13+ /**
14+ * A container adapter around Laravel containers that adds a "sane" implementation of PSR-11.
15+ * Notably, "has" will return true if the class exists, since Laravel is an auto-wiring framework.
16+ */
17+ class SanePsr11ContainerAdapter implements ContainerInterface
18+ {
19+ /**
20+ * @var Container
21+ */
22+ private $ container ;
23+
24+ public function __construct (Container $ container )
25+ {
26+ $ this ->container = $ container ;
27+ }
28+
29+ /**
30+ * Finds an entry of the container by its identifier and returns it.
31+ *
32+ * @param string $id Identifier of the entry to look for.
33+ *
34+ * @throws NotFoundExceptionInterface No entry was found for **this** identifier.
35+ * @throws ContainerExceptionInterface Error while retrieving the entry.
36+ *
37+ * @return mixed Entry.
38+ */
39+ public function get ($ id )
40+ {
41+ return $ this ->container ->get ($ id );
42+ }
43+
44+ /**
45+ * Returns true if the container can return an entry for the given identifier.
46+ * Returns false otherwise.
47+ *
48+ * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
49+ * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
50+ *
51+ * @param string $id Identifier of the entry to look for.
52+ *
53+ * @return bool
54+ */
55+ public function has ($ id )
56+ {
57+ if (class_exists ($ id )) {
58+ return true ;
59+ }
60+ return $ this ->container ->has ($ id );
61+ }
62+ }
0 commit comments