Skip to content

Commit

Permalink
Merge pull request #241 from bowphp/5.x-refactoring-for-perf
Browse files Browse the repository at this point in the history
[5.x] Fixes for model Relationship performance
  • Loading branch information
papac authored May 24, 2023
2 parents bcfd0c0 + 04c99dc commit e6b96bc
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 11 deletions.
10 changes: 10 additions & 0 deletions src/Auth/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ public function getAuthenticateUserId()
{
return $this->attributes[$this->primary_key];
}

/**
* Define the additionals values
*
* @return array
*/
public function customJwtAttributes(): array
{
return [];
}
}
2 changes: 1 addition & 1 deletion src/Auth/Guards/GuardContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Bow\Auth\Authentication;

/**
* @method ?string getToken()
* @method ?\Policier\Token getToken()
*/
abstract class GuardContract
{
Expand Down
4 changes: 3 additions & 1 deletion src/Auth/Guards/JwtGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ public function getToken(): ?Token
*/
public function login(Authentication $user): bool
{
$this->token = $this->getPolicier()->encode($user->getAuthenticateUserId(), [
$attributes = array_merge($user->customJwtAttributes(), [
"id" => $user->getAuthenticateUserId(),
"logged" => true
]);

$this->token = $this->getPolicier()->encode($user->getAuthenticateUserId(), $attributes);

return true;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Auth/Guards/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ private function getSession(): Session
$session = Session::getInstance();

if (is_null($session)) {
throw new AuthenticationException("Please the session configuration is not load");
throw new AuthenticationException(
"Please the session configuration is not load"
);
}

return $session;
Expand Down
8 changes: 4 additions & 4 deletions src/Database/Barry/Concerns/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ public function hasMany(
* The has one relative
*
* @param string $related
* @param string $primary_key
* @param string $foreign_key
* @param string $primary_key
* @return HasOne
*/
public function hasOne(
string $related,
?string $primary_key = null,
?string $foreign_key = null
?string $foreign_key = null,
?string $primary_key = null
): HasOne {
$related_model = app()->make($related);

Expand All @@ -124,6 +124,6 @@ public function hasOne(
$foreign_key = rtrim($related_model->getTable(), 's') . '_id';
}

return new HasOne($related_model, $this, $primary_key, $foreign_key);
return new HasOne($related_model, $this, $foreign_key, $primary_key);
}
}
2 changes: 1 addition & 1 deletion src/Database/Barry/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(
*/
public function getResults(): ?Model
{
$key = $this->query->getTable() . "_" . $this->local_key;
$key = $this->query->getTable() . ":belongsto:" . $this->related->getTable() . ":" . $this->foreign_key;
$cache = Cache::cache('file')->get($key);

if (!is_null($cache)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Barry/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(Model $related, Model $parent, string $foreign_key,
*/
public function getResults(): ?Model
{
$key = $this->query->getTable() . "_" . $this->local_key;
$key = $this->query->getTable() . ":hasone:" . $this->related->getTable() . ":" . $this->foreign_key;
$cache = Cache::cache('file')->get($key);

if (!is_null($cache)) {
Expand Down
1 change: 1 addition & 0 deletions tests/Auth/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function test_it_should_be_a_default_guard()
{
$config = TestingConfiguration::getConfig();
$auth = Auth::getInstance();

$this->assertEquals($auth->getName(), $config["auth"]["default"]);
$this->assertEquals($auth->getName(), "web");
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Config/stubs/policier.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
/**
* Hashing algorithm being used
*
* HS256, HS384, HS512, RS256, RS384, RS512, ES256, ES384, ES512,
* HS256, HS384, HS512, ES256, ES384, ES512,
*/
"alg" => "HS256",
"alg" => "HS512",
];

0 comments on commit e6b96bc

Please sign in to comment.