Skip to content

Commit 252348e

Browse files
olumbycrynobone
andauthored
[4.x] Fix Browser Sessions not showing platform and browser (#1412)
* use simple key value store * Add failing tests for PR #1412 Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
1 parent c0aa1cb commit 252348e

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/Agent.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace Laravel\Jetstream;
44

55
use Closure;
6-
use Detection\Cache\CacheException;
7-
use Detection\Exception\MobileDetectException;
86
use Detection\MobileDetect;
97

108
/**
@@ -50,6 +48,13 @@ class Agent extends MobileDetect
5048
'WeChat' => 'MicroMessenger',
5149
];
5250

51+
/**
52+
* Key value store for resolved strings.
53+
*
54+
* @var array<string, mixed>
55+
*/
56+
protected $store = [];
57+
5358
/**
5459
* Get the platform name from the User Agent.
5560
*
@@ -126,24 +131,18 @@ protected function findDetectionRulesAgainstUserAgent(array $rules)
126131
* @param string $key
127132
* @param \Closure():mixed $callback
128133
* @return mixed
129-
*
130-
* @throws \Detection\Exception\MobileDetectException
131134
*/
132135
protected function retrieveUsingCacheOrResolve(string $key, Closure $callback)
133136
{
134-
try {
135-
$cacheKey = $this->createCacheKey($key);
137+
$cacheKey = $this->createCacheKey($key);
136138

137-
if (! is_null($cacheItem = $this->cache->get($cacheKey))) {
138-
return $cacheItem->get();
139-
}
140-
141-
return tap(call_user_func($callback), function ($result) use ($cacheKey) {
142-
$this->cache->set($cacheKey, $result);
143-
});
144-
} catch (CacheException $e) {
145-
throw new MobileDetectException("Cache problem in for {$key}: {$e->getMessage()}");
139+
if (! is_null($cacheItem = $this->store[$cacheKey] ?? null)) {
140+
return $cacheItem;
146141
}
142+
143+
return tap(call_user_func($callback), function ($result) use ($cacheKey) {
144+
$this->store[$cacheKey] = $result;
145+
});
147146
}
148147

149148
/**

tests/AgentTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ public function testOperatingSystems($userAgent, $platform)
2222
$agent = new Agent();
2323
$agent->setUserAgent($userAgent);
2424

25-
$this->assertEquals($platform, $agent->platform());
25+
$this->assertSame($platform, $agent->platform());
26+
27+
// Test cached value return the same output.
28+
$this->assertSame($platform, $agent->platform());
2629
}
2730

2831
public static function operatingSystemsDataProvider()
@@ -48,7 +51,10 @@ public function testBrowsers($userAgent, $browser)
4851
$agent = new Agent();
4952
$agent->setUserAgent($userAgent);
5053

51-
$this->assertEquals($browser, $agent->browser());
54+
$this->assertSame($browser, $agent->browser());
55+
56+
// Test cached value return the same output.
57+
$this->assertSame($browser, $agent->browser());
5258
}
5359

5460
public static function browsersDataProvider()

0 commit comments

Comments
 (0)