在 Symfony 2.8 之前
SimplePreAuthenticatorInterface
位于以下命名空间 Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface
它在 2.8 中已被弃用,并在 3.0 中更改为 Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface
现在我正在写一个包,它必须在 symfony 2.7 和 symfony 3.0 中工作,它是一个供私有(private)使用的 api 身份验证器包。
我想为它写一个检查接口(interface)是否存在的工厂
示例来自 FosUserBundle
if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
$tokenStorage = $this->get('security.token_storage');
} else {
$tokenStorage = $this->get('security.context');
}
但是我实现这个接口(interface)的类是 DI 中的一个服务,symfony 防火墙直接使用它。
我的问题是如何以最佳实践和逻辑的方式进行这种抽象。
AccessTokenAuthenticator 类:
<?php
namespace MyCompany\SBundle\Security;
// this usage for before symfony 2.8
use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
/**
* Class AccessTokenAuthenticator
*/
class AccessTokenAuthenticator implements SimplePreAuthenticatorInterface
{
服务.yml
# Authentication
mycompany_security.security.accesstoken_authenticator:
class: MyCompany\SBundle\Security\AccessTokenAuthenticator
arguments: ["@mycompany_security.security.accesstoken_userprovider"]
防火墙配置:
secure_area:
pattern: ^/
stateless: true
simple_preauth:
authenticator: mycompany_security.security.accesstoken_authenticator
我的确切问题是,即使我定义了两个彼此相同但实现命名空间不同的类,即使我如何将其提供给防火墙?这如何从防火墙中抽象出来?
如有任何帮助,我们将不胜感激。
最佳答案
这可能不符合 PSR 但会起作用(可能)。
文件AccessTokenAuthenticator.php
if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
class AccessTokenAuthenticator implements \Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface { }
} else {
class AccessTokenAuthenticator implements \Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface { }
}
自动加载应该仍会拾取它。
关于php - 必须保持向后兼容性中断的身份验证器类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36835683/