88use Doctrine \ORM \Mapping \ClassMetadataFactory ;
99use Faker \Factory as FakerFactory ;
1010use Faker \Generator as FakerGenerator ;
11+ use Illuminate \Contracts \Http \Kernel as HttpKernel ;
1112use Illuminate \Support \ServiceProvider ;
1213use InvalidArgumentException ;
1314use LaravelDoctrine \ORM \Auth \DoctrineUserProvider ;
2829use LaravelDoctrine \ORM \Console \SchemaValidateCommand ;
2930use LaravelDoctrine \ORM \Exceptions \ExtensionNotFound ;
3031use LaravelDoctrine \ORM \Extensions \ExtensionManager ;
32+ use LaravelDoctrine \ORM \Http \Middleware \BootExtensions ;
3133use LaravelDoctrine \ORM \Testing \Factory as EntityFactory ;
3234use LaravelDoctrine \ORM \Validation \DoctrinePresenceVerifier ;
3335
@@ -40,9 +42,7 @@ public function boot()
4042 {
4143 $ this ->extendAuthManager ();
4244
43- $ this ->app ['events ' ]->listen ('router.matched ' , function () {
44- $ this ->app ->make (ExtensionManager::class)->boot ();
45- });
45+ $ this ->bootExtensionManager ();
4646
4747 if (!$ this ->isLumen ()) {
4848 $ this ->publishes ([
@@ -226,6 +226,32 @@ protected function extendAuthManager()
226226 });
227227 }
228228
229+ /**
230+ * Boots the extension manager at the appropriate time depending on if the app
231+ * is running as Laravel HTTP, Lumen HTTP or in a console environment
232+ */
233+ protected function bootExtensionManager ()
234+ {
235+ // If running in console we can boot immediately
236+ if (php_sapi_name () === 'cli ' ) {
237+ $ this ->app ->make (ExtensionManager::class)->boot ();
238+
239+ return ;
240+ }
241+
242+ // If running a HTTP Request in Laravel we want to push in middleware so we
243+ // boot after the session start. Some extensions make use of the session
244+ // to find out who the currently authenticated user is, e.g Loggable
245+ if (!$ this ->isLumen ()) {
246+ $ this ->app ->make (HttpKernel::class)->pushMiddleware (BootExtensions::class);
247+
248+ return ;
249+ }
250+
251+ // Add BootExtension to the end of the Lumen middleware stack
252+ $ this ->app ->middleware ([BootExtensions::class]);
253+ }
254+
229255 /**
230256 * Register the Entity factory instance in the container.
231257 *
0 commit comments