예제 #1
0
 protected function encodeResource(ResourceFactory $resource, RamlEncoderOptions $options)
 {
     $controller = $this->app->make($resource->getController());
     $ramlResource = ['displayName' => $resource->getName(), 'description' => $resource->getDescription()];
     foreach ($resource->getMethods() as $method) {
         $ramlAction = ['securedBy' => $this->middlewareToSecuritySchemes($options, $resource->getMiddleware())];
         if ($controller instanceof AnnotatedControllerInterface) {
             $ramlAction['description'] = $controller->getMethodDescription($method->getMethod());
             $ramlAction['body'] = $this->requestsToBody($controller->getMethodExampleRequests($method->getMethod()))->toArray();
             $ramlAction['responses'] = $this->responsesToGroup($controller->getMethodExampleResponses($method->getMethod()))->toArray();
         } else {
             $ramlAction['description'] = 'This method does not provide a description';
         }
         $reflector = new ResourceReflector($this->app);
         $request = $reflector->getMethodRequest($resource, $method);
         $uriParameters = [];
         $queryParameters = [];
         if ($request instanceof ApiCheckableRequest) {
             $spec = $request->getCheckable();
             if ($spec instanceof Validator) {
                 $spec = $spec->getSpec();
             }
             if ($spec instanceof Spec) {
                 if ($method->getVerb() == HttpMethods::POST || $method->getVerb() == HttpMethods::PUT || $method->getVerb() == HttpMethods::DELETE) {
                     $this->addPostSchema($ramlResource, $ramlAction, $uriParameters, $resource, $method, $spec, $reflector);
                 } else {
                     $parameters = $reflector->getMethodParameters($resource, $method);
                     $fields = array_unique(array_merge(array_keys($spec->getConstraints()), array_keys($spec->getDefaults()), $spec->getRequired()));
                     foreach ($fields as $field) {
                         if (in_array($field, $parameters)) {
                             $uriParameters[$field] = $this->specFieldToParameter($spec, $field);
                             continue;
                         }
                         $queryParameters[$field] = $this->specFieldToParameter($spec, $field);
                     }
                 }
             }
         }
         if (!Arr::has($ramlResource, $method->getPath())) {
             $ramlResource[$method->getPath()] = [];
         }
         $ramlResource[$method->getPath()]['uriParameters'] = $uriParameters;
         $ramlAction['queryParameters'] = $queryParameters;
         $verb = strtolower($method->getVerb());
         $ramlResource[$method->getPath()][$verb] = RamlUtils::filterEmptyValues($ramlAction);
         $ramlResource[$method->getPath()] = RamlUtils::filterEmptyValues($ramlResource[$method->getPath()]);
     }
     return RamlUtils::filterEmptyValues($ramlResource);
 }
 /**
  * @param ResourceFactory $factory
  * @param ResourceMethod $method
  *
  * @return Div|string
  */
 protected function reflectOnRequest(ResourceFactory $factory, ResourceMethod $method)
 {
     try {
         $reflector = new ResourceReflector($this->container);
         $request = $reflector->getRequest($reflector->getMethodArgumentTypes($factory, $method));
         if ($request instanceof ApiCheckableRequest) {
             $spec = $request->getCheckable();
             if ($spec instanceof Validator) {
                 $spec = $spec->getSpec();
             }
             if (!$spec instanceof Spec) {
                 return 'Only Specs are supported at the moment.';
             }
             return new Div([], [new Div([], [new Bold([], 'Required: '), implode(', ', $spec->getRequired())]), new Div([], [new Table(['class' => 'table'], [new TableHeader([], [new TableRow([], [new TableHeaderCell([], 'Field'), new TableHeaderCell([], 'Constraints'), new TableHeaderCell([], 'Default')])]), new TableBody([], Std::map(function ($value, $key) use($spec) {
                 return $this->renderConstraint($spec, $key);
             }, $spec->getConstraints()))])])]);
         } else {
             return 'Handler cannot be reflected upon.';
         }
     } catch (Exception $e) {
         return 'Unable to resolve handler';
     }
 }