public function parse($schema, $path) { $this->data = $this->parser->parse($schema); $path = ApiGeneration::transformRoutePlaceholder($path); if (isset($this->data[$path]) && is_array($this->data[$path])) { $resource = $this->parseResource($this->data[$path], $path); } else { // we check whether the path is nested $parts = explode('/', trim($path, '/')); $data = $this->data; foreach ($parts as $part) { if (isset($data['/' . $part])) { $data = $data['/' . $part]; } else { $data = null; break; } } if (!empty($data) && is_array($data)) { $resource = $this->parseResource($data, $path); } else { throw new RuntimeException('Could not find resource definition "' . $path . '" in RAML schema'); } } $version = $this->getNormalizedVersion(); $title = null; if (isset($this->data['title'])) { $title = $this->data['title']; } $doc = new Documentation\Version($title); $doc->addResource($version, $resource); return $doc; }
protected function getApis(Resource $resource) { $api = new Api(ApiGeneration::transformRoutePlaceholder($resource->getPath())); $description = $resource->getDescription(); $methods = $resource->getMethods(); if (!empty($description)) { $api->setDescription($description); } foreach ($methods as $method) { // get operation name $request = $method->getRequest(); $response = $this->getSuccessfulResponse($method); $description = $method->getDescription(); $entityName = ''; if ($request instanceof SchemaInterface) { $entityName = $request->getDefinition()->getName(); } elseif ($response instanceof SchemaInterface) { $entityName = $response->getDefinition()->getName(); } // create new operation $operation = new Operation($method->getName(), strtolower($method->getName()) . ucfirst($entityName)); if (!empty($description)) { $operation->setSummary($description); } // path parameter $parameters = $resource->getPathParameters()->getDefinition(); foreach ($parameters as $parameter) { $param = new Parameter('path', $parameter->getName(), $parameter->getDescription(), $parameter->isRequired()); $this->setParameterType($parameter, $param); $operation->addParameter($param); } // query parameter $parameters = $method->getQueryParameters()->getDefinition(); foreach ($parameters as $parameter) { $param = new Parameter('query', $parameter->getName(), $parameter->getDescription(), $parameter->isRequired()); $this->setParameterType($parameter, $param); $operation->addParameter($param); } // request body if ($request instanceof SchemaInterface) { $description = $request->getDefinition()->getDescription(); $type = strtolower($method->getName()) . 'Request'; $parameter = new Parameter('body', 'body', $description, true); $parameter->setType($type); $operation->addParameter($parameter); } // response body $responses = $method->getResponses(); foreach ($responses as $statusCode => $response) { $type = strtolower($method->getName()) . 'Response'; $message = $response->getDefinition()->getDescription() ?: 'Response'; $operation->addResponseMessage(new ResponseMessage($statusCode, $message, $type)); } $api->addOperation($operation); } return array($api); }
public function doIndex() { $resourceListing = new ResourceListing('1.0'); $resources = $this->resourceListing->getResourceIndex(); foreach ($resources as $resource) { $path = '/*'; $path .= ApiGeneration::transformRoutePlaceholder($resource->getPath()); $resourceListing->addResource(new ResourceObject($path)); } $this->setBody($resourceListing, WriterInterface::JSON); }
public function generate(Resource $resource) { $path = ApiGeneration::transformRoutePlaceholder($resource->getPath() ?: '/'); $description = $resource->getDescription(); $raml = '#%RAML 0.8' . "\n"; $raml .= '---' . "\n"; $raml .= 'baseUri: ' . Inline::dump($this->baseUri) . "\n"; $raml .= 'version: v' . $this->version . "\n"; $raml .= 'title: ' . Inline::dump($this->title) . "\n"; $raml .= $path . ':' . "\n"; if (!empty($description)) { $raml .= ' description: ' . Inline::dump($description) . "\n"; } // path parameter $parameters = $resource->getPathParameters()->getDefinition(); if (count($parameters) > 0) { $raml .= ' uriParameters:' . "\n"; foreach ($parameters as $parameter) { $raml .= ' ' . $parameter->getName() . ':' . "\n"; $this->setParameterType($parameter, $raml, 6); } } $generator = new SchemaGenerator\JsonSchema($this->targetNamespace); $methods = $resource->getMethods(); foreach ($methods as $method) { $raml .= ' ' . strtolower($method->getName()) . ':' . "\n"; // description $description = $method->getDescription(); if (!empty($description)) { $raml .= ' description: ' . Inline::dump($description) . "\n"; } // query parameter $parameters = $method->getQueryParameters()->getDefinition(); if (count($parameters) > 0) { $raml .= ' queryParameters:' . "\n"; foreach ($parameters as $parameter) { $raml .= ' ' . $parameter->getName() . ':' . "\n"; $this->setParameterType($parameter, $raml, 8); } } // request body if ($method->hasRequest()) { $schema = $generator->generate($method->getRequest()); $schema = str_replace("\n", "\n ", $schema); $raml .= ' body:' . "\n"; $raml .= ' application/json:' . "\n"; $raml .= ' schema: |' . "\n"; $raml .= ' ' . $schema . "\n"; } // response body $raml .= ' responses:' . "\n"; $responses = $method->getResponses(); foreach ($responses as $statusCode => $response) { $schema = $generator->generate($response); $schema = str_replace("\n", "\n ", $schema); $raml .= ' ' . $statusCode . ':' . "\n"; $raml .= ' body:' . "\n"; $raml .= ' application/json:' . "\n"; $raml .= ' schema: |' . "\n"; $raml .= ' ' . $schema . "\n"; } } return $raml; }