Skip to content

feat: add config option to change response key #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/api-debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
// Memory usage.
\Lanin\Laravel\ApiDebugger\Collections\MemoryCollection::class,
],

'response_key' => env('API_DEBUGGER_KEY', 'debug')
];
17 changes: 16 additions & 1 deletion src/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ class Debugger
/**
* @var string
*/
protected $responseKey = 'debug';
const DEFAULT_RESPONSE_KEY = 'debug';

/**
* @var string
*/
protected $responseKey = self::DEFAULT_RESPONSE_KEY;

/**
* @var Storage
Expand Down Expand Up @@ -172,6 +177,16 @@ protected function setResponseData(Response $response, $data)
return $response->setContent($content);
}

/**
* Get the current response key
*
* @return string
*/
public function getResponseKey()
{
return $this->responseKey;
}

/**
* Set response attribute key name.
*
Expand Down
17 changes: 17 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function boot()
$config = $this->app['config'];
if ($config['api-debugger.enabled']) {
$this->registerCollections($config['api-debugger.collections']);

$this->setResponseKey($config['api-debugger.response_key']);
}
}

Expand Down Expand Up @@ -62,4 +64,19 @@ protected function registerCollections(array $collections)
$debugger->populateWith($this->app->make($collection));
}
}

/**
* Set the response key for the debug object
* (default: debug)
*
* @param string key
*/
protected function setResponseKey($key)
{
$debugger = $this->app->make(Debugger::class);

if($key && $key !== Debugger::DEFAULT_RESPONSE_KEY){
$debugger->setResponseKey($key);
}
}
}
16 changes: 16 additions & 0 deletions tests/DebuggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Cache;
use Lanin\Laravel\ApiDebugger\Debugger;

class DebuggerTest extends TestCase
{
Expand Down Expand Up @@ -229,4 +230,19 @@ public function it_preserves_array()
->assertStatus(200)
->assertSeeText('"baz":[]');
}

/** @test */
public function it_can_set_a_new_response_key()
{
/** @var Debugger $debugger */
$debugger = $this->app->make(Debugger::class);

$this->assertEquals(Debugger::DEFAULT_RESPONSE_KEY, $debugger->getResponseKey(), 'Response key is not the default');

$new_response_key = 'key123';

$debugger->setResponseKey($new_response_key);

$this->assertEquals($new_response_key, $debugger->getResponseKey(), 'Response key was not changed from "'.$debugger->getResponseKey().'" to "'.$new_response_key.'"');
}
}