public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
 {
     // description
     if (null === $annotation->getDescription()) {
         $comments = explode("\n", $annotation->getDocumentation());
         // just set the first line
         $comment = trim($comments[0]);
         $comment = preg_replace("#\n+#", ' ', $comment);
         $comment = preg_replace('#\\s+#', ' ', $comment);
         $comment = preg_replace('#[_`*]+#', '', $comment);
         if ('@' !== substr($comment, 0, 1)) {
             $annotation->setDescription($comment);
         }
     }
     // requirements
     $requirements = $annotation->getRequirements();
     foreach ($route->getRequirements() as $name => $value) {
         if (!isset($requirements[$name]) && '_method' !== $name && '_scheme' !== $name) {
             $requirements[$name] = array('requirement' => $value, 'dataType' => '', 'description' => '');
         }
         if ('_scheme' === $name) {
             $https = 'https' == $value;
             $annotation->setHttps($https);
         }
     }
     if (method_exists($route, 'getSchemes')) {
         $annotation->setHttps(in_array('https', $route->getSchemes()));
     }
     $paramDocs = array();
     foreach (explode("\n", $this->commentExtractor->getDocComment($method)) as $line) {
         if (preg_match('{^@param (.+)}', trim($line), $matches)) {
             $paramDocs[] = $matches[1];
         }
         if (preg_match('{^@deprecated\\b(.*)}', trim($line), $matches)) {
             $annotation->setDeprecated(true);
         }
         if (preg_match('{^@link\\b(.*)}', trim($line), $matches)) {
             $annotation->setLink($matches[1]);
         }
     }
     $regexp = '{(\\w*) *\\$%s\\b *(.*)}i';
     foreach ($route->compile()->getVariables() as $var) {
         $found = false;
         foreach ($paramDocs as $paramDoc) {
             if (preg_match(sprintf($regexp, preg_quote($var)), $paramDoc, $matches)) {
                 $requirements[$var]['dataType'] = isset($matches[1]) ? $matches[1] : '';
                 $requirements[$var]['description'] = $matches[2];
                 if (!isset($requirements[$var]['requirement'])) {
                     $requirements[$var]['requirement'] = '';
                 }
                 $found = true;
                 break;
             }
         }
         if (!isset($requirements[$var]) && false === $found) {
             $requirements[$var] = array('requirement' => '', 'dataType' => '', 'description' => '');
         }
     }
     $annotation->setRequirements($requirements);
 }
