定制 symfonycorp/connect 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

symfonycorp/connect

Composer 安装命令:

composer require symfonycorp/connect

包简介

SymfonyConnect SDK

README 文档

README

About

This is the official SDK for the SymfonyConnect API. It works for the public API or with a registered OAuth application. To register an application, please go to your SymfonyConnect Account.

Installation

To install the SDK, run the command below and you will get the latest version:

composer require symfonycorp/connect

Usage

OAuth

To use the SDK in a Symfony application, we recommend using the built-in bundle.

Otherwise, you can take inspiration from the following part, which will show you how to include OAuth authentication within a Silex App.

Warning: We take for granted that you already have registered your app on SymfonyConnect and that you're in possession of your application_id, application_secret and scope.

  1. Configure your silex app with the data we gave us at app registration.

    // index.php
    use SymfonyCorp\Connect\Api\Api;
    use SymfonyCorp\Connect\OAuthConsumer;
    
    $app = new Silex\Application();
    $app['connect_id'] = 'application_id';
    $app->register(new Silex\Provider\UrlGeneratorServiceProvider());
    $app->register(new Silex\Provider\SessionServiceProvider());
    
    $app['connect_secret'] = 'application_secret';
    // List of scope copy-pasted from your application page on SymfonyConnect
    $app['connect_scope'] = array(
        'SCOPE_ADDITIONAL_EMAILS',
        'SCOPE_BIRTHDAY',
        'SCOPE_EMAIL',
        'SCOPE_LOCATION',
        'SCOPE_PUBLIC',
        'SCOPE_SSH_KEYS',
    );
    
    $app['connect_consumer'] = new OAuthConsumer(
        $app['connect_id'],
        $app['connect_secret'],
        implode(' ', $app['connect_scope']) // scope MUST be space separated
    );
    $app['connect_api'] = new Api();

    This done. We can now move on to the second step.

  2. We need to create two controllers to handle the OAuth2 Three-Legged workflow.

    The first controller goal is to redirect the user to SymfonyConnect in order to ask him for the authorization that your app will use his data. This controller will be bound to the connect_auth route. In your template, you'll need to create a link to this route.

    // index.php
    $app->get('/connect/new', function () use ($app) {
        $callback = $app['url_generator']->generate('connect_callback', array(), true);
        $url = $app['connect_consumer']->getAuthorizationUri($callback);
    
        return $app->redirect($url);
    })->bind('connect_auth');

    The second controller is the one that will welcome the user after SymfonyConnect redirected him to your application. When registering your client, you'll have to provide the exact absolute URL that points to this controller.

    $app->get('/connect/callback', function (Request $request) use ($app) {
        // There was an error during the workflow.
        if ($request->get('error')) {
            throw new \RuntimeException($request->get('error_description'));
        }
    
        // Everything went fine, you can now request an access token.
        try {
            $data = $app['connect_consumer']->requestAccessToken($app['url_generator']->generate('connect_callback', array(), true), $request->get('code'));
        } catch (OAuthException $e) {
            throw $e;
        }
    
        // At this point, we have an access token and we can use the SDK to request the API
        $app['connect_api']->setAccessToken($data['access_token']); // All further request will be done with this access token
        $root = $app['connect_api']->getRoot();
        $user = $root->getCurrentUser();
        $user->getBadges()->refresh();
    
        $app['session']->start();
        $app['session']->set('connect_access_token', $data['access_token']);
        $app['session']->set('connect_user', $user);
    
        return $app->redirect('/');
    })->bind('connect_callback');
  3. Create a link from your template

    In a template, you can use the following snippet of code to render a SymfonyConnect button:

    <a href="#" class="connect-with-symfony">
        <span>Log in with SymfonyConnect</span>
    </a>

    And include the following CSS file: https://connect.symfony.com/css/sln.css

Et voilà! Your application can now use SymfonyConnect as an authentication method!

The API

The SymfonyConnect Connect API is RESTFul and (tries to) conforms to the HATEOAS principle.

Here are some useful recipes.

  1. Search

    $root = $api->getRoot();
    
    // Will search for users
    $users = $root->getUsers('fab');
  2. Edit authenticated user

    $app['connect_api']->setAccessToken($app['session']->get('connect_access_token'));
    $root = $app['connect_api']->getRoot();
    $user = $root->getCurrentUser();
    $user->setBiography("I'm sexy and I know it.");
    $user->submitForm();

As you can see by these examples, you always have to to go through the API Root to make an action. This is because the API is discoverable and that the SDK should not know anything beside the API's entry point.

License

This library is licensed under the MIT license.

symfonycorp/connect 适用场景与选型建议

symfonycorp/connect 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46.95k 次下载、GitHub Stars 达 92, 最近一次更新时间为 2019 年 07 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「sdk」 「SymfonyConnect」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 symfonycorp/connect 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 symfonycorp/connect 我们能提供哪些服务?
定制开发 / 二次开发

基于 symfonycorp/connect 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 46.95k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 92
  • 点击次数: 21
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 92
  • Watchers: 17
  • Forks: 33
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-08