1
<?php
2

3
namespace Nuwave\Lighthouse;
4

5
use Closure;
6
use Exception;
7
use Illuminate\Contracts\Config\Repository as ConfigRepository;
8
use Illuminate\Contracts\Container\Container;
9
use Illuminate\Foundation\Application as LaravelApplication;
10
use Illuminate\Routing\Router;
11
use Illuminate\Support\ServiceProvider;
12
use Laravel\Lumen\Application as LumenApplication;
13
use Nuwave\Lighthouse\Console\CacheCommand;
14
use Nuwave\Lighthouse\Console\ClearCacheCommand;
15
use Nuwave\Lighthouse\Console\DirectiveCommand;
16
use Nuwave\Lighthouse\Console\IdeHelperCommand;
17
use Nuwave\Lighthouse\Console\InterfaceCommand;
18
use Nuwave\Lighthouse\Console\MutationCommand;
19
use Nuwave\Lighthouse\Console\PrintSchemaCommand;
20
use Nuwave\Lighthouse\Console\QueryCommand;
21
use Nuwave\Lighthouse\Console\ScalarCommand;
22
use Nuwave\Lighthouse\Console\SubscriptionCommand;
23
use Nuwave\Lighthouse\Console\UnionCommand;
24
use Nuwave\Lighthouse\Console\ValidateSchemaCommand;
25
use Nuwave\Lighthouse\Console\ValidatorCommand;
26
use Nuwave\Lighthouse\Execution\ContextFactory;
27
use Nuwave\Lighthouse\Execution\ErrorPool;
28
use Nuwave\Lighthouse\Execution\SingleResponse;
29
use Nuwave\Lighthouse\Execution\ValidationRulesProvider;
30
use Nuwave\Lighthouse\Schema\AST\ASTBuilder;
31
use Nuwave\Lighthouse\Schema\DirectiveLocator;
32
use Nuwave\Lighthouse\Schema\ResolverProvider;
33
use Nuwave\Lighthouse\Schema\Source\SchemaSourceProvider;
34
use Nuwave\Lighthouse\Schema\Source\SchemaStitcher;
35
use Nuwave\Lighthouse\Schema\TypeRegistry;
36
use Nuwave\Lighthouse\Schema\Values\FieldValue;
37
use Nuwave\Lighthouse\Support\AppVersion;
38
use Nuwave\Lighthouse\Support\Compatibility\LaravelMiddlewareAdapter;
39
use Nuwave\Lighthouse\Support\Compatibility\LumenMiddlewareAdapter;
40
use Nuwave\Lighthouse\Support\Compatibility\MiddlewareAdapter;
41
use Nuwave\Lighthouse\Support\Contracts\CanStreamResponse;
42
use Nuwave\Lighthouse\Support\Contracts\CreatesContext;
43
use Nuwave\Lighthouse\Support\Contracts\CreatesResponse;
44
use Nuwave\Lighthouse\Support\Contracts\ProvidesResolver;
45
use Nuwave\Lighthouse\Support\Contracts\ProvidesSubscriptionResolver;
46
use Nuwave\Lighthouse\Support\Contracts\ProvidesValidationRules;
47
use Nuwave\Lighthouse\Support\Http\Responses\ResponseStream;
48
use Nuwave\Lighthouse\Testing\TestingServiceProvider;
49

50
class LighthouseServiceProvider extends ServiceProvider
51
{
52
    /**
53
     * Bootstrap any application services.
54
     */
55 1
    public function boot(ConfigRepository $configRepository): void
56
    {
57 1
        $this->publishes([
58 1
            __DIR__.'/lighthouse.php' => $this->app->configPath().'/lighthouse.php',
59 1
        ], 'lighthouse-config');
60

61 1
        $this->publishes([
62 1
            __DIR__.'/default-schema.graphql' => $configRepository->get('lighthouse.schema.register'),
63 1
        ], 'lighthouse-schema');
64

65 1
        $this->loadRoutesFrom(__DIR__.'/Support/Http/routes.php');
66
    }
67

68
    /**
69
     * Load routes from provided path.
70
     *
71
     * @param  string  $path
72
     */
73 1
    protected function loadRoutesFrom($path): void
74
    {
75 1
        if (AppVersion::isLumen()) {
76 0
            require \Safe\realpath($path);
77

78 0
            return;
79
        }
80

81 1
        parent::loadRoutesFrom($path);
82
    }
83

84
    /**
85
     * Register any application services.
86
     */
87 1
    public function register(): void
88
    {
89 1
        $this->mergeConfigFrom(__DIR__.'/lighthouse.php', 'lighthouse');
90

91 1
        $this->app->singleton(GraphQL::class);
92 1
        $this->app->singleton(ASTBuilder::class);
93 1
        $this->app->singleton(DirectiveLocator::class);
94 1
        $this->app->singleton(TypeRegistry::class);
95 1
        $this->app->singleton(ErrorPool::class);
96 1
        $this->app->singleton(CreatesContext::class, ContextFactory::class);
97 1
        $this->app->singleton(CanStreamResponse::class, ResponseStream::class);
98

99 1
        $this->app->bind(CreatesResponse::class, SingleResponse::class);
100

101
        $this->app->singleton(SchemaSourceProvider::class, function (): SchemaStitcher {
102 0
            return new SchemaStitcher(
103 0
                config('lighthouse.schema.register', '')
104
            );
105 1
        });
106

107 1
        $this->app->bind(ProvidesResolver::class, ResolverProvider::class);
108
        $this->app->bind(ProvidesSubscriptionResolver::class, function (): ProvidesSubscriptionResolver {
109
            return new class implements ProvidesSubscriptionResolver {
110 0
                public function provideSubscriptionResolver(FieldValue $fieldValue): Closure
111
                {
112 0
                    throw new Exception(
113 0
                        'Add the SubscriptionServiceProvider to your config/app.php to enable subscriptions.'
114
                    );
115
                }
116
            };
117 1
        });
118

119 1
        $this->app->bind(ProvidesValidationRules::class, ValidationRulesProvider::class);
120

121
        $this->app->singleton(MiddlewareAdapter::class, function (Container $app): MiddlewareAdapter {
122
            // prefer using fully-qualified class names here when referring to Laravel-only or Lumen-only classes
123 0
            if ($app instanceof LaravelApplication) {
124 0
                return new LaravelMiddlewareAdapter(
125 0
                    $app->get(Router::class)
126
                );
127
            }
128

129 0
            if ($app instanceof LumenApplication) {
130 0
                return new LumenMiddlewareAdapter($app);
131
            }
132

133 0
            throw new Exception(
134 0
                'Could not correctly determine Laravel framework flavor, got '.get_class($app).'.'
135
            );
136 1
        });
137

138 1
        if ($this->app->runningInConsole()) {
139 1
            $this->commands([
140 1
                CacheCommand::class,
141
                ClearCacheCommand::class,
142
                DirectiveCommand::class,
143
                IdeHelperCommand::class,
144
                InterfaceCommand::class,
145
                MutationCommand::class,
146
                PrintSchemaCommand::class,
147
                QueryCommand::class,
148
                ScalarCommand::class,
149
                SubscriptionCommand::class,
150
                UnionCommand::class,
151
                ValidateSchemaCommand::class,
152
                ValidatorCommand::class,
153
            ]);
154
        }
155

156 1
        if ($this->app->runningUnitTests()) {
157 1
            $this->app->register(TestingServiceProvider::class);
158
        }
159
    }
160
}

Read our documentation on viewing source code .

Loading