Esempio n. 2
0
 /**
  * Returns a new ApiDoc instance with more data.
  *
  * @param  ApiDoc            $annotation
  * @param  Route             $route
  * @param  \ReflectionMethod $method
  * @return ApiDoc
  */
 protected function extractData(ApiDoc $annotation, Route $route, \ReflectionMethod $method)
 {
     // create a new annotation
     $annotation = clone $annotation;
     // doc
     $annotation->setDocumentation($this->commentExtractor->getDocCommentText($method));
     // parse annotations
     $this->parseAnnotations($annotation, $route, $method);
     // route
     $annotation->setRoute($route);
     // input (populates 'parameters' for the formatters)
     if (null !== ($input = $annotation->getInput())) {
         $parameters = array();
         $normalizedInput = $this->normalizeClassParameter($input);
         $supportedParsers = array();
         foreach ($this->getParsers($normalizedInput) as $parser) {
             if ($parser->supports($normalizedInput)) {
                 $supportedParsers[] = $parser;
                 $parameters = $this->mergeParameters($parameters, $parser->parse($normalizedInput));
             }
         }
         foreach ($supportedParsers as $parser) {
             if ($parser instanceof PostParserInterface) {
                 $parameters = $this->mergeParameters($parameters, $parser->postParse($normalizedInput, $parameters));
             }
         }
         $parameters = $this->clearClasses($parameters);
         $parameters = $this->generateHumanReadableTypes($parameters);
         if ('PUT' === $method) {
             // All parameters are optional with PUT (update)
             array_walk($parameters, function ($val, $key) use(&$data) {
                 $parameters[$key]['required'] = false;
             });
         }
         $annotation->setParameters($parameters);
     }
     // output (populates 'response' for the formatters)
     if (null !== ($output = $annotation->getOutput())) {
         $response = array();
         $supportedParsers = array();
         $normalizedOutput = $this->normalizeClassParameter($output);
         foreach ($this->getParsers($normalizedOutput) as $parser) {
             if ($parser->supports($normalizedOutput)) {
                 $supportedParsers[] = $parser;
                 $response = $this->mergeParameters($response, $parser->parse($normalizedOutput));
             }
         }
         foreach ($supportedParsers as $parser) {
             if ($parser instanceof PostParserInterface) {
                 $mp = $parser->postParse($normalizedOutput, $response);
                 $response = $this->mergeParameters($response, $mp);
             }
         }
         $response = $this->clearClasses($response);
         $response = $this->generateHumanReadableTypes($response);
         $annotation->setResponse($response);
     }
     return $annotation;
 }
 protected function getDescription($className, PropertyMetadata $item)
 {
     $ref = new \ReflectionClass($className);
     if ($item instanceof VirtualPropertyMetadata) {
         $extracted = $this->commentExtractor->getDocCommentText($ref->getMethod($item->getter));
     } else {
         $extracted = $this->commentExtractor->getDocCommentText($ref->getProperty($item->name));
     }
     return $extracted;
 }
 /**
  * Returns a new ApiDoc instance with more data.
  *
  * @param  ApiDoc            $annotation
  * @param  Route             $route
  * @param  \ReflectionMethod $method
  * @return ApiDoc
  */
 protected function extractData(ApiDoc $annotation, Route $route, \ReflectionMethod $method)
 {
     // create a new annotation
     $annotation = clone $annotation;
     // doc
     $annotation->setDocumentation($this->commentExtractor->getDocCommentText($method));
     // parse annotations
     $this->parseAnnotations($annotation, $route, $method);
     // route
     $annotation->setRoute($route);
     // input (populates 'parameters' for the formatters)
     if (null !== ($input = $annotation->getInput())) {
         $parameters = array();
         $normalizedInput = $this->normalizeClassParameter($input);
         $supportedParsers = array();
         foreach ($this->getParsers($normalizedInput) as $parser) {
             if ($parser->supports($normalizedInput)) {
                 $supportedParsers[] = $parser;
                 $parameters = $this->mergeParameters($parameters, $parser->parse($normalizedInput));
             }
         }
         foreach ($supportedParsers as $parser) {
             if ($parser instanceof PostParserInterface) {
                 $parameters = $this->mergeParameters($parameters, $parser->postParse($normalizedInput, $parameters));
             }
         }
         $parameters = $this->clearClasses($parameters);
         $parameters = $this->generateHumanReadableTypes($parameters);
         if ('PATCH' === $annotation->getMethod()) {
             // All parameters are optional with PUT (update)
             array_walk($parameters, function ($val, $key) use(&$parameters) {
                 $parameters[$key]['required'] = false;
             });
         }
         $annotation->setParameters($parameters);
     }
     // output (populates 'response' for the formatters)
     if (null !== ($output = $annotation->getOutput())) {
         $response = array();
         $supportedParsers = array();
         $normalizedOutput = $this->normalizeClassParameter($output);
         foreach ($this->getParsers($normalizedOutput) as $parser) {
             if ($parser->supports($normalizedOutput)) {
                 $supportedParsers[] = $parser;
                 $response = $this->mergeParameters($response, $parser->parse($normalizedOutput));
             }
         }
         foreach ($supportedParsers as $parser) {
             if ($parser instanceof PostParserInterface) {
                 $mp = $parser->postParse($normalizedOutput, $response);
                 $response = $this->mergeParameters($response, $mp);
             }
         }
         $response = $this->clearClasses($response);
         $response = $this->generateHumanReadableTypes($response);
         $annotation->setResponse($response);
         $annotation->setResponseForStatusCode($response, $normalizedOutput, 200);
     }
     if (count($annotation->getResponseMap()) > 0) {
         foreach ($annotation->getResponseMap() as $code => $modelName) {
             if ('200' === (string) $code && isset($modelName['type']) && isset($modelName['model'])) {
                 /*
                  * Model was already parsed as the default `output` for this ApiDoc.
                  */
                 continue;
             }
             $normalizedModel = $this->normalizeClassParameter($modelName);
             $parameters = array();
             $supportedParsers = array();
             foreach ($this->getParsers($normalizedModel) as $parser) {
                 if ($parser->supports($normalizedModel)) {
                     $supportedParsers[] = $parser;
                     $parameters = $this->mergeParameters($parameters, $parser->parse($normalizedModel));
                 }
             }
             foreach ($supportedParsers as $parser) {
                 if ($parser instanceof PostParserInterface) {
                     $mp = $parser->postParse($normalizedModel, $parameters);
                     $parameters = $this->mergeParameters($parameters, $mp);
                 }
             }
             $parameters = $this->clearClasses($parameters);
             $parameters = $this->generateHumanReadableTypes($parameters);
             $annotation->setResponseForStatusCode($parameters, $normalizedModel, $code);
         }
     }
     return $annotation;
 }
 /**
  * Returns a new ApiDoc instance with more data.
  *
  * @param  ApiDoc            $annotation
  * @param  Route             $route
  * @param  \ReflectionMethod $method
  * @return ApiDoc
  */
 protected function extractData(ApiDoc $annotation, Route $route, \ReflectionMethod $method)
 {
     // create a new annotation
     $annotation = clone $annotation;
     // parse annotations
     $this->parseAnnotations($annotation, $route, $method);
     // route
     $annotation->setRoute($route);
     // description
     if (null === $annotation->getDescription()) {
         $comments = explode("\n", $this->commentExtractor->getDocCommentText($method));
         // just set the first line
         $comment = trim($comments[0]);
         $comment = preg_replace("#\n+#", ' ', $comment);
         $comment = preg_replace('#\\s+#', ' ', $comment);
         $comment = preg_replace('#[_`*]+#', '', $comment);
         if ('@' !== substr($comment, 0, 1)) {
             $annotation->setDescription($comment);
         }
     }
     // doc
     $annotation->setDocumentation($this->commentExtractor->getDocCommentText($method));
     // input (populates 'parameters' for the formatters)
     if (null !== ($input = $annotation->getInput())) {
         $parameters = array();
         $normalizedInput = $this->normalizeClassParameter($input);
         foreach ($this->parsers as $parser) {
             if ($parser->supports($normalizedInput)) {
                 $parameters = $parser->parse($normalizedInput);
                 break;
             }
         }
         if ('PUT' === $method) {
             // All parameters are optional with PUT (update)
             array_walk($parameters, function ($val, $key) use(&$data) {
                 $parameters[$key]['required'] = false;
             });
         }
         $annotation->setParameters($parameters);
     }
     // output (populates 'response' for the formatters)
     if (null !== ($output = $annotation->getOutput())) {
         $response = array();
         $normalizedOutput = $this->normalizeClassParameter($output);
         foreach ($this->parsers as $parser) {
             if ($parser->supports($normalizedOutput)) {
                 $response = $parser->parse($normalizedOutput);
                 break;
             }
         }
         $annotation->setResponse($response);
     }
     // requirements
     $requirements = array();
     foreach ($route->getRequirements() as $name => $value) {
         if ('_method' !== $name) {
             $requirements[$name] = array('requirement' => $value, 'dataType' => '', 'description' => '');
         }
         if ('_scheme' == $name) {
             $https = 'https' == $value;
             $annotation->setHttps($https);
         }
     }
     $paramDocs = array();
     foreach (explode("\n", $this->commentExtractor->getDocComment($method)) as $line) {
         if (preg_match('{^@param (.+)}', trim($line), $matches)) {
             $paramDocs[] = $matches[1];
         }
         if (preg_match('{^@deprecated\\b(.*)}', trim($line), $matches)) {
             $annotation->setDeprecated(true);
         }
     }
     $regexp = '{(\\w*) *\\$%s\\b *(.*)}i';
     foreach ($route->compile()->getVariables() as $var) {
         $found = false;
         foreach ($paramDocs as $paramDoc) {
             if (preg_match(sprintf($regexp, preg_quote($var)), $paramDoc, $matches)) {
                 $requirements[$var]['dataType'] = isset($matches[1]) ? $matches[1] : '';
                 $requirements[$var]['description'] = $matches[2];
                 if (!isset($requirements[$var]['requirement'])) {
                     $requirements[$var]['requirement'] = '';
                 }
                 $found = true;
                 break;
             }
         }
         if (!isset($requirements[$var]) && false === $found) {
             $requirements[$var] = array('requirement' => '', 'dataType' => '', 'description' => '');
         }
     }
     $annotation->setRequirements($requirements);
     return $annotation;
 }
