1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace LaravelUi5\OData;
6:
7: use Illuminate\Support\Facades\Route;
8: use Illuminate\Support\ServiceProvider;
9: use LaravelUi5\OData\Console\CacheCommand;
10: use LaravelUi5\OData\Console\ClearCommand;
11: use LaravelUi5\OData\Service\Contracts\ODataServiceRegistryInterface;
12: use LaravelUi5\OData\Service\Contracts\ReadAuthorizerInterface;
13: use LaravelUi5\OData\Service\AllowAllReadAuthorizer;
14:
15: class ODataServiceProvider extends ServiceProvider
16: {
17: public function register(): void
18: {
19: $this->mergeConfigFrom(__DIR__ . '/../config.php', 'odata');
20:
21: $this->app->singleton(ODataServiceRegistryInterface::class, fn ($app) => $app->make(
22: config('odata.service_registry', ODataServiceRegistry::class)
23: ),
24: );
25:
26: // The read-authorization forward-exit. Default is the no-op AllowAll — OData stays
27: // security-agnostic. Hosts bind their own enforcer via the config key or by rebinding.
28: // bind (not singleton): the real enforcer is request-scoped (it authorizes the current
29: // actor), so a fresh resolution per request avoids a stale-actor trap under Octane.
30: $this->app->bind(
31: ReadAuthorizerInterface::class,
32: config('odata.read_authorizer', AllowAllReadAuthorizer::class),
33: );
34: }
35:
36: public function boot(): void
37: {
38: if ($this->app->runningInConsole()) {
39: $this->publishes([__DIR__ . '/../config.php' => config_path('odata.php')], 'config');
40: $this->commands([
41: CacheCommand::class,
42: ClearCommand::class,
43: ]);
44: }
45:
46: if (config('odata.register_routes', true)) {
47: Route::prefix(config('odata.prefix', 'odata'))
48: ->middleware(config('odata.middleware', []))
49: ->group(__DIR__ . '/../routes/odata.php');
50: }
51: }
52: }
53: