-
Notifications
You must be signed in to change notification settings - Fork 1
/
PackageProvider.php
96 lines (84 loc) · 4.23 KB
/
PackageProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php declare(strict_types = 1);
namespace LastDragon_ru\LaraASP\GraphQL;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\ServiceProvider;
use LastDragon_ru\LaraASP\Core\Provider\WithConfig;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderFieldResolver as BuilderFieldResolverContract;
use LastDragon_ru\LaraASP\GraphQL\Builder\Defaults\BuilderFieldResolver;
use LastDragon_ru\LaraASP\GraphQL\Builder\ManipulatorFactory;
use LastDragon_ru\LaraASP\GraphQL\Directives\Definitions\TypeDirective;
use LastDragon_ru\LaraASP\GraphQL\Printer\DirectiveResolver;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByDirective;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchBySchemaDirective;
use LastDragon_ru\LaraASP\GraphQL\SortBy\Contracts\SorterFactory as SorterFactoryContract;
use LastDragon_ru\LaraASP\GraphQL\SortBy\Definitions\SortByDirective;
use LastDragon_ru\LaraASP\GraphQL\SortBy\Definitions\SortBySchemaDirective;
use LastDragon_ru\LaraASP\GraphQL\SortBy\SorterFactory;
use LastDragon_ru\LaraASP\GraphQL\Stream\Contracts\StreamFactory as StreamFactoryContract;
use LastDragon_ru\LaraASP\GraphQL\Stream\Definitions\StreamDirective;
use LastDragon_ru\LaraASP\GraphQL\Stream\StreamFactory;
use LastDragon_ru\LaraASP\GraphQLPrinter\Contracts\DirectiveResolver as DirectiveResolverContract;
use LastDragon_ru\LaraASP\GraphQLPrinter\Contracts\Printer as SchemaPrinterContract;
use LastDragon_ru\LaraASP\GraphQLPrinter\Contracts\Settings as SettingsContract;
use LastDragon_ru\LaraASP\GraphQLPrinter\Printer;
use LastDragon_ru\LaraASP\GraphQLPrinter\Settings\DefaultSettings;
use Nuwave\Lighthouse\Events\BuildSchemaString;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;
use Nuwave\Lighthouse\Schema\SchemaBuilder;
use Override;
use function array_slice;
use function explode;
use function implode;
class PackageProvider extends ServiceProvider {
use WithConfig;
public function boot(Dispatcher $dispatcher): void {
$this->bootDirectives($dispatcher);
}
#[Override]
public function register(): void {
parent::register();
$this->registerConfig(PackageConfig::class);
$this->registerBindings();
$this->registerSchemaPrinter();
}
protected function bootDirectives(Dispatcher $dispatcher): void {
$dispatcher->listen(
RegisterDirectiveNamespaces::class,
static function (): array {
return [
implode('\\', array_slice(explode('\\', SearchByDirective::class), 0, -1)),
implode('\\', array_slice(explode('\\', SortByDirective::class), 0, -1)),
implode('\\', array_slice(explode('\\', StreamDirective::class), 0, -1)),
implode('\\', array_slice(explode('\\', TypeDirective::class), 0, -1)),
];
},
);
$dispatcher->listen(BuildSchemaString::class, SearchBySchemaDirective::class);
$dispatcher->listen(BuildSchemaString::class, SortBySchemaDirective::class);
}
protected function registerBindings(): void {
$this->app->scopedIf(ManipulatorFactory::class);
$this->app->scopedIf(SorterFactoryContract::class, SorterFactory::class);
$this->app->scopedIf(StreamFactoryContract::class, StreamFactory::class);
$this->app->scopedIf(BuilderFieldResolverContract::class, BuilderFieldResolver::class);
}
protected function registerSchemaPrinter(): void {
$this->app->scopedIf(SettingsContract::class, DefaultSettings::class);
$this->app->scopedIf(DirectiveResolverContract::class, DirectiveResolver::class);
$this->app->scopedIf(
SchemaPrinterContract::class,
static function (Container $container): SchemaPrinterContract {
$settings = $container->make(SettingsContract::class);
$resolver = $container->make(DirectiveResolverContract::class);
$schema = $container->make(SchemaBuilder::class)->schema();
$printer = new Printer($settings, $resolver, $schema);
return $printer;
},
);
}
#[Override]
protected function getName(): string {
return Package::Name;
}
}