Completed
Push — master ( c23c04...32905f )
by Nikita
01:15
created

ServiceProvider::register()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 14
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
71
        $this->app->singleton(GraphQLManager::class, function ($app) {
72
            return new GraphQLManager($app);
73
        });
74
75
        $this->app->singleton(AbstractSchema::class, function ($app) {
76
            return $app->make(GraphQLManager::class)->schema();
77
        });
78
79
        $this->app->alias(
80
            GraphQLManager::class, GraphQLManagerContract::class
81
        );
82
83
        $this->app->singleton(Processor::class, function ($app) {
84
            return new Processor($app->make(AbstractSchema::class));
85
        });
86
87
        $this->app->singleton('graphql.types', function ($app) {
88
            $types = config('graphql.types') ?: [];
89
90
            return new TypeRegistry($app, $types);
91
        });
92
    }
93
94
    /**
95
     * Register console commands.
96
     *
97
     * @return void
98
     */
99
    protected function registerCommands()
100
    {
101
        $this->app->bind('command.make:graphql:field', FieldMakeCommand::class);
102
        $this->app->bind('command.make:graphql:schema', SchemaMakeCommand::class);
103
        $this->app->bind('command.make:graphql:type', TypeMakeCommand::class);
104
105
        $this->commands([
106
            'command.make:graphql:field',
107
            'command.make:graphql:schema',
108
            'command.make:graphql:type',
109
        ]);
110
    }
111
112
    /**
113
     * Return GraphQL config path.
114
     *
115
     * @return string
116
     */
117
    protected function configPath()
118
    {
119
        return __DIR__.'/../../config/graphql.php';
120
    }
121
}
122