承接 igsem/phalcon-swagger 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

igsem/phalcon-swagger

Composer 安装命令:

composer require igsem/phalcon-swagger

包简介

A Swagger plugin to generate API documentation for Phalcon PHP framework.

README 文档

README

What it does?

This bundle/plugin parses your source code and generates swagger documentation for your API.

How does it work?

The bundle has one controller with 2 actions.

  • The first action parses your source code and returns a json
  • The second controller renders a view to display Swagger-UI

Swagger-UI uses CDN's for its assets, therefor no js or css files are included!

Hot to install?

You can install the bundle via composer or just download the git repos and paste it into your project dir.

There are 2 important things which needs to be configured:

Routing

You must configure 2 routes in order to get it working:

    [
        'class' => \Igsem\Docs\Controllers\DocsController::class,
        'methods' => [
            'get' => [
                getenv('SWAGGER_JSON_URI') => 'indexAction',
                '/docs' => 'docsAction',
            ],
        ],
    ],

The first route is the route of the json response, which swagger needs in order to render the documentation. This URL maps to a controller which scans your project for annotations.

The second route is the route where you want to be able to access the documentation.

I am using an env file for my configurations, therefore the first route is a parameter in my env file. The reason for this is that the URL for the json response needs to be registered in the DI as well.

DI

The bundle is expecting an entry with the name swagger in your di container.

Here I am loading the configuration from an .env file:

'swagger' => [
        'path' => APP_PATH . '/src',
        'host' => getenv('SWAGGER_HOST'),
        'schemes' => explode(',', getenv('SWAGGER_SCHEMES')),
        'basePath' => getenv('SWAGGER_BASEPATH'),
        'version' => getenv('SWAGGER_VERSION'),
        'title' => getenv('SWAGGER_TITLE'),
        'description' => getenv('SWAGGER_DESCRIPTION'),
        'email' => getenv('SWAGGER_EMAIL'),
        'jsonUri' => getenv('SWAGGER_JSON_URI'),
        'exclude' => explode(',', getenv('SWAGGER_EXCLUDE')),
    ],

And here I am registering the di:

    /**
     * Configure Swagger
     */
    protected function initDocs()
    {
        /** @var PhConfig $config */
        $config = $this->diContainer->getShared('config');
        $this->diContainer->setShared(
            'swagger',
            function () use ($config) {
                return $config->get('swagger')->toArray();
            }

        );
    }

Now, it is up to you how you get the swagger entry into your di container. You can just go and register all the values statically.

These values are expected and mandatory:

  • path - a path to your source dir, which should be scanned, ideally APP_PATH. '/src'
  • host - your domain name e.g. my-awesome-api.com
  • schemes [array] - schemes which are supported e.g. http,https(this must be an array)
  • basePath - url base path e.g. /
  • version - your API version e.g. 0.0.1
  • title - title of your application
  • description - description of your application
  • email - contact email
  • jsonUri - the url you configured for the json response e.g. /swagger-json

These values are optional:

  • exclude [string|array] - a path(s) to exclude from scanning, ex. APP_PATH. '/src/path_to_exclude/' or [APP_PATH. '/src/path_to_exclude_1/', APP_PATH. '/src/path_to_exclude_2/']

Usage

The usage is the same as with the standard Swagger library, see https://github.com/zircote/swagger-php for more info.

I am using just a basic configuration for Swagger but if you would like to extend it, use ignored folders etc. I recommend to have an annotation on your base controller like this:

/**
 * @SWG\Swagger(
 *     schemes={"http","https"},
 *     host="api.host.com",
 *     basePath="/",
 *     @SWG\Info(
 *         version="1.0.0",
 *         title="This is my website cool API",
 *         description="Api description...",
 *         termsOfService="",
 *         @SWG\Contact(
 *             email="contact@mysite.com"
 *         ),
 *         @SWG\License(
 *             name="Private License",
 *             url="URL to the license"
 *         )
 *     ),
 *     @SWG\ExternalDocumentation(
 *         description="Find out more about my website",
 *         url="http..."
 *     )
 * )
 */

class BaseController

!Please note that the configuration is overwriting the annotation, therefore use this as an extend only!

alt text

Examples

Here is how to annotate models:


/**
 * @SWG\Definition(required={"email", "name", "password"}, type="object", @SWG\Xml(name="User"))
 */
class User extends Model
{
    /**
     * @SWG\Property(name="id", type="string", description="UUID")
     * @var int
     */
    public $id;

    /**
     * @SWG\Property(name="name", type="string")
     * @var string
     */
    public $name;

    /**
     * @SWG\Property(name="email", type="string")
     * @var string
     */
    public $email;

    /**
     * @SWG\Property(name="password", type="string")
     * @var string
     */
    public $password;
}

And an example controller for login:

/**
     *
     * @SWG\POST(
     *   path="/login",
     *   summary="Login",
     *   produces={"application/json"},
     *     @SWG\Parameter(
     *     in="formData",
     *     type="string",
     *     name="email",
     *     required=true,
     *     @SWG\Schema(ref="#/definitions/User")
     *   ),
     *     @SWG\Parameter(
     *     in="formData",
     *     type="string",
     *     name="password",
     *     required=true,
     *     @SWG\Schema(ref="#/definitions/User")
     *   ),
     *   @SWG\Response(
     *     response=200,
     *     description="Returns a JWT token for authorization"
     *   ),
     *   @SWG\Response(
     *     response=404,
     *     description="Not found User, Invalid password"
     *   ),
     *   @SWG\Response(
     *     response=422,
     *     description="Validation of formData failed"
     *   )
     * )
     * @return string
     */
    public function loginAction()
    {
        ...

alt text

What is missing

I came up with the library quiet fast and had no time to write tests or test it on more examples. The library should work and I am using it in my projects. Everyone is free to use or modify it as he sees fit. I will be more than happy to have some pull requests :) if someone is interested.

igsem/phalcon-swagger 适用场景与选型建议

igsem/phalcon-swagger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 57.71k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2017 年 04 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 igsem/phalcon-swagger 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 57.71k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 16
  • Watchers: 3
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-04-16