如何在 Zend Expressive 应用程序上下文中的 BlameableListener 中设置用户值?

How to set an user value in BlameableListener in a Zend Expressive application context?

如何在 Zend Expressive 应用程序中为 Gedmo\Blameable\BlameableListener 设置用户值?

事件订阅者已成功添加到 EventManager(请参阅配置文件)。 TimestampableListener 正在按预期工作。

/config/autoload/doctrine.local.php

<?php

declare(strict_types = 1);

use Gedmo\Blameable\BlameableListener;
use Gedmo\Timestampable\TimestampableListener;

return [
    'doctrine' => [
        // [..]
        'event_manager' => [
            'orm_default' => [
                'subscribers' => [
                    BlameableListener::class,
                    TimestampableListener::class,
                ],
            ],
        ],
    ],
];

backend/App/Entity/Role.php

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Blameable\Traits\BlameableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;

/**
 * @ORM\Entity()
 * @ORM\Table(name="role")
 */
class Role
{
    use TimestampableEntity;
    use BlameableEntity;

    /**
     * @ORM\Id()
     * @ORM\Column(name="id", type="guid")
     * @ORM\GeneratedValue(strategy="UUID")
     * @var string
     */
    private $id;

    /**
     * @ORM\Column(name="name", type="string")
     * @var string
     */
    private $name;

    // [..]
}

documentation 中指出:

Note that you need to set the user on the BlameableListener (unless you use the Symfony2 extension which does automatically assign the current security context user).

我不确定如何实现它。在某个地方我应该能够定义一个可调用的或 class 来处理设置用户值,但是如何?

我通过定义 BlameableUserValueHandlerInterface 解决了这个问题。必须实施并传递给身份验证中间件。中间件将当前身份提供给 BlameableUserValueHandler,后者将其分配给 BlameableListener.

中的用户值