Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#53 minime annotations remove #36

Merged
merged 3 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ronasit/laravel-swagger",
"description": "Provided middleware for generating of swagger-documentation file by run testing of RESTful API.",
"description": "Provided middleware for generating of swagger-documentation file by run testing of restful API.",
"keywords": [
"laravel",
"swagger",
Expand All @@ -17,8 +17,7 @@
],
"require": {
"php": ">=7.1.0",
"laravel/framework": ">=5.3.0",
"minime/annotations": "~3.0"
"laravel/framework": ">=5.3.0"
},
"autoload": {
"psr-4": {
Expand Down
83 changes: 43 additions & 40 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

namespace RonasIT\Support\AutoDoc\Services;

use ReflectionClass;
use Illuminate\Container\Container;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Minime\Annotations\Interfaces\AnnotationsBagInterface;
use Minime\Annotations\Reader as AnnotationReader;
use Minime\Annotations\Parser;
use Minime\Annotations\Cache\ArrayCache;
use RonasIT\Support\AutoDoc\Interfaces\DataCollectorInterface;
use RonasIT\Support\AutoDoc\Traits\GetDependenciesTrait;
use RonasIT\Support\AutoDoc\Exceptions\WrongSecurityConfigException;
Expand All @@ -25,7 +22,6 @@ class SwaggerService
{
use GetDependenciesTrait;

protected $annotationReader;
protected $dataCollector;

protected $data;
Expand All @@ -46,8 +42,6 @@ public function __construct(Container $container)
if (config('app.env') == 'testing') {
$this->container = $container;

$this->annotationReader = new AnnotationReader(new Parser, new ArrayCache);;

$this->security = config('auto-doc.security');

$this->data = $this->dataCollector->getTmpData();
Expand Down Expand Up @@ -149,7 +143,7 @@ public function addData(Request $request, $response)

$this->prepareItem();

$this->parseRequest($request);
$this->parseRequest();
$this->parseResponse($response);

$this->dataCollector->saveTmpData($this->data);
Expand Down Expand Up @@ -209,7 +203,7 @@ protected function getPathParams()
return $result;
}

protected function parseRequest($request)
protected function parseRequest()
{
$this->saveConsume();
$this->saveTags();
Expand All @@ -223,7 +217,7 @@ protected function parseRequest($request)
return;
}

$annotations = $this->annotationReader->getClassAnnotations($concreteRequest);
$annotations = $this->getClassAnnotations($concreteRequest);

$this->saveParameters($concreteRequest, $annotations);
$this->saveDescription($concreteRequest, $annotations);
Expand Down Expand Up @@ -278,11 +272,11 @@ protected function makeResponseExample($content, $mimeType, $description = '')

if ($mimeType === 'application/json') {
$responseExample['schema'] = [
'example' => json_decode($content, true),
'example' => json_decode($content, true)
];
} elseif ($mimeType === 'application/pdf') {
$responseExample['schema'] = [
'example' => base64_encode($content),
'example' => base64_encode($content)
];
} else {
$responseExample['examples']['example'] = $content;
Expand All @@ -291,7 +285,7 @@ protected function makeResponseExample($content, $mimeType, $description = '')
return $responseExample;
}

protected function saveParameters($request, AnnotationsBagInterface $annotations)
protected function saveParameters($request, array $annotations)
{
$formRequest = new $request;
$formRequest->setUserResolver($this->request->getUserResolver());
Expand All @@ -307,12 +301,12 @@ protected function saveParameters($request, AnnotationsBagInterface $annotations
}
}

protected function saveGetRequestParameters($rules, AnnotationsBagInterface $annotations)
protected function saveGetRequestParameters($rules, array $annotations)
{
foreach ($rules as $parameter => $rule) {
$validation = explode('|', $rule);

$description = $annotations->get($parameter, implode(', ', $validation));
$description = Arr::get($annotations, $parameter, implode(', ', $validation));

$existedParameter = Arr::first($this->item['parameters'], function ($existedParameter, $key) use ($parameter) {
return $existedParameter['name'] == $parameter;
Expand All @@ -334,7 +328,7 @@ protected function saveGetRequestParameters($rules, AnnotationsBagInterface $ann
}
}

protected function savePostRequestParameters($actionName, $rules, AnnotationsBagInterface $annotations)
protected function savePostRequestParameters($actionName, $rules, array $annotations)
{
if ($this->requestHasMoreProperties($actionName)) {
if ($this->requestHasBody()) {
Expand All @@ -353,19 +347,14 @@ protected function savePostRequestParameters($actionName, $rules, AnnotationsBag
}
}

protected function saveDefinitions($objectName, $rules, $annotations)
protected function saveDefinitions($objectName, $rules, array $annotations)
{
$data = [
'type' => 'object',
'properties' => []
];
foreach ($rules as $parameter => $rule) {
$rulesArray = $rule;

if (!is_array($rule)) {
$rulesArray = explode('|', $rule);
}

$rulesArray = (is_array($rule)) ? $rule : explode('|', $rule);
$parameterType = $this->getParameterType($rulesArray);
$this->saveParameterType($data, $parameter, $parameterType);
$this->saveParameterDescription($data, $parameter, $rulesArray, $annotations);
Expand Down Expand Up @@ -411,9 +400,9 @@ protected function saveParameterType(&$data, $parameter, $parameterType)
];
}

protected function saveParameterDescription(&$data, $parameter, array $rulesArray, AnnotationsBagInterface $annotations)
protected function saveParameterDescription(&$data, $parameter, array $rulesArray, array $annotations)
{
$description = $annotations->get($parameter, implode(', ', $rulesArray));
$description = Arr::get($annotations, $parameter, implode(', ', $rulesArray));
$data['properties'][$parameter]['description'] = $description;
}

Expand Down Expand Up @@ -487,11 +476,11 @@ public function saveTags()
$this->item['tags'] = [$tag];
}

public function saveDescription($request, AnnotationsBagInterface $annotations)
public function saveDescription($request, array $annotations)
{
$this->item['summary'] = $this->getSummary($request, $annotations);

$description = $annotations->get('description');
$description = Arr::get($annotations, 'description');

if (!empty($description)) {
$this->item['description'] = $description;
Expand All @@ -515,9 +504,9 @@ protected function addSecurityToOperation()
}
}

protected function getSummary($request, AnnotationsBagInterface $annotations)
protected function getSummary($request, array $annotations)
{
$summary = $annotations->get('summary');
$summary = Arr::get($annotations, 'summary');

if (empty($summary)) {
$summary = $this->parseRequestName($request);
Expand Down Expand Up @@ -561,7 +550,9 @@ function () use ($request, $code) {
return empty($request) ? Response::$statusTexts[$code] : null;
},
function () use ($request, $code) {
return $this->annotationReader->getClassAnnotations($request)->get("_{$code}");
$annotations = $this->getClassAnnotations($request);

return Arr::get($annotations, "_{$code}");
},
function () use ($code) {
return config("auto-doc.defaults.code-descriptions.{$code}");
Expand Down Expand Up @@ -594,9 +585,7 @@ public function saveProductionData()

public function getDocFileContent()
{
$data = $this->dataCollector->getDocumentation();

return $data;
return $this->dataCollector->getDocumentation();
}

protected function camelCaseToUnderScore($input)
Expand Down Expand Up @@ -696,15 +685,29 @@ protected function prepareInfo($info)
return $info;
}

protected function throwTraitMissingException()
protected function getClassAnnotations($class): array
{
$message = "ERROR:\n" .
"It looks like you did not add AutoDocRequestTrait to your requester. \n" .
"Please add it or mark in the test that you do not want to collect the \n" .
"documentation for this case using the skipDocumentationCollecting() method\n";
$reflection = new ReflectionClass($class);

$annotations = $reflection->getDocComment();

fwrite(STDERR, print_r($message, TRUE));
$blocks = explode("\n", $annotations);

die;
$result = [];

foreach ($blocks as $block) {
if (Str::contains($block, '@')) {
$index = strpos($block, '@');
$block = substr($block, $index);
$exploded = explode(' ', $block);

$paramName = str_replace('@', '', array_shift($exploded));
$paramValue = implode(' ', $exploded);

$result[$paramName] = $paramValue;
}
}

return $result;
}
}
13 changes: 0 additions & 13 deletions src/Tests/AutoDocTestCase.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/Traits/AutoDocRequestTrait.php

This file was deleted.