21
21
22
22
/**
23
23
* Service Provider
24
+ *
25
+ * https://<server>:<port>/<prefix>/<service-uri>/$metadata
26
+ *
24
27
* @link https://laravel.com/docs/8.x/providers
25
28
* @package Flat3\Lodata
26
29
*/
27
30
class ServiceProvider extends \Illuminate \Support \ServiceProvider
28
31
{
29
- /**
30
- * Get the endpoint of the OData service document
31
- * @return string
32
- */
33
- public static function endpoint (): string
34
- {
35
- return url (self ::route ()).'/ ' ;
36
- }
37
-
38
- /**
39
- * Get the configured route prefix
40
- * @return string
41
- */
42
- public static function route (): string
43
- {
44
- return rtrim (config ('lodata.prefix ' ), '/ ' );
45
- }
46
-
47
32
/**
48
33
* Service provider registration method
49
34
*/
@@ -59,14 +44,61 @@ public function boot()
59
44
{
60
45
if ($ this ->app ->runningInConsole ()) {
61
46
$ this ->publishes ([__DIR__ .'/../config.php ' => config_path ('lodata.php ' )], 'config ' );
47
+ $ this ->bootServices (new Endpoint ('' ));
62
48
}
49
+ else {
50
+ // Let’s examine the request path
51
+ $ segments = explode ('/ ' , request ()->path ());
52
+
53
+ // we only kick off operation when path prefix is configured in lodata.php
54
+ // as all requests share the same root configuration
55
+ if ($ segments [0 ] === config ('lodata.prefix ' )) {
56
+
57
+ // next look up the configured service endpoints
58
+ $ serviceUris = config ('lodata.endpoints ' , []);
59
+
60
+ $ service = null ;
61
+ if (0 === sizeof ($ serviceUris )) {
62
+ // when no locators are defined, fallback to global mode; this will
63
+ // ensure compatibility with prior versions of this package
64
+ $ service = new Endpoint ('' );
65
+ }
66
+ else if (array_key_exists ($ segments [1 ], $ serviceUris )) {
67
+ $ clazz = $ serviceUris [$ segments [1 ]];
68
+ $ service = new $ clazz ($ segments [1 ]);
69
+ }
70
+ else {
71
+ // when no service definition is configured for the path segment,
72
+ // we abort with an error condition; typically a dev working on
73
+ // setting up his project
74
+ abort ('No odata service endpoint defined for path ' . $ segments [1 ]);
75
+ }
76
+
77
+ $ this ->bootServices ($ service );
78
+ }
79
+ }
80
+ }
81
+
82
+ private function bootServices ($ service ): void
83
+ {
84
+ // register the $service, which is a singleton, with the container; this allows us
85
+ // to fulfill all old ServiceProvider::route() and ServiceProvider::endpoint()
86
+ // calls with app()->make(ODataService::class)->route() or
87
+ // app()->make(ODataService::class)->endpoint()
88
+ $ this ->app ->instance (Endpoint::class, $ service );
63
89
64
90
$ this ->loadJsonTranslationsFrom (__DIR__ .'/../lang ' );
65
91
66
- $ this ->app ->singleton (Model::class, function () {
67
- return new Model ();
68
- });
92
+ // next instantiate and discover the global Model
93
+ $ model = $ service ->discover (new Model ());
94
+ assert ($ model instanceof Model);
95
+
96
+ // and register it with the container
97
+ $ this ->app ->instance (Model::class, $ model );
69
98
99
+ // I don't get why you are doing this twice? You never load the model
100
+ // via app()->make('lodata.model') or app()->make(Model::class). What
101
+ // am I missing here?
70
102
$ this ->app ->bind ('lodata.model ' , function ($ app ) {
71
103
return $ app ->make (Model::class);
72
104
});
@@ -83,7 +115,7 @@ public function boot()
83
115
return version_compare (InstalledVersions::getVersion ('doctrine/dbal ' ), '4.0.0 ' , '>= ' ) ? new DBAL \DBAL4 ($ args ['connection ' ]) : new DBAL \DBAL3 ($ args ['connection ' ]);
84
116
});
85
117
86
- $ route = self :: route ();
118
+ $ route = $ service -> route ();
87
119
$ middleware = config ('lodata.middleware ' , []);
88
120
89
121
Route::get ("{$ route }/_lodata/odata.pbids " , [PBIDS ::class, 'get ' ]);
0 commit comments