Esempio n. 6
0
 public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
 {
     // description
     if (null === $annotation->getDescription()) {
         $comments = explode("\n", $annotation->getDocumentation());
         // just set the first line
         $comment = trim($comments[0]);
         $comment = preg_replace("#\n+#", ' ', $comment);
         $comment = preg_replace('#\\s+#', ' ', $comment);
         $comment = preg_replace('#[_`*]+#', '', $comment);
         if ('@' !== substr($comment, 0, 1)) {
             $annotation->setDescription($comment);
         }
     }
     // schema
     if ($annotation->getSchema() !== null) {
         $path = 'file://' . realpath($this->schemaPath . $annotation->getSchema());
         $properties = (array) json_decode(file_get_contents($path))->properties;
         $tab = [];
         foreach ($properties as $objectName => $property) {
             if (isset($property->properties)) {
                 $tab = array_merge($tab, $annotation->schemaFormat($property->properties, $property->required, $objectName));
             } else {
                 $required = json_decode(file_get_contents($path))->required;
                 $tab = array_merge($tab, $annotation->schemaFormat($properties, $required));
             }
         }
         $annotation->setParameters($tab);
     }
     // requirements
     $requirements = $annotation->getRequirements();
     foreach ($route->getRequirements() as $name => $value) {
         if (!isset($requirements[$name]) && '_method' !== $name && '_scheme' !== $name) {
             $requirements[$name] = array('requirement' => $value, 'dataType' => '', 'description' => '');
         }
         if ('_scheme' === $name) {
             $https = 'https' == $value;
             $annotation->setHttps($https);
         }
     }
     if (method_exists($route, 'getSchemes')) {
         $annotation->setHttps(in_array('https', $route->getSchemes()));
     }
     $paramDocs = array();
     foreach (explode("\n", $this->commentExtractor->getDocComment($method)) as $line) {
         if (preg_match('{^@param (.+)}', trim($line), $matches)) {
             $paramDocs[] = $matches[1];
         }
         if (preg_match('{^@deprecated}', trim($line))) {
             $annotation->setDeprecated(true);
         }
         if (preg_match('{^@link (.+)}', trim($line), $matches)) {
             $annotation->setLink($matches[1]);
         }
     }
     $regexp = '{(\\w*) *\\$%s\\b *(.*)}i';
     foreach ($route->compile()->getVariables() as $var) {
         $found = false;
         foreach ($paramDocs as $paramDoc) {
             if (preg_match(sprintf($regexp, preg_quote($var)), $paramDoc, $matches)) {
                 $annotationRequirements = $annotation->getrequirements();
                 if (!isset($annotationRequirements[$var]['dataType'])) {
                     $requirements[$var]['dataType'] = isset($matches[1]) ? $matches[1] : '';
                 }
                 if (!isset($annotationRequirements[$var]['description'])) {
                     $requirements[$var]['description'] = $matches[2];
                 }
                 if (!isset($requirements[$var]['requirement']) && !isset($annotationRequirements[$var]['requirement'])) {
                     $requirements[$var]['requirement'] = '';
                 }
                 $found = true;
                 break;
             }
         }
         if (!isset($requirements[$var]) && false === $found) {
             $requirements[$var] = array('requirement' => '', 'dataType' => '', 'description' => '');
         }
     }
     $annotation->setRequirements($requirements);
 }
