tomi20v / phalswag

phalswag 是一个基于 cphalcon 框架的 php 使用的 swagger 工具包

0.1.0 2015-03-29 23:30 UTC

This package is not auto-updated.

Last update: 2024-10-02 09:36:32 UTC


README

phalswag 是 Phalcon 框架的一个组件,用于验证入站 HTTP 请求与 swagger.json 架构的匹配,并构建相应的响应。目标是尽可能地自动化基于 swagger 定义编写 REST API。

使用提供的 Phalcon Mvc Controller 扩展的最简单示例

class UsersController extends Controller
{
	// by defining these,
	protected static $_swaggerPath = '../app/config/swagger';
	protected static $_swaggerFname = 'users.json';

	public function getAction() {

		try {

			$Response = $this->_process(
				// swagger operation ID
				'usersGet',
				// callback which receives input and shall return result data
				function($RequestModel) {
					$User = UserModel::findById($RequestModel->id);
					return $User;
				}
			);

		}
		catch (\Exception $e) {

			$Response = $this->ResponseBuilder->buildError(500);

		}

		return $Response;

	}
}

更完整的示例将包括

// get the operation object
$Operation = $this->SwaggerService->getOperationById(
	$operationId,
	$this->_Swagger
);

// bind to request model
$this->SwaggerService->bindRequest(
	$RequestModel,
	$Operation,
	$this->dispatcher->getParams(),
	$this->request
);

// get response schema and build from data object
$ResponseSchema = $this->SwaggerService->getResponseSchema(
	200,
	$Operation,
	$this->_Swagger
);
$Result = $this->SwaggerService->buildBySchema(
	$Object,
	$ResponseSchema
);

当前状态:对于swagger定义中的大多数元素,可遍历模型并用来自 HTTP 输入的数据填充您选择的请求模型(类)。可以对数据进行基本验证,但例如无法验证正文中的结构。它可以构建包含结果数据的模型响应。它不能解析 $ref。