Issues (9)

src/ServiceProvider.php (1 issue)

1
<?php
2
3
namespace DeInternetJongens\LighthouseUtils;
4
5
use DeInternetJongens\LighthouseUtils\Console\GenerateSchemaCommand;
6
use ReflectionClass;
7
8
class ServiceProvider extends \Illuminate\Support\ServiceProvider
9
{
10
    const CONFIG_PATH = __DIR__ . '/../config/lighthouse-utils.php';
11
    const MIGRATION_PATH = __DIR__ . '/../database/migrations/create_graphql_schema_table.php.stub';
12
    const DIRECTIVE_PATH = __DIR__.'/Directives';
13
    const DIRECTIVE_NAMESPACE = 'DeInternetJongens\\LighthouseUtils\\Directives';
14
15
16
    /** @var string */
17
    private $directiveAppPath;
18
19
    public function __construct(\Illuminate\Contracts\Foundation\Application $app)
20
    {
21
        parent::__construct($app);
22
23
        // The path where our directives will be published to.
24
        $this->directiveAppPath = app_path('DijLighthouse/Directives');
25
    }
26
27
    public function boot()
28
    {
29
        $this->publishes([
30
            self::CONFIG_PATH => config_path('lighthouse-utils.php'),
31
        ], 'config');
32
33
        if (! class_exists('CreatePermissionTables')) {
34
            $timestamp = date('Y_m_d_His', time());
35
            $this->publishes([
36
                self::MIGRATION_PATH => $this->app->databasePath()."/migrations/{$timestamp}_create_graphql_schema_table.php",
37
            ], 'migrations');
38
        }
39
40
        $this->registerDirectives();
41
    }
42
43
    public function register()
44
    {
45
46
        $this->mergeConfigFrom(
47
            self::CONFIG_PATH,
48
            'lighthouse-utils'
49
        );
50
51
        // Merging config doesn't seem to work on arrays, this is our work-around.
52
        config()->set('lighthouse.directives', array_merge(config('lighthouse.directives', []), [$this->directiveAppPath]));
53
54
        $this->app->bind('lighthouse-utils', function () {
55
            return new LighthouseUtils();
56
        });
57
58
        if ($this->app->runningInConsole()) {
59
            $this->commands([
60
                GenerateSchemaCommand::class
61
            ]);
62
        }
63
    }
64
65
    /**
66
     * @return void
67
     * @throws \ReflectionException
68
     */
69
    private function registerDirectives(): void
70
    {
71
        foreach (glob(self::DIRECTIVE_PATH . \DIRECTORY_SEPARATOR . '*.php') as $directiveFile) {
72
            // some/path/foo.bar -> some/path/foo
73
            $pathParts = explode('.', $directiveFile);
74
            $classNameWithPath = $pathParts[count($pathParts) - 2];
75
76
            // some/path/foo -> foo
77
            $classNameWithPathParts = explode(\DIRECTORY_SEPARATOR, $classNameWithPath);
78
            $className = end($classNameWithPathParts);
79
80
            // foo -> some\namespace\foo
81
            $namespace = self::DIRECTIVE_NAMESPACE;
82
            $class = $namespace . '\\' . $className;
83
84
            $reflectionClass = new ReflectionClass($class);
85
86
            // Things like abstract classes aren't instantiable, we don't register them.
87
            if ($reflectionClass->isInstantiable()) {
88
                \graphql()->directives()->register($reflectionClass->newInstance());
0 ignored issues
show
Deprecated Code introduced by
The function Nuwave\Lighthouse\GraphQL::directives() has been deprecated: Use resolve() instead, will be removed in v3 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

88
                /** @scrutinizer ignore-deprecated */ \graphql()->directives()->register($reflectionClass->newInstance());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
89
            }
90
        }
91
    }
92
}
93