Esempio n. 7
0
 /**
  * Returns a new ApiDoc instance with more data.
  *
  * @param  ApiDoc $annotation
  * @param  Route $route
  * @param  \ReflectionMethod $method
  * @param DunglasResource $dunglasResource
  * @return ApiDoc
  */
 protected function extractData(ApiDoc $annotation, Route $route, \ReflectionMethod $method, DunglasResource $dunglasResource = null)
 {
     //remove methode OPTIONS
     $methods = $route->getMethods();
     $optionIndex = array_search('OPTIONS', $methods);
     if ($optionIndex !== false) {
         unset($methods[$optionIndex]);
         $route->setMethods($methods);
     }
     if (in_array(strtolower($this->versionApi), $this->nelmioDocStandardVersion)) {
         return parent::extractData($annotation, $route, $method);
     }
     // create a new annotation
     $annotation = clone $annotation;
     $annotation->addTag($this->versionApi, '#ff0000');
     // doc
     $annotation->setDocumentation($this->commentExtractor->getDocCommentText($method));
     // parse annotations
     $this->parseAnnotations($annotation, $route, $method);
     $resource = $route->getDefault('_resource');
     // route
     $annotation->setRoute($route);
     $entityClassInput = $entityClassOutput = null;
     //section
     $annotation->setSection($resource);
     $annotation = $this->addFilters($resource, $annotation, $dunglasResource, $route);
     if (in_array($annotation->getMethod(), ['POST', 'PUT'])) {
         $formName = 'api_v2_' . strtolower($dunglasResource->getShortName());
         if ($hasFormtype = $this->registry->hasType($formName)) {
             $type = $this->registry->getType($formName);
             if ($type instanceof ResolvedFormTypeInterface) {
                 $entityClassInput = get_class($type->getInnerType());
                 $dunglasResource->initValidationGroups($type->getInnerType()->validationGrp);
             }
         }
     }
     if ('GET' === $annotation->getMethod()) {
         $entityClassInput = null;
     }
     if (is_null($annotation->getOutput()) || is_array($annotation->getOutput())) {
         $entityClassOutput = $this->transformerHelper->getEntityClass($resource);
     } else {
         $entityClassOutput = $annotation->getOutput();
     }
     if ('DELETE' === $annotation->getMethod()) {
         $entityClassInput = $entityClassOutput = null;
     }
     // input (populates 'parameters' for the formatters)
     if (null !== ($input = $entityClassInput)) {
         $normalizedInput = $this->normalizeClassParameter($input, $dunglasResource);
         $parameters = $this->getParametersParser($normalizedInput, $resource, $dunglasResource, $annotation);
         if ($hasFormtype && in_array($annotation->getMethod(), ['POST', 'PUT'])) {
             $parameters = $this->processFormParams($formName, $parameters, $dunglasResource);
         }
         $parameters = $this->clearClasses($parameters);
         $parameters = $this->generateHumanReadableTypes($parameters);
         if ($annotation->getMethod() === 'POST') {
             if (isset($parameters['id'])) {
                 unset($parameters['id']);
             }
         }
         if (in_array($annotation->getMethod(), ['PUT', 'PATCH'])) {
             // All parameters are optional with PUT (update)
             array_walk($parameters, function ($val, $key) use(&$parameters) {
                 $parameters[$key]['required'] = false;
             });
         }
         $annotation->setParameters($parameters);
     }
     // output (populates 'response' for the formatters)
     if (null !== ($output = $entityClassOutput)) {
         $normalizedOutput = $this->normalizeClassParameter($output, $dunglasResource);
         if (!is_array($annotation->getOutput())) {
             $response = $this->getParametersParser($normalizedOutput, $resource, $dunglasResource, $annotation, 'Output');
             foreach ($response as $key => $params) {
                 if (isset($response[$key]['children'])) {
                     unset($response[$key]['children']);
                 }
                 if ($response[$key]['actualType'] === 'model') {
                     $response[$key]['dataType'] = 'Integer | ' . $response[$key]['dataType'];
                 }
             }
             $response = $this->clearClasses($response);
             $response = $this->generateHumanReadableTypes($response);
         } else {
             $response = $this->normalizeArrayParameter($annotation->getOutput());
         }
         $annotation->setResponse($response);
         $annotation->setResponseForStatusCode($response, $normalizedOutput, 200);
     }
     if (count($annotation->getResponseMap()) > 0) {
         foreach ($annotation->getResponseMap() as $code => $modelName) {
             if (is_array($modelName)) {
                 continue;
             }
             if ('200' === (string) $code && isset($modelName['type']) && isset($modelName['model'])) {
                 /*
                  * Model was already parsed as the default `output` for this ApiDoc.
                  */
                 continue;
             }
             $normalizedModel = $this->normalizeClassParameter($modelName, $dunglasResource);
             $parameters = array();
             $supportedParsers = array();
             foreach ($this->getParsers($normalizedModel) as $parser) {
                 if ($parser->supports($normalizedModel)) {
                     $supportedParsers[] = $parser;
                     $parameters = $this->mergeParameters($parameters, $parser->parse($normalizedModel));
                 }
             }
             foreach ($supportedParsers as $parser) {
                 if ($parser instanceof PostParserInterface) {
                     $mp = $parser->postParse($normalizedModel, $parameters);
                     $parameters = $this->mergeParameters($parameters, $mp);
                 }
             }
             $parameters = $this->clearClasses($parameters);
             $parameters = $this->generateHumanReadableTypes($parameters);
             $annotation->setResponseForStatusCode($parameters, $normalizedModel, $code);
         }
     }
     return $annotation;
 }
Esempio n. 8
0
 /**
  * @param $propertyMetadata
  *
  * @return string
  */
 private function getDescription($propertyMetadata) : string
 {
     return $this->docCommentExtractor->getDocCommentText($propertyMetadata);
 }