netvlies / basecamp-bundle
基于 Basecamp API 客户端的包
dev-master
2013-06-14 14:58 UTC
Requires
- php: >=5.3.0
- netvlies/basecamp-php: >=1.0
This package is not auto-updated.
Last update: 2024-09-23 13:38:01 UTC
README
Symfony2 包,围绕 basecamp-php 客户端。它提供了一种简单的方式将新的 Basecamp API 集成到您的项目中。
安装
使用 Composer 安装包
$ composer.phar require netvlies/basecamp-bundle
在您的内核中启用包
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Netvlies\Bundle\BasecampBundle\NetvliesBasecampBundle(), ); }
配置
此包支持通过 HTTP 认证(简单快捷)或 OAuth(稍微复杂一些)进行身份验证。
HTTP 认证
以下是一个使用 HTTP 认证的最低配置示例。您需要做的就是提供自己的凭据。
# app/config/config.yml netvlies_basecamp: authentication: http app_name: My Funky Application app_contact: http://www.myfunkyapplication.com identification: user_id: 1234 username: your@username.com password: secret
OAuth 认证
如果您有更复杂的使用场景,您可能希望使用 OAuth。要在您的 Symfony2 项目中实现 OAuth,我们推荐使用 HWIOAuthBundle。
创建 OAuth 凭据提供者
首先,实现 Netvlies\Bundle\BasecampBundle\OAuth\CredentialsProviderInterface
。以下是一个简单的示例,假设您将 OAuth 数据存储在 User
实体中
namespace Acme\Bundle\MainBundle\OAuth; use Netvlies\Bundle\BasecampBundle\OAuth\CredentialsProviderInterface; use Symfony\Component\Security\Core\SecurityContext; class BasecampCredentialsProvider implements CredentialsProviderInterface { protected $context; public function __construct(SecurityContext $context) { $this->context = $context; } public function getBasecampId() { if (! $this->context->isGranted('IS_AUTHENTICATED_FULLY')) { throw new \RuntimeException('Please login before using Basecamp'); } return $this->context->getToken()->getUser()->getBasecampId(); } public function getToken() { if (! $this->context->isGranted('IS_AUTHENTICATED_FULLY')) { throw new \RuntimeException('Please login before using Basecamp'); } return $this->context->getToken()->getUser()->getToken(); } }
现在将其注册为一个服务
services: acme.basecamp.oauth_credentials_provider: class: Acme\Bundle\MainBundle\OAuth\BasecampCredentialsProvider arguments: ["@security.context"]
配置包以使用您的提供者
最后,将服务 ID 添加到包配置中,如下所示
netvlies_basecamp: authentication: oauth app_name: My Funky Application app_contact: http://www.myfunkyapplication.com oauth: credentials_provider: acme.basecamp.oauth_credentials_provider
使用方法
您现在可以从容器中获取客户端并使用它
$client = $this->get('basecamp'); $project = $client->getProject(array( 'projectId' => 1 ));