Skip to content

Commit ea96a38

Browse files
authored
Fix redirect session (barryvdh#1657)
* Fix redirect session * Check if enabled * Test session redirect * Test database * Remove flag * TWeak wait
1 parent 2351fc0 commit ea96a38

File tree

4 files changed

+86
-5
lines changed

4 files changed

+86
-5
lines changed

phpunit.xml.dist

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<php>
2929
<env name="DEBUGBAR_ENABLED" value="true"/>
3030
<env name="DB_CONNECTION" value="testing"/>
31+
<env name="DB_CONNECTION" value="testing"/>
3132
<ini name="xdebug.file_link_format" value="vscode://file/%f:%l"/>
3233
</php>
3334
</phpunit>

src/LaravelDebugbar.php

+29-3
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ class LaravelDebugbar extends DebugBar
100100

101101
protected ?string $editorTemplateLink = null;
102102
protected array $remoteServerReplacements = [];
103-
104-
103+
protected bool $responseIsModified = false;
104+
protected array $stackedData = [];
105105
/**
106106
* @param Application $app
107107
*/
@@ -711,10 +711,13 @@ public function modifyResponse(Request $request, Response $response)
711711
{
712712
/** @var Application $app */
713713
$app = $this->app;
714-
if (!$this->isEnabled() || $this->isDebugbarRequest()) {
714+
if (!$this->isEnabled() || $this->isDebugbarRequest() || $this->responseIsModified) {
715715
return $response;
716716
}
717717

718+
// Prevent duplicate modification
719+
$this->responseIsModified = true;
720+
718721
// Show the Http Response Exception in the Debugbar, when available
719722
if (isset($response->exception)) {
720723
$this->addThrowable($response->exception);
@@ -984,6 +987,29 @@ public function injectDebugbar(Response $response)
984987
}
985988
}
986989

990+
/**
991+
* Checks if there is stacked data in the session
992+
*
993+
* @return boolean
994+
*/
995+
public function hasStackedData()
996+
{
997+
return count($this->getStackedData(false)) > 0;
998+
}
999+
1000+
/**
1001+
* Returns the data stacked in the session
1002+
*
1003+
* @param boolean $delete Whether to delete the data in the session
1004+
* @return array
1005+
*/
1006+
public function getStackedData($delete = true): array
1007+
{
1008+
$this->stackedData = array_merge($this->stackedData, parent::getStackedData($delete));
1009+
1010+
return $this->stackedData;
1011+
}
1012+
9871013
/**
9881014
* Disable the Debugbar
9891015
*/

src/ServiceProvider.php

+30-2
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
namespace Barryvdh\Debugbar;
44

5-
use Barryvdh\Debugbar\Middleware\DebugbarEnabled;
65
use Barryvdh\Debugbar\Middleware\InjectDebugbar;
76
use DebugBar\DataFormatter\DataFormatter;
87
use DebugBar\DataFormatter\DataFormatterInterface;
98
use Illuminate\Container\Container;
109
use Illuminate\Contracts\View\Factory;
1110
use Illuminate\Contracts\Http\Kernel;
11+
use Illuminate\Routing\Events\ResponsePrepared;
1212
use Illuminate\Routing\Router;
1313
use Illuminate\Session\SessionManager;
1414
use Illuminate\Support\Collection;
15-
use Barryvdh\Debugbar\Facade as DebugBar;
1615

1716
class ServiceProvider extends \Illuminate\Support\ServiceProvider
1817
{
@@ -106,6 +105,7 @@ public function boot()
106105

107106
$this->loadRoutesFrom(__DIR__ . '/debugbar-routes.php');
108107

108+
$this->registerResponseListener();
109109
$this->registerMiddleware(InjectDebugbar::class);
110110

111111
$this->commands(['command.debugbar.clear']);
@@ -151,4 +151,32 @@ protected function registerMiddleware($middleware)
151151
$kernel = $this->app[Kernel::class];
152152
$kernel->pushMiddleware($middleware);
153153
}
154+
155+
/**
156+
* Register the Response Listener
157+
*
158+
* @param string $middleware
159+
*/
160+
protected function registerResponseListener()
161+
{
162+
if (!isset($this->app['events']) || !class_exists(ResponsePrepared::class)) {
163+
return;
164+
}
165+
166+
/**
167+
* For redirects, prepare the response early to store in the session.
168+
* For regular requests, get the stacked data early
169+
*/
170+
$this->app['events']->listen(ResponsePrepared::class, function (ResponsePrepared $event) {
171+
/** @var LaravelDebugbar $debugbar */
172+
$debugbar = $this->app->make(LaravelDebugbar::class);
173+
if ($debugbar->isEnabled()) {
174+
if ($event->response->isRedirection()) {
175+
$debugbar->modifyResponse($event->request, $event->response);
176+
} else {
177+
$debugbar->getStackedData();
178+
}
179+
}
180+
});
181+
}
154182
}

tests/DebugbarBrowserTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Barryvdh\Debugbar\Tests;
44

55
use Illuminate\Routing\Router;
6+
use Laravel\Dusk\Browser;
67

78
class DebugbarBrowserTest extends BrowserTestCase
89
{
10+
911
/**
1012
* Define environment setup.
1113
*
@@ -15,6 +17,8 @@ class DebugbarBrowserTest extends BrowserTestCase
1517
*/
1618
protected function getEnvironmentSetUp($app)
1719
{
20+
parent::getEnvironmentSetUp($app);
21+
1822
$app['env'] = 'local';
1923

2024
// $app['config']->set('app.debug', true);
@@ -25,6 +29,9 @@ protected function getEnvironmentSetUp($app)
2529
$this->addWebRoutes($router);
2630
$this->addApiRoutes($router);
2731

32+
$kernel = app('Illuminate\Contracts\Http\Kernel');
33+
$kernel->pushMiddleware('Illuminate\Session\Middleware\StartSession');
34+
2835
\Orchestra\Testbench\Dusk\Options::withoutUI();
2936
}
3037

@@ -33,6 +40,12 @@ protected function getEnvironmentSetUp($app)
3340
*/
3441
protected function addWebRoutes(Router $router)
3542
{
43+
$router->get('web/redirect', [
44+
'uses' => function () {
45+
return redirect($this->applicationBaseUrl() . '/web/plain');
46+
}
47+
]);
48+
3649
$router->get('web/plain', [
3750
'uses' => function () {
3851
return 'PONG';
@@ -58,6 +71,19 @@ protected function addApiRoutes(Router $router)
5871
]);
5972
}
6073

74+
public function testItStacksOnRedirect()
75+
{
76+
$this->browse(function (Browser $browser) {
77+
$browser->visit('web/redirect')
78+
->assertSee('PONG')
79+
->waitFor('.phpdebugbar')
80+
->assertSee('GET web/plain')
81+
->click('.phpdebugbar-tab-history')
82+
->waitForTextIn('.phpdebugbar-widgets-dataset-history', 'web/redirect (stacked)')
83+
->assertSee('web/redirect');
84+
});
85+
}
86+
6187
public function testItInjectsOnPlainText()
6288
{
6389
$this->browse(function ($browser) {

0 commit comments

Comments
 (0)