Skip to content

Commit 2474809

Browse files
authored
Merge pull request #20 from mycarrysun/response-key
feat: add config option to change response key
2 parents ccc7480 + 0cbfc52 commit 2474809

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

config/api-debugger.php

+2
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@
1818
// Memory usage.
1919
\Lanin\Laravel\ApiDebugger\Collections\MemoryCollection::class,
2020
],
21+
22+
'response_key' => env('API_DEBUGGER_KEY', 'debug')
2123
];

src/Debugger.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ class Debugger
1616
/**
1717
* @var string
1818
*/
19-
protected $responseKey = 'debug';
19+
const DEFAULT_RESPONSE_KEY = 'debug';
20+
21+
/**
22+
* @var string
23+
*/
24+
protected $responseKey = self::DEFAULT_RESPONSE_KEY;
2025

2126
/**
2227
* @var Storage
@@ -172,6 +177,16 @@ protected function setResponseData(Response $response, $data)
172177
return $response->setContent($content);
173178
}
174179

180+
/**
181+
* Get the current response key
182+
*
183+
* @return string
184+
*/
185+
public function getResponseKey()
186+
{
187+
return $this->responseKey;
188+
}
189+
175190
/**
176191
* Set response attribute key name.
177192
*

src/ServiceProvider.php

+17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public function boot()
2424
$config = $this->app['config'];
2525
if ($config['api-debugger.enabled']) {
2626
$this->registerCollections($config['api-debugger.collections']);
27+
28+
$this->setResponseKey($config['api-debugger.response_key']);
2729
}
2830
}
2931

@@ -62,4 +64,19 @@ protected function registerCollections(array $collections)
6264
$debugger->populateWith($this->app->make($collection));
6365
}
6466
}
67+
68+
/**
69+
* Set the response key for the debug object
70+
* (default: debug)
71+
*
72+
* @param string key
73+
*/
74+
protected function setResponseKey($key)
75+
{
76+
$debugger = $this->app->make(Debugger::class);
77+
78+
if($key && $key !== Debugger::DEFAULT_RESPONSE_KEY){
79+
$debugger->setResponseKey($key);
80+
}
81+
}
6582
}

tests/DebuggerTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Schema\Blueprint;
66
use Illuminate\Support\Facades\Cache;
7+
use Lanin\Laravel\ApiDebugger\Debugger;
78

89
class DebuggerTest extends TestCase
910
{
@@ -229,4 +230,19 @@ public function it_preserves_array()
229230
->assertStatus(200)
230231
->assertSeeText('"baz":[]');
231232
}
233+
234+
/** @test */
235+
public function it_can_set_a_new_response_key()
236+
{
237+
/** @var Debugger $debugger */
238+
$debugger = $this->app->make(Debugger::class);
239+
240+
$this->assertEquals(Debugger::DEFAULT_RESPONSE_KEY, $debugger->getResponseKey(), 'Response key is not the default');
241+
242+
$new_response_key = 'key123';
243+
244+
$debugger->setResponseKey($new_response_key);
245+
246+
$this->assertEquals($new_response_key, $debugger->getResponseKey(), 'Response key was not changed from "'.$debugger->getResponseKey().'" to "'.$new_response_key.'"');
247+
}
232248
}

0 commit comments

Comments
 (0)