Skip to content

Commit 4c60859

Browse files
committed
Adding a PSR-11 container adapter to get back sane defaults
1 parent e9a7625 commit 4c60859

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/Providers/GraphQLiteServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
1515
use TheCodingMachine\GraphQLite\Laravel\Controllers\GraphQLiteController;
1616
use TheCodingMachine\GraphQLite\Laravel\Middlewares\GraphQLMiddleware;
17+
use TheCodingMachine\GraphQLite\Laravel\SanePsr11ContainerAdapter;
1718
use TheCodingMachine\GraphQLite\Schema;
1819
use TheCodingMachine\GraphQLite\SchemaFactory;
1920
use GraphQL\Type\Schema as WebonyxSchema;
@@ -62,7 +63,7 @@ public function register()
6263
});
6364

6465
$this->app->singleton(SchemaFactory::class, function (Application $app) {
65-
$service = new SchemaFactory($app->make(Repository::class), $app);
66+
$service = new SchemaFactory($app->make(Repository::class), new SanePsr11ContainerAdapter($app));
6667

6768
$controllers = config('graphqlite.controllers', 'App\\Http\\Controllers');
6869
if (!is_iterable($controllers)) {

src/SanePsr11ContainerAdapter.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)