Skip to content

Commit ee14a8c

Browse files
committed
Add affordance to restore queries while preparing response
1 parent 67c4653 commit ee14a8c

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

src/Illuminate/Database/Connection.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,6 @@ public function allowQueries($callback = null)
850850
}
851851
}
852852

853-
854853
/**
855854
* Register a callback to be invoked when the connection queries for longer than a given amount of time.
856855
*

src/Illuminate/Foundation/Application.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,8 @@ public function getNamespace()
14251425
if (realpath($this->path()) === realpath($this->basePath($pathChoice))) {
14261426
return $this->namespace = $namespace;
14271427
}
1428-
} }
1428+
}
1429+
}
14291430

14301431
throw new RuntimeException('Unable to detect application namespace.');
14311432
}
@@ -1444,4 +1445,17 @@ public function preventQueriesWhilePreparingResponse($connection = null)
14441445

14451446
return $this;
14461447
}
1448+
1449+
/**
1450+
* Restores ability for database to query while preparing the request response.
1451+
*
1452+
* @param string|null $connection
1453+
* @return $this
1454+
*/
1455+
public function allowQueriesWhilePreparingResponse($connection = null)
1456+
{
1457+
$this['events']->listen(PreparingResponse::class, fn () => $this['db']->connection($connection)->allowQueries());
1458+
1459+
return $this;
1460+
}
14471461
}

tests/Database/DatabaseConnectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ public function testItCanBeNested()
516516
return $isActive().$connection->allowQueries(function () use ($connection, $isActive) {
517517
return $isActive().$connection->preventQueries(function () use ($connection, $isActive) {
518518
return $isActive().$connection->preventQueries(function () use ($connection, $isActive) {
519-
return $isActive().$connection->allowQueries(function () use ($connection, $isActive) {
519+
return $isActive().$connection->allowQueries(function () use ($isActive) {
520520
return $isActive();
521521
}).$isActive();
522522
}).$isActive();

tests/Integration/Foundation/ApplicationTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Illuminate\Tests\Integration\Foundation;
44

5-
use Illuminate\Contracts\Http\Kernel;
65
use Illuminate\Http\Resources\Json\JsonResource;
76
use Illuminate\Support\Facades\DB;
87
use Illuminate\Support\Facades\Route;
@@ -52,7 +51,8 @@ public function testItCanThrowOnJsonResourceResolution()
5251
Route::get('test-route', function () {
5352
DB::table('foo')->get();
5453

55-
return new class('xxxx') extends JsonResource {
54+
return new class('xxxx') extends JsonResource
55+
{
5656
public function toArray($request)
5757
{
5858
return [
@@ -80,7 +80,8 @@ public function testItCanRunQueriesAfterResponseResolved()
8080
$table->id();
8181
});
8282
Route::get('test-route', function () {
83-
return new class('xxxx') extends JsonResource {
83+
return new class('xxxx') extends JsonResource
84+
{
8485
public function toArray($request)
8586
{
8687
return [
@@ -97,6 +98,32 @@ public function toArray($request)
9798

9899
$this->assertCount(1, DB::getQueryLog());
99100
}
101+
102+
public function testItcanRestoreValue()
103+
{
104+
Schema::create('foo', function ($table) {
105+
$table->id();
106+
});
107+
Route::get('test-route', function () {
108+
return new class('xxxx') extends JsonResource
109+
{
110+
public function toArray($request)
111+
{
112+
return [
113+
//
114+
];
115+
}
116+
};
117+
})->middleware(TestMiddleware::class);
118+
DB::enableQueryLog();
119+
$this->withoutExceptionHandling();
120+
121+
$this->app->preventQueriesWhilePreparingResponse();
122+
$this->app->allowQueriesWhilePreparingResponse();
123+
$this->get('test-route')->assertOk();
124+
125+
$this->assertCount(1, DB::getQueryLog());
126+
}
100127
}
101128

102129
class TestMiddleware

0 commit comments

Comments
 (0)