public function generate(Resource $resource) { $declaration = new Declaration($this->apiVersion); $declaration->setBasePath($this->basePath); $declaration->setApis($this->getApis($resource)); $declaration->setModels($this->getModels($resource)); $declaration->setResourcePath(ApiGeneration::transformRoutePlaceholder($resource->getPath())); $writer = new JsonWriter(); $swagger = $writer->write($declaration); // since swagger does not fully support the json schema spec we must // remove the $ref fragments $swagger = str_replace('#\\/definitions\\/', '', $swagger); return $swagger; }
private function scanMethods(Declaration $declaration, ReflectionClass $class, $endpoint, array &$models) { $methods = $class->getMethods(); $endpoint = trim($endpoint, '/'); foreach ($methods as $method) { if ($method->isPublic()) { $doc = $this->getAnnotations($class, $method->getName()); $httpMethod = $doc->getFirstAnnotation('httpMethod'); $path = $doc->getFirstAnnotation('path'); if (!empty($httpMethod) && !empty($path)) { // add api $path = '/' . trim($endpoint . $path, '/'); $desc = trim($doc->getText()); $api = new Api($path, $desc); $this->addOperationByComment($api, $doc, $httpMethod, $models); $declaration->addApi($api); } } } }
public function testSerialize() { $operation = new Operation('PUT', 'updatePet', 'Update an existing pet'); $api = new Api('/foo', 'Foobar'); $api->addOperation($operation); $model = new Model('Order', 'Order object'); $model->addProperty(new Property('id', 'integer')); $model->addProperty(new PropertyReference('customer', 'Customer')); $model->addProperty(new Property('petId', 'integer')); $model->addProperty(new Property('quantity', 'integer')); $model->addProperty(new Property('status', 'string')); $model->addProperty(new Property('shipDate', 'string')); $declaration = new Declaration('1.0.0', 'http://petstore.swagger.wordnik.com/api', '/store'); $declaration->addApi($api); $declaration->addModel($model); $content = <<<'JSON' { "apiVersion": "1.0.0", "swaggerVersion": "1.2", "basePath": "http://petstore.swagger.wordnik.com/api", "resourcePath": "/store", "apis": [{ "path": "/foo", "description": "Foobar", "operations": [{ "method": "PUT", "nickname": "updatePet", "summary": "Update an existing pet", "parameters": [], "responseMessages": [] }] }], "models": { "Order": { "id": "Order", "description": "Order object", "properties": { "id": { "id": "id", "type": "integer" }, "customer": { "id": "customer", "$ref": "Customer" }, "petId": { "id": "petId", "type": "integer" }, "quantity": { "id": "quantity", "type": "integer" }, "status": { "id": "status", "type": "string" }, "shipDate": { "id": "shipDate", "type": "string" } } } } } JSON; $this->assertRecordEqualsContent($declaration, $content); }