Skip to content

Commit e6b96bc

Browse files
authored
Merge pull request #241 from bowphp/5.x-refactoring-for-perf
[5.x] Fixes for model Relationship performance
2 parents bcfd0c0 + 04c99dc commit e6b96bc

9 files changed

Lines changed: 26 additions & 11 deletions

File tree

src/Auth/Authentication.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,14 @@ public function getAuthenticateUserId()
1717
{
1818
return $this->attributes[$this->primary_key];
1919
}
20+
21+
/**
22+
* Define the additionals values
23+
*
24+
* @return array
25+
*/
26+
public function customJwtAttributes(): array
27+
{
28+
return [];
29+
}
2030
}

src/Auth/Guards/GuardContract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Bow\Auth\Authentication;
99

1010
/**
11-
* @method ?string getToken()
11+
* @method ?\Policier\Token getToken()
1212
*/
1313
abstract class GuardContract
1414
{

src/Auth/Guards/JwtGuard.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,13 @@ public function getToken(): ?Token
147147
*/
148148
public function login(Authentication $user): bool
149149
{
150-
$this->token = $this->getPolicier()->encode($user->getAuthenticateUserId(), [
150+
$attributes = array_merge($user->customJwtAttributes(), [
151151
"id" => $user->getAuthenticateUserId(),
152152
"logged" => true
153153
]);
154154

155+
$this->token = $this->getPolicier()->encode($user->getAuthenticateUserId(), $attributes);
156+
155157
return true;
156158
}
157159

src/Auth/Guards/SessionGuard.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ private function getSession(): Session
7878
$session = Session::getInstance();
7979

8080
if (is_null($session)) {
81-
throw new AuthenticationException("Please the session configuration is not load");
81+
throw new AuthenticationException(
82+
"Please the session configuration is not load"
83+
);
8284
}
8385

8486
return $session;

src/Database/Barry/Concerns/Relationship.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ public function hasMany(
104104
* The has one relative
105105
*
106106
* @param string $related
107-
* @param string $primary_key
108107
* @param string $foreign_key
108+
* @param string $primary_key
109109
* @return HasOne
110110
*/
111111
public function hasOne(
112112
string $related,
113-
?string $primary_key = null,
114-
?string $foreign_key = null
113+
?string $foreign_key = null,
114+
?string $primary_key = null
115115
): HasOne {
116116
$related_model = app()->make($related);
117117

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

127-
return new HasOne($related_model, $this, $primary_key, $foreign_key);
127+
return new HasOne($related_model, $this, $foreign_key, $primary_key);
128128
}
129129
}

src/Database/Barry/Relations/BelongsTo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct(
5151
*/
5252
public function getResults(): ?Model
5353
{
54-
$key = $this->query->getTable() . "_" . $this->local_key;
54+
$key = $this->query->getTable() . ":belongsto:" . $this->related->getTable() . ":" . $this->foreign_key;
5555
$cache = Cache::cache('file')->get($key);
5656

5757
if (!is_null($cache)) {

src/Database/Barry/Relations/HasOne.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct(Model $related, Model $parent, string $foreign_key,
4949
*/
5050
public function getResults(): ?Model
5151
{
52-
$key = $this->query->getTable() . "_" . $this->local_key;
52+
$key = $this->query->getTable() . ":hasone:" . $this->related->getTable() . ":" . $this->foreign_key;
5353
$cache = Cache::cache('file')->get($key);
5454

5555
if (!is_null($cache)) {

tests/Auth/AuthenticationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function test_it_should_be_a_default_guard()
4444
{
4545
$config = TestingConfiguration::getConfig();
4646
$auth = Auth::getInstance();
47+
4748
$this->assertEquals($auth->getName(), $config["auth"]["default"]);
4849
$this->assertEquals($auth->getName(), "web");
4950
}

tests/Config/stubs/policier.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
/**
4747
* Hashing algorithm being used
4848
*
49-
* HS256, HS384, HS512, RS256, RS384, RS512, ES256, ES384, ES512,
49+
* HS256, HS384, HS512, ES256, ES384, ES512,
5050
*/
51-
"alg" => "HS256",
51+
"alg" => "HS512",
5252
];

0 commit comments

Comments
 (0)