Php 重写Wordpress的ResourceOwner类
我去覆盖到Php 重写Wordpress的ResourceOwner类,php,symfony,service,oauth-2.0,hwioauthbundle,Php,Symfony,Service,Oauth 2.0,Hwioauthbundle,我去覆盖到HWIOAuthBundle,因为它没有返回用户信息响应 我注意到每个ResourceOwner都以服务的形式工作,并声明为我已经将注册小枝覆盖到了HWIOAuthBundle中,并创建了一个新的bundle,继承自HWIOAuthBundle类似的内容,但我无法使用它 搜索后,我发现我们可以覆盖WordpressResourceOwner服务的类 步骤: 1) 创建新的资源所有者 例: 2) 创建CompilerPass类以覆盖服务类,如 例: 3) 我们必须将build方法重写为B
HWIOAuthBundle
,因为它没有返回用户信息响应
我注意到每个ResourceOwner都以服务的形式工作,并声明为我已经将注册小枝覆盖到了
HWIOAuthBundle
中,并创建了一个新的bundle,继承自HWIOAuthBundle
类似的内容,但我无法使用它
搜索后,我发现我们可以覆盖WordpressResourceOwner服务的类
步骤:
1) 创建新的资源所有者
例:
2) 创建CompilerPass类以覆盖服务类,如
例:
3) 我们必须将build
方法重写为Bundle类
例:
4) 最后一步调用的compile
方法类似于
例:
namespace project\OAuthBundle\OAuth\ResourceOwner;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth2ResourceOwner;
use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
/**
* Override WordpressResourceOwner class into HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\WordpressResourceOwner
*
* @author ahmedhamdy
*
*/
class WordpressResourceOwner extends GenericOAuth2ResourceOwner
{
/**
* {@inheritDoc}
*/
protected $paths = array(
'identifier' => 'ID',
'nickname' => 'username',
'realname' => 'display_name',
'email' => 'email',
'profilepicture' => 'avatar_URL',
);
/**
* {@inheritDoc}
*/
protected function doGetUserInformationRequest($url, array $parameters = array())
{
$apiAccessToken = $parameters['apiAccessToken'];
$headers = array(
0 => 'authorization: Bearer '.$apiAccessToken,
);
return $this->httpRequest($url,null,$headers);
}
/**
* {@inheritDoc}
*/
public function getUserInformation(array $accessToken, array $extraParameters = array())
{
$url = $this->normalizeUrl($this->options['infos_url'], array(
'access_token' => $accessToken['access_token']
));
$parameters = array(
'apiAccessToken' => $accessToken['access_token'],
);
$content = $this->doGetUserInformationRequest($url,$parameters)->getContent();
$response = $this->getUserResponse();
$response->setResponse($content);
$response->setResourceOwner($this);
$response->setOAuthToken(new OAuthToken($accessToken));
return $response;
}
/**
* {@inheritDoc}
*/
protected function configureOptions(OptionsResolverInterface $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefaults(array(
'authorization_url' => 'https://public-api.wordpress.com/oauth2/authorize',
'access_token_url' => 'https://public-api.wordpress.com/oauth2/token',
'infos_url' => 'https://public-api.wordpress.com/rest/v1/me',
));
}
}
namespace project\OAuthBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* OverrideWordpressResourceOwner
*
* @author ahmedhamdy
*/
class OverrideWordpressResourceOwner implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$definition = $container->getDefinition("hwi_oauth.abstract_resource_owner.wordpress");
$definition->setClass('ProfileTree\OAuthBundle\OAuth\ResourceOwner\WordpressResourceOwner');
}
}
namespace Project\OAuthBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use ProfileTree\OAuthBundle\DependencyInjection\Compiler\OverrideWordpressResourceOwner;
class ProfileTreeOAuthBundle extends Bundle
{
public function getParent()
{
return 'HWIOAuthBundle';
}
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new OverrideWordpressResourceOwner());
}
}
namespace Project\OAuthBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class ProfileTreeOAuthExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$container->compile();
}
}