Ejemplo n.º 1
0
Archivo: Xsd.php Proyecto: seytar/psx
 public function appendSchema(DOMElement $element, Resource $resource)
 {
     $methods = $resource->getMethods();
     foreach ($methods as $method) {
         $request = $method->getRequest();
         $response = $this->getSuccessfulResponse($method);
         $name = strtolower($method->getName()) . 'Request';
         if ($request instanceof SchemaInterface) {
             $this->appendSchemaElement($request, $element, $name);
         } else {
             $el = $element->ownerDocument->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:element');
             $el->setAttribute('name', $name);
             $el->setAttribute('type', 'tns:' . 'void');
             $element->appendChild($el);
         }
         $name = strtolower($method->getName()) . 'Response';
         if ($response instanceof SchemaInterface) {
             $this->appendSchemaElement($response, $element, $name);
         } else {
             $el = $element->ownerDocument->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:element');
             $el->setAttribute('name', $name);
             $el->setAttribute('type', 'tns:' . 'void');
             $element->appendChild($el);
         }
     }
     // add default schema types
     $this->appendDefaultSchema($element);
 }
Ejemplo n.º 2
0
Archivo: Wsdl.php Proyecto: seytar/psx
 protected function getOperations(Resource $resource)
 {
     $operations = array();
     $methods = $resource->getMethods();
     foreach ($methods as $method) {
         $request = $method->getRequest();
         $response = $this->getSuccessfulResponse($method);
         if ($request instanceof SchemaInterface) {
             $entityName = $request->getDefinition()->getName();
         } elseif ($response instanceof SchemaInterface) {
             $entityName = $response->getDefinition()->getName();
         }
         $operation = new Operation(strtolower($method->getName()) . ucfirst($entityName));
         $operation->setMethod($method->getName());
         if ($request instanceof SchemaInterface) {
             $operation->setIn($request->getDefinition()->getName());
         }
         if ($response instanceof SchemaInterface) {
             $operation->setOut($response->getDefinition()->getName());
         }
         if ($operation->hasOperation()) {
             $operations[] = $operation;
         }
     }
     return $operations;
 }
Ejemplo n.º 3
0
 public function generate(Resource $resource)
 {
     $methods = $resource->getMethods();
     $definitions = array();
     $properties = array();
     foreach ($methods as $method) {
         $request = $method->getRequest();
         if ($request instanceof SchemaInterface) {
             list($defs, $props) = $this->getJsonSchema($request);
             $key = strtolower($method->getName()) . 'Request';
             $definitions = array_merge($definitions, $defs);
             if (isset($props['properties'])) {
                 $properties[$key] = $props;
             }
         }
         $response = $this->getSuccessfulResponse($method);
         if ($response instanceof SchemaInterface) {
             list($defs, $props) = $this->getJsonSchema($response);
             $key = strtolower($method->getName()) . 'Response';
             $definitions = array_merge($definitions, $defs);
             if (isset($props['properties'])) {
                 $properties[$key] = $props;
             }
         }
     }
     $result = array('$schema' => JsonSchemaGenerator::SCHEMA, 'id' => $this->targetNamespace, 'type' => 'object', 'definitions' => $definitions, 'properties' => $properties);
     return Json::encode($result, JSON_PRETTY_PRINT);
 }
Ejemplo n.º 4
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);
 }
Ejemplo n.º 5
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.º 6
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.º 7
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;
 }