Skip to content

Commit f24a274

Browse files
Merge pull request #88 from laravel-doctrine/hotfix/boot-extensions
Hotfix/boot extensions
2 parents 2af501b + 14296da commit f24a274

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

src/DoctrineServiceProvider.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\ORM\Mapping\ClassMetadataFactory;
99
use Faker\Factory as FakerFactory;
1010
use Faker\Generator as FakerGenerator;
11+
use Illuminate\Contracts\Http\Kernel as HttpKernel;
1112
use Illuminate\Support\ServiceProvider;
1213
use InvalidArgumentException;
1314
use LaravelDoctrine\ORM\Auth\DoctrineUserProvider;
@@ -28,6 +29,7 @@
2829
use LaravelDoctrine\ORM\Console\SchemaValidateCommand;
2930
use LaravelDoctrine\ORM\Exceptions\ExtensionNotFound;
3031
use LaravelDoctrine\ORM\Extensions\ExtensionManager;
32+
use LaravelDoctrine\ORM\Http\Middleware\BootExtensions;
3133
use LaravelDoctrine\ORM\Testing\Factory as EntityFactory;
3234
use 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
*
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace LaravelDoctrine\ORM\Http\Middleware;
4+
5+
use Closure;
6+
use LaravelDoctrine\ORM\Extensions\ExtensionManager;
7+
8+
class BootExtensions
9+
{
10+
/**
11+
* @var ExtensionManager
12+
*/
13+
protected $manager;
14+
15+
/**
16+
* @param ExtensionManager $manager
17+
*/
18+
public function __construct(ExtensionManager $manager)
19+
{
20+
$this->manager = $manager;
21+
}
22+
23+
/**
24+
* @param \Illuminate\Http\Request $request
25+
* @param Closure $next
26+
*
27+
* @return mixed
28+
*/
29+
public function handle($request, Closure $next)
30+
{
31+
$this->manager->boot();
32+
33+
return $next($request);
34+
}
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use LaravelDoctrine\ORM\Http\Middleware\BootExtensions;
4+
use Mockery as m;
5+
6+
class BootExtensionsTest extends PHPUnit_Framework_TestCase
7+
{
8+
public function tearDown()
9+
{
10+
m::close();
11+
}
12+
13+
public function testHandle()
14+
{
15+
$kernelMock = m::mock(LaravelDoctrine\ORM\Extensions\ExtensionManager::class)
16+
->shouldReceive('boot')
17+
->once()
18+
->getMock();
19+
20+
$requestMock = m::mock(Illuminate\Http\Request::class);
21+
22+
$called = false;
23+
24+
$nextMock = function () use (&$called) {
25+
$called = true;
26+
};
27+
28+
/** @noinspection PhpParamsInspection */
29+
$middleware = new BootExtensions($kernelMock);
30+
31+
/** @noinspection PhpParamsInspection */
32+
$middleware->handle($requestMock, $nextMock);
33+
34+
$this->assertTrue($called);
35+
}
36+
}

0 commit comments

Comments
 (0)