Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add collecting identities * Apply fixes from StyleCI * [ci-review] Apply changes from Rector action. --------- Co-authored-by: StyleCI Bot <bot@styleci.io> Co-authored-by: rector-bot <rector@yiiframework.com>
- Loading branch information
1 parent
f8dc651
commit 03c53ad
Showing
7 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{ | ||
"symbol-whitelist": [ | ||
"Yiisoft\\Auth\\AuthenticationMethodInterface", | ||
"Yiisoft\\Auth\\IdentityInterface", | ||
"opcache_invalidate" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\Collector; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Yiisoft\Auth\AuthenticationMethodInterface; | ||
use Yiisoft\Auth\IdentityInterface; | ||
|
||
final class AuthenticationMethodInterfaceProxy implements AuthenticationMethodInterface | ||
{ | ||
public function __construct(private AuthenticationMethodInterface $decorated, private IdentityCollector $collector) | ||
{ | ||
} | ||
|
||
public function authenticate(ServerRequestInterface $request): ?IdentityInterface | ||
{ | ||
$identity = null; | ||
try { | ||
$identity = $this->decorated->authenticate($request); | ||
} finally { | ||
$this->collector->collect($identity); | ||
} | ||
return $identity; | ||
} | ||
|
||
public function challenge(ResponseInterface $response): ResponseInterface | ||
{ | ||
return $this->decorated->challenge($response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\Collector; | ||
|
||
use Yiisoft\Auth\IdentityInterface; | ||
|
||
final class IdentityCollector implements CollectorInterface, IndexCollectorInterface | ||
{ | ||
use CollectorTrait; | ||
|
||
private array $identities = []; | ||
|
||
public function getCollected(): array | ||
{ | ||
return $this->identities; | ||
} | ||
|
||
public function collect(?IdentityInterface $identity): void | ||
{ | ||
if (!$this->isActive()) { | ||
return; | ||
} | ||
|
||
if ($identity === null) { | ||
return; | ||
} | ||
|
||
$this->identities[] = [ | ||
'id' => $identity->getId(), | ||
'class' => $identity::class, | ||
]; | ||
} | ||
|
||
private function reset(): void | ||
{ | ||
$this->identities = []; | ||
} | ||
|
||
public function getIndexData(): array | ||
{ | ||
$lastIdentity = end($this->identities); | ||
return [ | ||
'identity' => [ | ||
'lastId' => is_array($lastIdentity) ? $lastIdentity['id'] : null, | ||
'total' => count($this->identities), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\Tests\Collector; | ||
|
||
use Yiisoft\Yii\Debug\Collector\CollectorInterface; | ||
use Yiisoft\Yii\Debug\Collector\IdentityCollector; | ||
use Yiisoft\Yii\Debug\Tests\Support\FakeIdentity; | ||
|
||
final class UserCollectorTest extends AbstractCollectorTestCase | ||
{ | ||
/** | ||
* @param IdentityCollector $collector | ||
*/ | ||
protected function collectTestData(CollectorInterface $collector): void | ||
{ | ||
$collector->collect(null); | ||
$collector->collect(new FakeIdentity('stub1')); | ||
|
||
$collector->collect(null); | ||
$collector->collect(new FakeIdentity('stub2')); | ||
$collector->collect(null); | ||
} | ||
|
||
protected function getCollector(): CollectorInterface | ||
{ | ||
return new IdentityCollector(); | ||
} | ||
|
||
protected function checkCollectedData(array $data): void | ||
{ | ||
parent::checkCollectedData($data); | ||
|
||
$this->assertCount(2, $data); | ||
$this->assertEquals([ | ||
['id' => 'stub1', 'class' => FakeIdentity::class], | ||
['id' => 'stub2', 'class' => FakeIdentity::class], | ||
], $data); | ||
} | ||
|
||
protected function checkIndexData(array $data): void | ||
{ | ||
parent::checkIndexData($data); | ||
|
||
$this->assertArrayHasKey('identity', $data); | ||
$this->assertArrayHasKey('lastId', $data['identity']); | ||
$this->assertEquals('stub2', $data['identity']['lastId']); | ||
$this->assertEquals(2, $data['identity']['total']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\Tests\Support; | ||
|
||
use Yiisoft\Auth\IdentityInterface; | ||
|
||
final class FakeIdentity implements IdentityInterface | ||
{ | ||
public function __construct(private ?string $id) | ||
{ | ||
} | ||
|
||
public function getId(): ?string | ||
{ | ||
return $this->id; | ||
} | ||
} |