Completed
Push — master ( 89fbbd...659c82 )
by Nikita
01:18
created

ServiceProvider::registerManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace LaraComponents\GraphQL;
4
5
use Youshido\GraphQL\Execution\Processor;
6
use Youshido\GraphQL\Schema\AbstractSchema;
7
use LaraComponents\GraphQL\Helpers\TypeRegistry;
8
use LaraComponents\GraphQL\Console\TypeMakeCommand;
9
use LaraComponents\GraphQL\Console\FieldMakeCommand;
10
use LaraComponents\GraphQL\Console\SchemaMakeCommand;
11
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
12
use LaraComponents\GraphQL\Contracts\GraphQLManager as GraphQLManagerContract;
13
14
class ServiceProvider extends BaseServiceProvider
15
{
16
    /**
17
     * Indicates if loading of the provider is deferred.
18
     *
19
     * @var bool
20
     */
21
    protected $defer = false;
22
23
    /**
24
     * Bootstrap any application services.
25
     *
26
     * @return void
27
     */
28
    public function boot()
29
    {
30
        $this->bootPublishes();
31
        $this->bootRouter();
32
    }
33
34
    /**
35
     * Bootstrap router.
36
     *
37
     * @return void
38
     */
39
    protected function bootRouter()
40
    {
41
        $router = $this->app->make('router');
42
43
        $router->group([
44
            'prefix' => config('graphql.route.prefix'),
45
            'middleware' => config('graphql.route.middleware'),
46
        ], function () {
47
            include __DIR__.'/routes.php';
48
        });
49
    }
50
51
    /**
52
     * Bootstrap publishes.
53
     *
54
     * @return void
55
     */
56
    protected function bootPublishes()
57
    {
58
        $this->publishes([$this->configPath() => config_path('graphql.php')], 'config');
59
    }
60
61
    /**
62
     * Register the service provider.
63
     *
64
     * @return void
65
     */
66
    public function register()
67
    {
68
        $this->mergeConfigFrom($this->configPath(), 'graphql');
69
        $this->registerCommands();
70
        $this->registerManager();
71
        $this->registerTypeRegistry();
72
73
        $this->app->singleton(AbstractSchema::class, function ($app) {
74
            return $app->make(GraphQLManager::class)->schema();
75
        });
76
77
        $this->app->singleton(Processor::class, function ($app) {
78
            return new Processor($app->make(AbstractSchema::class));
79
        });
80
    }
81
82
    /**
83
     * Register console commands.
84
     *
85
     * @return void
86
     */
87
    protected function registerCommands()
88
    {
89
        $this->app->bind('command.make:graphql:field', FieldMakeCommand::class);
90
        $this->app->bind('command.make:graphql:schema', SchemaMakeCommand::class);
91
        $this->app->bind('command.make:graphql:type', TypeMakeCommand::class);
92
93
        $this->commands([
94
            'command.make:graphql:field',
95
            'command.make:graphql:schema',
96
            'command.make:graphql:type',
97
        ]);
98
    }
99
100
    /**
101
     * Register the manager.
102
     *
103
     * @return void
104
     */
105
    protected function registerManager()
106
    {
107
        $this->app->singleton(GraphQLManager::class, function ($app) {
108
            return new GraphQLManager($app);
109
        });
110
111
        $this->app->alias(
112
            GraphQLManager::class, GraphQLManagerContract::class
113
        );
114
    }
115
116
    /**
117
     * Register the type registry.
118
     *
119
     * @return void
120
     */
121
    protected function registerTypeRegistry()
122
    {
123
        $this->app->singleton('graphql.types', function ($app) {
124
            $types = config('graphql.types') ?: [];
125
126
            return new TypeRegistry($app, $types);
127
        });
128
    }
129
130
    /**
131
     * Return GraphQL config path.
132
     *
133
     * @return string
134
     */
135
    protected function configPath()
136
    {
137
        return __DIR__.'/../../config/graphql.php';
138
    }
139
}
140