bpa/api-sandbox-bundle
Composer 安装命令:
composer require bpa/api-sandbox-bundle
包简介
Symfony Bundle for easy creation of API Sandboxes and integration with NelmioApiDocBundle
README 文档
README
This bundle is designed to prevent requests using your real controllers and responding with a fake
response you defined. It integrates nicely with NelmioApiDocBundle and FOSRestBundle.
When using NelmioApiDocBundle this bundle will generate curl examples for your provided sandbox
requests automatically which will show in the documentation.
Requirements
ApiSandboxBundle requires php >= 5.5 and symfony >= 2.7.
Installation
The easiest way to install this library is through composer.
Just add the following lines to your composer.json file and run composer.phar update:
{
"require": {
"bpa/api-sandbox-bundle": "dev-master"
}
}
Configuration
Load the bundle in your AppKernel.php:
class AppKernel extends Kernel { public function registerBundles() { // ... $bundles = [ // ... new Bpa\ApiSandboxBundle\ApiSandboxBundle(), // ... ]; // ... } }
I would recommend to create a new environment for your sandbox by copying the app.php front controller
to something like app_sandbox.php. In your new front controller you have to change the following line
to the new environment:
$kernel = new AppKernel('sandbox', false);
Create a new config_sandbox.yml in your app/config directory with the following contents:
imports: - { resource: config_prod.yml } api_sandbox: enabled: true force_response: true
This takes all settings from your prod environment and enables the sandbox for your new sandbox
environment.
Usage
A basic Controller
When using the FOSRestBundle and NelmioApiDocBundle for your API an integration within your
application could look something like this:
<?php namespace AppBundle\Controller; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Bpa\ApiSandboxBundle\Annotation as Bpa; use FOS\RestBundle\Controller\FOSRestController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse; /** * Controller for books */ class BookController extends FOSRestController { /** * Get book informations * * Retrieve informations about a specific book * * @ApiDoc( * section="Books", * resource=true, * statusCodes={ * 200="Ok", * } * ) * * @Bpa\SandboxRequest( * responses={ * @Bpa\SandboxResponse( * statusCode=200, * content="@AppBundle/Resources/sandbox/responses/books/get.json", * ) * } * ) * * @param Request $request * * @return JsonResponse */ public function getAction(Request $request) { return new JsonResponse([ 'books' => [ 'id' => 1, 'title' => 'A Brief History of Time' ], ]); } }
Using parameters
It's possible to define multiple responses for a single Controller action and distinguish between
them by using the SandboxRequest\Parameter Annotation like this:
<?php class BookController extends FOSRestController { /** * ... * * @Bpa\SandboxRequest( * responses={ * @Bpa\SandboxResponse( * statusCode=200, * content="@AppBundle/Resources/sandbox/responses/books/get_1.json", * parameters={ * @Bpa\SandboxRequest\Parameter(name="id", value="1"), * } * ), * @Bpa\SandboxResponse( * statusCode=200, * content="@AppBundle/Resources/sandbox/responses/books/get_2.json", * parameters={ * @Bpa\SandboxRequest\Parameter(name="id", value="2"), * } * ), * @Bpa\SandboxResponse( * statusCode=200, * content="@AppBundle/Resources/sandbox/responses/books/get.json", * ) * } * ) * * ... */ public function getAction(Request $request) { /* ... */ } }
If you now provide the parameter id = 1 the first response will be returned. With id = 2 the second
one is returned and for all other requests the third response will be returned.
Automatic generation of documentation
With all this set up you will see automatically generated examples API documentation with
the NelmioApiDocBundle. If you designed your own theme for your documentation you are able
to provide your own ExampleGenerator which only should extend the provided ExampleGenerator
and override the buildExample method:
<?php namespace DocBundle\Service\ApiDoc\Extractor; use Bpa\ApiSandboxBundle\Annotation\SandboxResponse; use Bpa\ApiSandboxBundle\Service\ApiDoc\Extractor\ExampleGenerator as BaseGenerator; use Symfony\Component\Routing\Route; class ExampleGenerator extends BaseGenerator { /** * @param Route $route * @param SandboxResponse $response * * @return mixed|string */ protected function buildExample(Route $route, SandboxResponse $response) { return 'Your custom example markdown'; } }
Contributing
Please feel free to contribute to this bundle. Any contribution is highly appreciated and will be reviewed.
bpa/api-sandbox-bundle 适用场景与选型建议
bpa/api-sandbox-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 94 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bpa/api-sandbox-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bpa/api-sandbox-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 94
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2018-01-03