Skip to content

Commit

Permalink
Merge pull request #88 from laravel-doctrine/hotfix/boot-extensions
Browse files Browse the repository at this point in the history
Hotfix/boot extensions
  • Loading branch information
patrickbrouwers committed Nov 3, 2015
2 parents 2af501b + 14296da commit f24a274
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/DoctrineServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +29,7 @@
use LaravelDoctrine\ORM\Console\SchemaValidateCommand;
use LaravelDoctrine\ORM\Exceptions\ExtensionNotFound;
use LaravelDoctrine\ORM\Extensions\ExtensionManager;
use LaravelDoctrine\ORM\Http\Middleware\BootExtensions;
use LaravelDoctrine\ORM\Testing\Factory as EntityFactory;
use LaravelDoctrine\ORM\Validation\DoctrinePresenceVerifier;

Expand All @@ -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([
Expand Down Expand Up @@ -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(BootExtensions::class);

return;
}

// Add BootExtension to the end of the Lumen middleware stack
$this->app->middleware([BootExtensions::class]);
}

/**
* Register the Entity factory instance in the container.
*
Expand Down
35 changes: 35 additions & 0 deletions src/Http/Middleware/BootExtensions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace LaravelDoctrine\ORM\Http\Middleware;

use Closure;
use LaravelDoctrine\ORM\Extensions\ExtensionManager;

class BootExtensions
{
/**
* @var ExtensionManager
*/
protected $manager;

/**
* @param ExtensionManager $manager
*/
public function __construct(ExtensionManager $manager)
{
$this->manager = $manager;
}

/**
* @param \Illuminate\Http\Request $request
* @param Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
$this->manager->boot();

return $next($request);
}
}
36 changes: 36 additions & 0 deletions tests/Http/Middleware/BootExtensionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use LaravelDoctrine\ORM\Http\Middleware\BootExtensions;
use Mockery as m;

class BootExtensionsTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
m::close();
}

public function testHandle()
{
$kernelMock = m::mock(LaravelDoctrine\ORM\Extensions\ExtensionManager::class)
->shouldReceive('boot')
->once()
->getMock();

$requestMock = m::mock(Illuminate\Http\Request::class);

$called = false;

$nextMock = function () use (&$called) {
$called = true;
};

/** @noinspection PhpParamsInspection */
$middleware = new BootExtensions($kernelMock);

/** @noinspection PhpParamsInspection */
$middleware->handle($requestMock, $nextMock);

$this->assertTrue($called);
}
}

0 comments on commit f24a274

Please sign in to comment.