Skip to content
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

[4.x] Fix Browser Sessions not showing platform and browser #1412

Merged
merged 4 commits into from
Nov 29, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 14 additions & 15 deletions src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Laravel\Jetstream;

use Closure;
use Detection\Cache\CacheException;
use Detection\Exception\MobileDetectException;
use Detection\MobileDetect;

/**
Expand Down Expand Up @@ -50,6 +48,13 @@ class Agent extends MobileDetect
'WeChat' => 'MicroMessenger',
];

/**
* Key value store for resolved strings.
*
* @var array<string, mixed>
*/
protected $store = [];

/**
* Get the platform name from the User Agent.
*
Expand Down Expand Up @@ -126,24 +131,18 @@ protected function findDetectionRulesAgainstUserAgent(array $rules)
* @param string $key
* @param \Closure():mixed $callback
* @return mixed
*
* @throws \Detection\Exception\MobileDetectException
*/
protected function retrieveUsingCacheOrResolve(string $key, Closure $callback)
{
try {
$cacheKey = $this->createCacheKey($key);
$cacheKey = $this->createCacheKey($key);

if (! is_null($cacheItem = $this->cache->get($cacheKey))) {
return $cacheItem->get();
}

return tap(call_user_func($callback), function ($result) use ($cacheKey) {
$this->cache->set($cacheKey, $result);
});
} catch (CacheException $e) {
throw new MobileDetectException("Cache problem in for {$key}: {$e->getMessage()}");
if (! is_null($cacheItem = $this->store[$cacheKey] ?? null)) {
return $cacheItem;
}

return tap(call_user_func($callback), function ($result) use ($cacheKey) {
$this->store[$cacheKey] = $result;
});
}

/**
Expand Down
10 changes: 8 additions & 2 deletions tests/AgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public function testOperatingSystems($userAgent, $platform)
$agent = new Agent();
$agent->setUserAgent($userAgent);

$this->assertEquals($platform, $agent->platform());
$this->assertSame($platform, $agent->platform());

// Test cached value return the same output.
$this->assertSame($platform, $agent->platform());
}

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

$this->assertEquals($browser, $agent->browser());
$this->assertSame($browser, $agent->browser());

// Test cached value return the same output.
$this->assertSame($browser, $agent->browser());
}

public static function browsersDataProvider()
Expand Down