tebru/retrofit-bundle
Composer 安装命令:
composer require tebru/retrofit-bundle
包简介
A Symfony2 bundle for the retrofit-php library.
README 文档
README
This Symfony2 bundle aims to provide an easy way to use the tebru/retrofit-php library. Please review the documentation for functionality.
Installation
composer require tebru/retrofit-bundle
As Retrofit does not include an http client, install Guzzle.
composer require guzzlehttp/guzzle
Add a line to your AppKernel.php
new Tebru\RetrofitBundle\TebruRetrofitBundle()
Usage
You will need to create providers for each API you want to consume. They should be created with the builder and return a RestAdapter. Here is an example:
<?php namespace AppBundle; use Tebru\Retrofit\Adapter\Rest\RestAdapter; class FooBarRestAdapterProvider { public static function get($baseUrl) { // setup additional dependencies like an http client here and use // the setters to add them to the builder // build the rest adapter return RestAdapter::builder() ->setBaseUrl($baseUrl) ->build(); } }
Create annotated interfaces. A simple example is shown below, more detailed examples are available here.
<?php namespace AppBundle; use Tebru\Retrofit\Annotation as Rest; interface Foo { /** * @Rest\GET("/get/{id}") */ public function getFoo($id); }
Everything else can be configured in your service definition. A yaml example is shown.
parameters: foo.class: AppBundle\Foo bar.class: AppBundle\Bar services: # create a rest adapter foobar_rest_adapter: class: %tebru_retrofit.rest_adapter.class% factory: [AppBundle\FooBarRestAdapterProvider, get] arguments: - "https://foobar.com" # use the rest adapter to create clients # clients must be tagged with 'tebru_retrofit.register' foo_service: class: %foo.class% factory: [@foobar_rest_adapter, create] arguments: [%foo.class%] tags: - { name: tebru_retrofit.register } bar_service: class: %bar.class% factory: [@foobar_rest_adapter, create] arguments: [%bar.class%] tags: - { name: tebru_retrofit.register } # inject your client services anywhere baz: class: AppBundle\Baz arguments: [@foo_service, @bar_service]
Mocking
Because Retrofit uses interfaces, it's easy to create mock implementations if you do not want to hit a real API.
services: foo_service: class: %foo.class% factory: [@foobar_rest_adapter, create] arguments: ['AppBundle\MockFoo'] tags: - { name: tebru_retrofit.register }
services: foo_service: class: %foo.class% factory: [@foobar_rest_adapter, create] arguments: [@mock_foo] tags: - { name: tebru_retrofit.register }
Take note of the arguments key in the above example. You may pass in a concrete class as a string or service to use that instead of the generated class. This is especially useful during development.
tebru/retrofit-bundle 适用场景与选型建议
tebru/retrofit-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.58k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2015 年 01 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tebru/retrofit-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tebru/retrofit-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 20.58k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-01-31