Ejemplo n.º 1
0
 public function onLoad()
 {
     parent::onLoad();
     $doc = $this->resourceListing->getDocumentation($this->context->get(Context::KEY_PATH));
     if (!$doc instanceof DocumentationInterface) {
         throw new RuntimeException('No documentation available');
     }
     $this->version = $this->getVersion($doc);
     $this->resource = $this->getResource($doc, $this->version);
     if (!$this->resource->hasMethod($this->getMethod())) {
         throw new StatusCode\MethodNotAllowedException('Method is not allowed', $this->resource->getAllowedMethods());
     }
     $this->pathParameters = $this->schemaAssimilator->assimilate($this->resource->getPathParameters(), $this->uriFragments);
     $this->queryParameters = $this->schemaAssimilator->assimilate($this->resource->getMethod($this->getMethod())->getQueryParameters(), $this->request->getUri()->getParameters());
 }
Ejemplo n.º 2
0
 public function testResource()
 {
     $resource = new Resource(Resource::STATUS_ACTIVE, '/foo');
     $resource->setTitle('foobar');
     $resource->setDescription('foobar');
     $resource->addPathParameter(Property::getString('foo'));
     $resource->addMethod(Factory::getMethod('GET'));
     $this->assertEquals(Resource::STATUS_ACTIVE, $resource->getStatus());
     $this->assertTrue($resource->isActive());
     $this->assertFalse($resource->isDeprecated());
     $this->assertFalse($resource->isClosed());
     $this->assertEquals('/foo', $resource->getPath());
     $this->assertEquals('foobar', $resource->getTitle());
     $this->assertEquals('foobar', $resource->getDescription());
     $this->assertInstanceOf('PSX\\Data\\SchemaInterface', $resource->getPathParameters());
     $this->assertInstanceOf('PSX\\Api\\Resource\\MethodAbstract', $resource->getMethod('GET'));
     $this->assertEquals(['GET' => $resource->getMethod('GET')], $resource->getMethods());
     $this->assertEquals(['GET'], $resource->getAllowedMethods());
     $this->assertTrue($resource->hasMethod('GET'));
     $this->assertFalse($resource->hasMethod('POST'));
 }
Ejemplo n.º 3
0
Archivo: Raml.php Proyecto: seytar/psx
 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;
 }
Ejemplo n.º 4
0
 public function generate(Resource $resource)
 {
     $class = strtolower(str_replace('\\', '-', get_class($this)));
     $html = '<div class="psx-resource ' . $class . '" data-status="' . $resource->getStatus() . '" data-path="' . $resource->getPath() . '">';
     $html .= '<h4>' . $this->getName() . '</h4>';
     $description = $resource->getDescription();
     if (!empty($description)) {
         $html .= '<div class="psx-resource-description">' . $description . '</div>';
     }
     $methods = $resource->getMethods();
     foreach ($methods as $method) {
         $html .= '<div class="psx-resource-method" data-method="' . $method->getName() . '">';
         $description = $method->getDescription();
         if (!empty($description)) {
             $html .= '<div class="psx-resource-method-description">' . $description . '</div>';
         }
         // path parameters
         $pathParameters = $resource->getPathParameters();
         if ($pathParameters instanceof SchemaInterface) {
             $result = $this->generateHtml($pathParameters, self::TYPE_PATH, $method->getName(), $resource->getPath());
             if (!empty($result)) {
                 $html .= '<div class="psx-resource-data psx-resource-query">';
                 $html .= '<h5>' . $method->getName() . ' Path-Parameters</h5>';
                 $html .= '<div class="psx-resource-data-content">' . $result . '</div>';
                 $html .= '</div>';
             }
         }
         // query parameters
         $queryParameters = $method->getQueryParameters();
         if ($queryParameters instanceof SchemaInterface) {
             $result = $this->generateHtml($queryParameters, self::TYPE_QUERY, $method->getName(), $resource->getPath());
             if (!empty($result)) {
                 $html .= '<div class="psx-resource-data psx-resource-query">';
                 $html .= '<h5>' . $method->getName() . ' Query-Parameters</h5>';
                 $html .= '<div class="psx-resource-data-content">' . $result . '</div>';
                 $html .= '</div>';
             }
         }
         // request
         $request = $method->getRequest();
         if ($request instanceof SchemaInterface) {
             $result = $this->generateHtml($request, self::TYPE_REQUEST, $method->getName(), $resource->getPath());
             if (!empty($result)) {
                 $html .= '<div class="psx-resource-data psx-resource-request">';
                 $html .= '<h5>' . $method->getName() . ' Request</h5>';
                 $html .= '<div class="psx-resource-data-content">' . $result . '</div>';
                 $html .= '</div>';
             }
         }
         // responses
         $responses = $method->getResponses();
         foreach ($responses as $statusCode => $response) {
             $result = $this->generateHtml($response, self::TYPE_RESPONSE, $method->getName(), $resource->getPath(), $statusCode);
             if (!empty($result)) {
                 $message = isset(Http::$codes[$statusCode]) ? Http::$codes[$statusCode] : 'Unknown';
                 $html .= '<div class="psx-resource-data psx-resource-response">';
                 $html .= '<h5>' . $method->getName() . ' Response - ' . $statusCode . ' ' . $message . '</h5>';
                 $html .= '<div class="psx-resource-data-content">' . $result . '</div>';
                 $html .= '</div>';
             }
         }
         $html .= '</div>';
     }
     $html .= '</div>';
     return $html;
 }
Ejemplo n.º 5
0
 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);
 }