From 7a7077a1ca0c365573a240ca259d77889547c165 Mon Sep 17 00:00:00 2001 From: Max Brokman Date: Tue, 3 Nov 2015 11:00:19 +0000 Subject: [PATCH 1/2] Changed behaviour of extension booting so that the extension manager is booted immediately when in CLI or is booted from a middleware for HTTP requests. --- src/DoctrineServiceProvider.php | 32 +++++++++++++++-- .../Middleware/BootExtensionsMiddleware.php | 32 +++++++++++++++++ .../BootExtensionsMiddlewareTest.php | 36 +++++++++++++++++++ 3 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 src/Http/Middleware/BootExtensionsMiddleware.php create mode 100644 tests/Http/Middleware/BootExtensionsMiddlewareTest.php diff --git a/src/DoctrineServiceProvider.php b/src/DoctrineServiceProvider.php index e13599df..914a7999 100644 --- a/src/DoctrineServiceProvider.php +++ b/src/DoctrineServiceProvider.php @@ -8,6 +8,7 @@ use Doctrine\ORM\Mapping\ClassMetadataFactory; use Faker\Factory as FakerFactory; use Faker\Generator as FakerGenerator; +use Illuminate\Contracts\Http\Kernel as HttpKernel; use Illuminate\Support\ServiceProvider; use InvalidArgumentException; use LaravelDoctrine\ORM\Auth\DoctrineUserProvider; @@ -28,6 +29,7 @@ use LaravelDoctrine\ORM\Console\SchemaValidateCommand; use LaravelDoctrine\ORM\Exceptions\ExtensionNotFound; use LaravelDoctrine\ORM\Extensions\ExtensionManager; +use LaravelDoctrine\ORM\Http\Middleware\BootExtensionsMiddleware; use LaravelDoctrine\ORM\Testing\Factory as EntityFactory; use LaravelDoctrine\ORM\Validation\DoctrinePresenceVerifier; @@ -40,9 +42,7 @@ public function boot() { $this->extendAuthManager(); - $this->app['events']->listen('router.matched', function () { - $this->app->make(ExtensionManager::class)->boot(); - }); + $this->bootExtensionManager(); if (!$this->isLumen()) { $this->publishes([ @@ -226,6 +226,32 @@ protected function extendAuthManager() }); } + /** + * Boots the extension manager at the appropriate time depending on if the app + * is running as Laravel HTTP, Lumen HTTP or in a console environment + */ + protected function bootExtensionManager() + { + // If running in console we can boot immediately + if (php_sapi_name() === 'cli') { + $this->app->make(ExtensionManager::class)->boot(); + + return; + } + + // If running a HTTP Request in Laravel we want to push in middleware so we + // boot after the session start. Some extensions make use of the session + // to find out who the currently authenticated user is, e.g Loggable + if (!$this->isLumen()) { + $this->app->make(HttpKernel::class)->pushMiddleware(BootExtensionsMiddleware::class); + + return; + } + + // Add the BootExtensionMiddleware to the end of the Lumen middleware stack + $this->app->middleware([BootExtensionsMiddleware::class]); + } + /** * Register the Entity factory instance in the container. * diff --git a/src/Http/Middleware/BootExtensionsMiddleware.php b/src/Http/Middleware/BootExtensionsMiddleware.php new file mode 100644 index 00000000..13743ca7 --- /dev/null +++ b/src/Http/Middleware/BootExtensionsMiddleware.php @@ -0,0 +1,32 @@ +extensionManager = $extensionManager; + } + + /** + * @param $request + * @param Closure $next + * @return mixed + */ + public function handle(Request $request, Closure $next) + { + $this->extensionManager->boot(); + + return $next($request); + } +} diff --git a/tests/Http/Middleware/BootExtensionsMiddlewareTest.php b/tests/Http/Middleware/BootExtensionsMiddlewareTest.php new file mode 100644 index 00000000..ef1b95d5 --- /dev/null +++ b/tests/Http/Middleware/BootExtensionsMiddlewareTest.php @@ -0,0 +1,36 @@ +shouldReceive('boot') + ->once() + ->getMock(); + + $requestMock = m::mock(Illuminate\Http\Request::class); + + $called = false; + + $nextMock = function () use (&$called) { + $called = true; + }; + + /** @noinspection PhpParamsInspection */ + $middleware = new BootExtensionsMiddleware($kernelMock); + + /** @noinspection PhpParamsInspection */ + $middleware->handle($requestMock, $nextMock); + + $this->assertTrue($called); + } +} From 14296da8e1b393dd7b1353c5db0a416e3eae0c44 Mon Sep 17 00:00:00 2001 From: Patrick Brouwers Date: Tue, 3 Nov 2015 17:55:32 +0100 Subject: [PATCH 2/2] Enhancement extensions booting process --- src/DoctrineServiceProvider.php | 8 ++--- src/Http/Middleware/BootExtensions.php | 35 +++++++++++++++++++ .../Middleware/BootExtensionsMiddleware.php | 32 ----------------- ...dlewareTest.php => BootExtensionsTest.php} | 6 ++-- 4 files changed, 42 insertions(+), 39 deletions(-) create mode 100644 src/Http/Middleware/BootExtensions.php delete mode 100644 src/Http/Middleware/BootExtensionsMiddleware.php rename tests/Http/Middleware/{BootExtensionsMiddlewareTest.php => BootExtensionsTest.php} (77%) diff --git a/src/DoctrineServiceProvider.php b/src/DoctrineServiceProvider.php index 914a7999..290b1a53 100644 --- a/src/DoctrineServiceProvider.php +++ b/src/DoctrineServiceProvider.php @@ -29,7 +29,7 @@ use LaravelDoctrine\ORM\Console\SchemaValidateCommand; use LaravelDoctrine\ORM\Exceptions\ExtensionNotFound; use LaravelDoctrine\ORM\Extensions\ExtensionManager; -use LaravelDoctrine\ORM\Http\Middleware\BootExtensionsMiddleware; +use LaravelDoctrine\ORM\Http\Middleware\BootExtensions; use LaravelDoctrine\ORM\Testing\Factory as EntityFactory; use LaravelDoctrine\ORM\Validation\DoctrinePresenceVerifier; @@ -243,13 +243,13 @@ protected function bootExtensionManager() // boot after the session start. Some extensions make use of the session // to find out who the currently authenticated user is, e.g Loggable if (!$this->isLumen()) { - $this->app->make(HttpKernel::class)->pushMiddleware(BootExtensionsMiddleware::class); + $this->app->make(HttpKernel::class)->pushMiddleware(BootExtensions::class); return; } - // Add the BootExtensionMiddleware to the end of the Lumen middleware stack - $this->app->middleware([BootExtensionsMiddleware::class]); + // Add BootExtension to the end of the Lumen middleware stack + $this->app->middleware([BootExtensions::class]); } /** diff --git a/src/Http/Middleware/BootExtensions.php b/src/Http/Middleware/BootExtensions.php new file mode 100644 index 00000000..406936d5 --- /dev/null +++ b/src/Http/Middleware/BootExtensions.php @@ -0,0 +1,35 @@ +manager = $manager; + } + + /** + * @param \Illuminate\Http\Request $request + * @param Closure $next + * + * @return mixed + */ + public function handle($request, Closure $next) + { + $this->manager->boot(); + + return $next($request); + } +} diff --git a/src/Http/Middleware/BootExtensionsMiddleware.php b/src/Http/Middleware/BootExtensionsMiddleware.php deleted file mode 100644 index 13743ca7..00000000 --- a/src/Http/Middleware/BootExtensionsMiddleware.php +++ /dev/null @@ -1,32 +0,0 @@ -extensionManager = $extensionManager; - } - - /** - * @param $request - * @param Closure $next - * @return mixed - */ - public function handle(Request $request, Closure $next) - { - $this->extensionManager->boot(); - - return $next($request); - } -} diff --git a/tests/Http/Middleware/BootExtensionsMiddlewareTest.php b/tests/Http/Middleware/BootExtensionsTest.php similarity index 77% rename from tests/Http/Middleware/BootExtensionsMiddlewareTest.php rename to tests/Http/Middleware/BootExtensionsTest.php index ef1b95d5..1c0a1bb1 100644 --- a/tests/Http/Middleware/BootExtensionsMiddlewareTest.php +++ b/tests/Http/Middleware/BootExtensionsTest.php @@ -1,9 +1,9 @@ handle($requestMock, $nextMock);