Skip to content

Commit b523b8b

Browse files
authored
Merge pull request #17 from facade/add-version-support
Add version support
2 parents ef0f5bc + a0e4f73 commit b523b8b

7 files changed

+82
-4
lines changed

src/Flare.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,33 @@ class Flare
4747
/** @var callable|null */
4848
private $previousErrorHandler;
4949

50+
/** @var callable|null */
51+
private $determineVersionCallable;
52+
5053
public static function register(string $apiKey, string $apiSecret = null, ContextDetectorInterface $contextDetector = null, Container $container = null)
5154
{
5255
$client = new Client($apiKey, $apiSecret);
5356

5457
return new static($client, $contextDetector, $container);
5558
}
5659

60+
public function determineVersionUsing($determineVersionCallable)
61+
{
62+
$this->determineVersionCallable = $determineVersionCallable;
63+
}
64+
65+
/**
66+
* @return null|string
67+
*/
68+
public function version()
69+
{
70+
if (! $this->determineVersionCallable) {
71+
return null;
72+
}
73+
74+
return ($this->determineVersionCallable)();
75+
}
76+
5777
public function __construct(Client $client, ContextDetectorInterface $contextDetector = null, Container $container = null, array $middleware = [])
5878
{
5979
$this->client = $client;
@@ -215,7 +235,8 @@ public function createReport(Throwable $throwable): Report
215235
$report = Report::createForThrowable(
216236
$throwable,
217237
$this->contextDetector->detectCurrentContext(),
218-
$this->applicationPath
238+
$this->applicationPath,
239+
$this->version()
219240
);
220241

221242
return $this->applyMiddlewareToReport($report);

src/Report.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class Report
3838
/** @var string */
3939
private $applicationPath;
4040

41+
/** @var ?string */
42+
private $applicationVersion;
43+
4144
/** @var array */
4245
private $userProvidedContext = [];
4346

@@ -62,16 +65,21 @@ class Report
6265
/** @var string */
6366
private $groupBy ;
6467

65-
public static function createForThrowable(Throwable $throwable, ContextInterface $context, ?string $applicationPath = null): self
66-
{
68+
public static function createForThrowable(
69+
Throwable $throwable,
70+
ContextInterface $context,
71+
?string $applicationPath = null,
72+
?string $version = null
73+
): self {
6774
return (new static())
6875
->setApplicationPath($applicationPath)
6976
->throwable($throwable)
7077
->useContext($context)
7178
->exceptionClass(self::getClassForThrowable($throwable))
7279
->message($throwable->getMessage())
7380
->stackTrace(Stacktrace::createForThrowable($throwable, $applicationPath))
74-
->exceptionContext($throwable);
81+
->exceptionContext($throwable)
82+
->setApplicationVersion($version);
7583
}
7684

7785
protected static function getClassForThrowable(Throwable $throwable): string
@@ -193,6 +201,18 @@ public function getApplicationPath(): ?string
193201
return $this->applicationPath;
194202
}
195203

204+
public function setApplicationVersion(?string $applicationVersion)
205+
{
206+
$this->applicationVersion = $applicationVersion;
207+
208+
return $this;
209+
}
210+
211+
public function getApplicationVersion(): ?string
212+
{
213+
return $this->applicationVersion;
214+
}
215+
196216
public function view(?View $view)
197217
{
198218
$this->view = $view;
@@ -273,6 +293,7 @@ public function toArray()
273293
'message_level' => $this->messageLevel,
274294
'open_frame_index' => $this->openFrameIndex,
275295
'application_path' => $this->applicationPath,
296+
'application_version' => $this->applicationVersion,
276297
];
277298
}
278299
}

tests/FlareTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,36 @@ public function it_can_add_glows()
255255
],
256256
], $glows);
257257
}
258+
259+
/** @test */
260+
public function a_version_callable_can_be_set()
261+
{
262+
$this->assertNull($this->flare->version());
263+
264+
$this->flare->determineVersionUsing(function () {
265+
return '123';
266+
});
267+
268+
$this->assertEquals('123', $this->flare->version());
269+
}
270+
271+
/** @test */
272+
public function it_will_add_the_version_to_the_report()
273+
{
274+
$this->reportException();
275+
276+
$payload = $this->fakeClient->getLastPayload();
277+
278+
$this->assertNull($payload['application_version']);
279+
280+
$this->flare->determineVersionUsing(function () {
281+
return '123';
282+
});
283+
284+
$this->reportException();
285+
286+
$payload = $this->fakeClient->getLastPayload();
287+
288+
$this->assertEquals('123', $payload['application_version']);
289+
}
258290
}

tests/__snapshots__/FlareTest__it_can_report_an_exception__1.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ stage: null
1515
message_level: null
1616
open_frame_index: null
1717
application_path: null
18+
application_version: null

tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ stage: null
1515
message_level: null
1616
open_frame_index: null
1717
application_path: null
18+
application_version: null

tests/__snapshots__/ReportTest__it_can_create_a_report_for_a_string_message__1.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ stage: null
1515
message_level: null
1616
open_frame_index: 0
1717
application_path: null
18+
application_version: null

tests/__snapshots__/ReportTest__it_can_create_a_report_with_glows__1.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ stage: null
2222
message_level: null
2323
open_frame_index: null
2424
application_path: null
25+
application_version: null

0 commit comments

Comments
 (0)