decodelabs/disciple

掌握您的用户

v0.4.4 2024-08-27 18:29 UTC

README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

掌握您的用户

Disciple提供了一套简单的接口,允许第三方代码定义可靠的入口点以访问用户状态和数据。

DecodeLabs博客上获取新闻和更新。

安装

通过Composer安装

composer require decodelabs/disciple

用法

导入

Disciple使用Veneer来在DecodeLabs\Disciple下提供一个统一的界面。您可以通过这个静态界面访问所有主要功能,而不会牺牲测试和依赖注入。

实现

Disciple的实现围绕一个适配器展开 - 它充当Disciple Veneer界面和您的系统用户管理基础设施之间的主要调解者。

namespace DecodeLabs\Disciple;

interface Adapter
{
    public function isLoggedIn(): bool;

    public function getIdentity(): ?string;
    public function getProfile(): Profile;

    public function isA(string ...$signifiers): bool;
}

您的适配器应在您的应用程序启动过程中进行注册

use DecodeLabs\Disciple;
use My\App\DiscipleAdapter;

Disciple::setAdapter(new DiscipleAdapter(
    $myUserManager
));

然后,在未来的任何时间点,都可以查询当前用户

use DecodeLabs\Disciple;

if(Disciple::isLoggedIn()) {
    echo 'Yay, you\'re logged in';
} else {
    echo 'Boo, nobody loves me';
}

个人资料

已注册的适配器应该能够提供一个Profile实例,表示关于当前用户的核心数据,例如姓名、电子邮件地址、地区等。

namespace DecodeLabs\Disciple;

interface Profile
{
    public function getId(): ?string;
    public function getEmail(): ?string;
    public function getFullName(): ?string;
    public function getFirstName(): ?string;
    public function getSurname(): ?string;
    public function getNickName(): ?string;

    public function getRegistrationDate(): ?DateTime;
    public function getLastLoginDate(): ?DateTime;

    public function getLanguage(): ?string;
    public function getCountry(): ?string;
    public function getTimeZone(): ?string;

    public function getSignifiers(): array;
}

Veneer界面可以直接与这些个人资料信息进行交互,从而快速访问用户数据

use DecodeLabs\Disciple;

if(Disciple::isLoggedIn()) {
    echo 'Hello '.Disciple::getFullName();
} else {
    echo 'You should probably log in first';
}

客户端

适配器还应能够提供一个客户端对象,可以报告用户如何与系统交互的详细信息。

目前,这包括以下内容,但在未来的版本中将增加更多

namespace DecodeLabs\Disciple;

interface Client
{
    public function getProtocol(): string;
    public function getIpString(): string;
    public function getAgent(): ?string;
}

标识符

Disciple接口定义了标识符的概念 - 用户可以根据这些字符串键进行分类和识别。

定义标识符的存储和分发方式的责任在于适配器实现,但是定义此接口允许在应用程序的高层结构中实现强大且快速访问的机制。

if(Disciple::isA('admin')) {
    echo 'You can see the fun stuff';
} else {
    echo 'You should go home now';
}

许可

Disciple在MIT许可下授权。有关完整的许可文本,请参阅LICENSE