Skip to content

Commit 47cf8c1

Browse files
authored
Merge pull request #908 from kenjis/perf-Auth
perf: refactor Auth
2 parents 2d7cd7c + 7d4bd5c commit 47cf8c1

File tree

5 files changed

+45
-45
lines changed

5 files changed

+45
-45
lines changed

phpstan-baseline.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
<?php declare(strict_types = 1);
22

33
$ignoreErrors = [];
4-
$ignoreErrors[] = [
5-
'message' => '#^Call to function property_exists\\(\\) with CodeIgniter\\\\Shield\\\\Config\\\\Auth and \'userProvider\' will always evaluate to true\\.$#',
6-
'count' => 1,
7-
'path' => __DIR__ . '/src/Auth.php',
8-
];
9-
$ignoreErrors[] = [
10-
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
11-
'count' => 1,
12-
'path' => __DIR__ . '/src/Auth.php',
13-
];
14-
$ignoreErrors[] = [
15-
'message' => '#^Only booleans are allowed in a ternary operator condition, CodeIgniter\\\\Shield\\\\Entities\\\\User\\|null given\\.$#',
16-
'count' => 1,
17-
'path' => __DIR__ . '/src/Auth.php',
18-
];
194
$ignoreErrors[] = [
205
'message' => '#^Call to deprecated function random_string\\(\\)\\:
216
The type \'basic\', \'md5\', and \'sha1\' are deprecated\\. They are not cryptographically secure\\.$#',

src/Auth.php

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
use CodeIgniter\Shield\Authentication\Authentication;
99
use CodeIgniter\Shield\Authentication\AuthenticationException;
1010
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
11+
use CodeIgniter\Shield\Config\Auth as AuthConfig;
1112
use CodeIgniter\Shield\Entities\User;
1213
use CodeIgniter\Shield\Models\UserModel;
1314

1415
/**
16+
* Facade for Authentication
17+
*
1518
* @method Result attempt(array $credentials)
1619
* @method Result check(array $credentials)
1720
* @method bool checkAction(string $token, string $type) [Session]
@@ -31,7 +34,8 @@ class Auth
3134
*/
3235
public const SHIELD_VERSION = '1.0.0-beta.7';
3336

34-
protected Authentication $authenticate;
37+
protected AuthConfig $config;
38+
protected ?Authentication $authenticate = null;
3539

3640
/**
3741
* The Authenticator alias to use for this request.
@@ -40,9 +44,21 @@ class Auth
4044

4145
protected ?UserModel $userProvider = null;
4246

43-
public function __construct(Authentication $authenticate)
47+
public function __construct(AuthConfig $config)
4448
{
45-
$this->authenticate = $authenticate->setProvider($this->getProvider());
49+
$this->config = $config;
50+
}
51+
52+
protected function ensureAuthentication(): void
53+
{
54+
if ($this->authenticate !== null) {
55+
return;
56+
}
57+
58+
$authenticate = new Authentication($this->config);
59+
$authenticate->setProvider($this->getProvider());
60+
61+
$this->authenticate = $authenticate;
4662
}
4763

4864
/**
@@ -52,7 +68,7 @@ public function __construct(Authentication $authenticate)
5268
*/
5369
public function setAuthenticator(?string $alias = null): self
5470
{
55-
if (! empty($alias)) {
71+
if ($alias !== null) {
5672
$this->alias = $alias;
5773
}
5874

@@ -64,6 +80,8 @@ public function setAuthenticator(?string $alias = null): self
6480
*/
6581
public function getAuthenticator(): AuthenticatorInterface
6682
{
83+
$this->ensureAuthentication();
84+
6785
return $this->authenticate
6886
->factory($this->alias);
6987
}
@@ -85,13 +103,15 @@ public function user(): ?User
85103
*/
86104
public function id()
87105
{
88-
return ($user = $this->user())
89-
? $user->id
90-
: null;
106+
$user = $this->user();
107+
108+
return ($user !== null) ? $user->id : null;
91109
}
92110

93111
public function authenticate(array $credentials): Result
94112
{
113+
$this->ensureAuthentication();
114+
95115
return $this->authenticate
96116
->factory($this->alias)
97117
->attempt($credentials);
@@ -134,22 +154,15 @@ public function getProvider(): UserModel
134154
return $this->userProvider;
135155
}
136156

137-
/** @var \CodeIgniter\Shield\Config\Auth $config */
138-
$config = config('Auth');
139-
140-
if (! property_exists($config, 'userProvider')) {
141-
throw AuthenticationException::forUnknownUserProvider();
142-
}
143-
144-
$className = $config->userProvider;
157+
$className = $this->config->userProvider;
145158
$this->userProvider = new $className();
146159

147160
return $this->userProvider;
148161
}
149162

150163
/**
151164
* Provide magic function-access to Authenticators to save use
152-
* from repeating code here, and to allow them have their
165+
* from repeating code here, and to allow them to have their
153166
* own, additional, features on top of the required ones,
154167
* like "remember-me" functionality.
155168
*
@@ -159,10 +172,12 @@ public function getProvider(): UserModel
159172
*/
160173
public function __call(string $method, array $args)
161174
{
162-
$authenticate = $this->authenticate->factory($this->alias);
175+
$this->ensureAuthentication();
176+
177+
$authenticator = $this->authenticate->factory($this->alias);
163178

164-
if (method_exists($authenticate, $method)) {
165-
return $authenticate->{$method}(...$args);
179+
if (method_exists($authenticator, $method)) {
180+
return $authenticator->{$method}(...$args);
166181
}
167182
}
168183
}

src/Authentication/Authentication.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use CodeIgniter\Shield\Config\Auth as AuthConfig;
88
use CodeIgniter\Shield\Models\UserModel;
99

10+
/**
11+
* Factory for Authenticators.
12+
*/
1013
class Authentication
1114
{
1215
/**
@@ -26,13 +29,10 @@ public function __construct(AuthConfig $config)
2629
}
2730

2831
/**
29-
* Returns an instance of the specified Authenticator.
32+
* Creates and returns the shared instance of the specified Authenticator.
3033
*
31-
* You can pass 'default' as the Authenticator and it
32-
* will return an instance of the first Authenticator specified
33-
* in the Auth config file.
34-
*
35-
* @param string|null $alias Authenticator alias
34+
* @param string|null $alias Authenticator alias. Passing `null` returns the
35+
* default authenticator.
3636
*
3737
* @throws AuthenticationException
3838
*/
@@ -61,7 +61,7 @@ public function factory(?string $alias = null): AuthenticatorInterface
6161
}
6262

6363
/**
64-
* Sets the User provider to use
64+
* Sets the User Provider to use.
6565
*
6666
* @return $this
6767
*/

src/Config/Services.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use CodeIgniter\Config\BaseService;
88
use CodeIgniter\Shield\Auth;
9-
use CodeIgniter\Shield\Authentication\Authentication;
109
use CodeIgniter\Shield\Authentication\JWTManager;
1110
use CodeIgniter\Shield\Authentication\Passwords;
11+
use CodeIgniter\Shield\Config\Auth as AuthConfig;
1212

1313
class Services extends BaseService
1414
{
@@ -21,9 +21,10 @@ public static function auth(bool $getShared = true): Auth
2121
return self::getSharedInstance('auth');
2222
}
2323

24+
/** @var AuthConfig $config */
2425
$config = config('Auth');
2526

26-
return new Auth(new Authentication($config));
27+
return new Auth($config);
2728
}
2829

2930
/**

src/Helpers/auth_helper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ function auth(?string $alias = null): Auth
2222

2323
if (! function_exists('user_id')) {
2424
/**
25-
* Returns the ID for the current logged in user.
26-
* Note: For \CodeIgniter\Shield\Entities\User this will always return an int.
25+
* Returns the ID for the current logged-in user.
2726
*
2827
* @return int|string|null
2928
*/

0 commit comments

Comments
 (0)