From f2c18c7badfa032218ab084093ae1b69dd021b1e Mon Sep 17 00:00:00 2001 From: Mohammad Alavi Date: Fri, 21 Jan 2022 20:53:52 +0330 Subject: [PATCH] add testing api using docs functionality --- Actions/GenerateDocumentationAction.php | 31 ++++--- Configs/vendor-documentation.php | 11 +++ Tasks/GenerateAPIDocsTask.php | 2 +- Tasks/RenderApidocJsonTask.php | 53 ++++++++++++ Tasks/RenderTemplatesTask.php | 105 ++++++++++++------------ 5 files changed, 136 insertions(+), 66 deletions(-) create mode 100644 Tasks/RenderApidocJsonTask.php diff --git a/Actions/GenerateDocumentationAction.php b/Actions/GenerateDocumentationAction.php index 2eae59d..497ea43 100644 --- a/Actions/GenerateDocumentationAction.php +++ b/Actions/GenerateDocumentationAction.php @@ -4,27 +4,32 @@ use App\Containers\Vendor\Documentation\Tasks\GenerateAPIDocsTask; use App\Containers\Vendor\Documentation\Tasks\GetAllDocsTypesTask; +use App\Containers\Vendor\Documentation\Tasks\RenderApidocJsonTask; use App\Containers\Vendor\Documentation\Tasks\RenderTemplatesTask; use App\Containers\Vendor\Documentation\UI\CLI\Commands\GenerateApiDocsCommand; use App\Ship\Parents\Actions\Action; class GenerateDocumentationAction extends Action { - public function run(GenerateApiDocsCommand $console): void - { - // parse the markdown file. - app(RenderTemplatesTask::class)->run(); + public function run(GenerateApiDocsCommand $console): void + { + // get docs types that needs to be generated by the user base on his configs. + $types = app(GetAllDocsTypesTask::class)->run(); - // get docs types that needs to be generated by the user base on his configs. - $types = app(GetAllDocsTypesTask::class)->run(); + foreach ($types as $type) { + app(RenderApidocJsonTask::class, ['docType' => $type])->run(); + } - $console->info("Generating API Documentations for (" . implode(' & ', $types) . ")\n"); + // parse the markdown file. + app(RenderTemplatesTask::class)->run(); - // for each type, generate docs. - $documentationUrls = array_map(static function ($type) use ($console) { - return app(GenerateAPIDocsTask::class)->run($type, $console); - }, $types); + $console->info("Generating API Documentations for (" . implode(' & ', $types) . ")\n"); - $console->info("Done! You can access your API Docs at: \n" . implode("\n", $documentationUrls)); - } + // for each type, generate docs. + $documentationUrls = array_map(static function ($type) use ($console) { + return app(GenerateAPIDocsTask::class)->run($type, $console); + }, $types); + + $console->info("Done! You can access your API Docs at: \n" . implode("\n", $documentationUrls)); + } } diff --git a/Configs/vendor-documentation.php b/Configs/vendor-documentation.php index 55cc63c..73f1db1 100644 --- a/Configs/vendor-documentation.php +++ b/Configs/vendor-documentation.php @@ -104,4 +104,15 @@ */ 'access-private-docs-roles' => [], + + /* + |-------------------------------------------------------------------------- + | Enable Sending Sample Request + |-------------------------------------------------------------------------- + | + | Enable sending of sample request + | + */ + + 'enable-sending-sample-request' => true, ]; diff --git a/Tasks/GenerateAPIDocsTask.php b/Tasks/GenerateAPIDocsTask.php index 6735ed5..9cef8d3 100644 --- a/Tasks/GenerateAPIDocsTask.php +++ b/Tasks/GenerateAPIDocsTask.php @@ -24,7 +24,7 @@ public function run($type, $console): string "app", "-o", $path, - "--single" + "--single", ]; $process = new Process($command); diff --git a/Tasks/RenderApidocJsonTask.php b/Tasks/RenderApidocJsonTask.php new file mode 100644 index 0000000..d92de26 --- /dev/null +++ b/Tasks/RenderApidocJsonTask.php @@ -0,0 +1,53 @@ + value] + private array $replaceArray; + + public function __construct(string $docType) + { + $this->templatePath = 'Containers/' . config('vendor-documentation.section_name') . '/Documentation/ApiDocJs/' . $docType . '/apidoc.json'; + $this->outputPath = 'Containers/' . config('vendor-documentation.section_name') . '/Documentation/ApiDocJs/' . $docType . '/apidoc.json'; + $this->replaceArray = [ + 'name' => config('app.name'), + 'description' => config('app.name') . ' (' . ucfirst($docType) . ' API) Documentation', + 'title' => 'Welcome to ' . config('app.name'), + 'url' => config('apiato.api.url'), + 'sampleUrl' => config('vendor-documentation.enable-sending-sample-request') ? config('apiato.api.url') : null, + ]; + } + + /** + * Read the markdown header template and fill it with some real data from the .env file. + */ + public function run(): string + { + // read the template file + $jsonContent = file_get_contents(app_path($this->templatePath)); + + //Decode the JSON data into a PHP array. + $contentsDecoded = json_decode($jsonContent, true); + + //Modify the variables. + foreach ($this->replaceArray as $key => $value) { + $contentsDecoded[$key] = $value; + } + + //Encode the array back into a JSON string. + $jsonContent = json_encode($contentsDecoded); + + // this is what the apidoc.json file will point to to load the header.md + // write the actual file + $path = app_path($this->outputPath); + file_put_contents($path, $jsonContent); + + return $path; + } +} diff --git a/Tasks/RenderTemplatesTask.php b/Tasks/RenderTemplatesTask.php index 59a270e..5ac1e50 100644 --- a/Tasks/RenderTemplatesTask.php +++ b/Tasks/RenderTemplatesTask.php @@ -7,65 +7,66 @@ class RenderTemplatesTask extends Task { - private $headerMarkdownContent; - private string $templatePath; - private string $outputPath; - // ['templateKey' => value] - private array $replaceArray; + private $headerMarkdownContent; + private string $templatePath; + private string $outputPath; + // ['templateKey' => value] + private array $replaceArray; - public function __construct() - { - $this->templatePath = 'Containers/' . config('vendor-documentation.section_name') . '/Documentation/ApiDocJs/shared/header.template.md'; - $this->outputPath = 'Containers/' . config('vendor-documentation.section_name') . '/Documentation/UI/WEB/Views/documentation/header.md'; - $this->replaceArray = [ - 'api.domain.test' => config('apiato.api.url'), - '{{rate-limit-expires}}' => config('apiato.api.throttle.expires'), - '{{rate-limit-attempts}}' => config('apiato.api.throttle.attempts'), - '{{access-token-expires-in}}' => $this->minutesToTimeDisplay(config('apiato.api.expires-in')), - '{{access-token-expires-in-minutes}}' => config('apiato.api.expires-in'), - '{{refresh-token-expires-in}}' => $this->minutesToTimeDisplay(config('apiato.api.refresh-expires-in')), - '{{refresh-token-expires-in-minutes}}' => config('apiato.api.refresh-expires-in'), - '{{pagination-limit}}' => config('repository.pagination.limit') - ]; - } + public function __construct() + { + $this->templatePath = 'Containers/' . config('vendor-documentation.section_name') . '/Documentation/ApiDocJs/shared/header.template.md'; + $this->outputPath = 'Containers/' . config('vendor-documentation.section_name') . '/Documentation/UI/WEB/Views/documentation/header.md'; + $this->replaceArray = [ + 'api.domain.test' => config('apiato.api.url'), + '{{rate-limit-expires}}' => config('apiato.api.throttle.expires'), + '{{rate-limit-attempts}}' => config('apiato.api.throttle.attempts'), + '{{access-token-expires-in}}' => $this->minutesToTimeDisplay(config('apiato.api.expires-in')), + '{{access-token-expires-in-minutes}}' => config('apiato.api.expires-in'), + '{{refresh-token-expires-in}}' => $this->minutesToTimeDisplay(config('apiato.api.refresh-expires-in')), + '{{refresh-token-expires-in-minutes}}' => config('apiato.api.refresh-expires-in'), + '{{pagination-limit}}' => config('repository.pagination.limit'), + ]; + } - private function minutesToTimeDisplay($minutes): string - { - $seconds = $minutes * 60; + private function minutesToTimeDisplay($minutes): string + { + $seconds = $minutes * 60; - $dtF = new DateTime('@0'); - $dtT = new DateTime("@$seconds"); + $dtF = new DateTime('@0'); + $dtT = new DateTime("@$seconds"); - return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds'); - } + return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds'); + } - /** - * Read the markdown header template and fill it with some real data from the .env file. - */ - public function run(): string - { - // read the template file - $this->headerMarkdownContent = file_get_contents(app_path($this->templatePath)); - $this->headerMarkdownContent = $this->replaceMarkdownContent($this->headerMarkdownContent, $this->replaceArray); + /** + * Read the markdown header template and fill it with some real data from the .env file. + */ + public function run(): string + { + // read the template file + $this->headerMarkdownContent = file_get_contents(app_path($this->templatePath)); + $this->headerMarkdownContent = $this->replaceMarkdownContent($this->headerMarkdownContent, $this->replaceArray); - // this is what the apidoc.json file will point to to load the header.md - // write the actual file - $path = app_path($this->outputPath); - file_put_contents($path, $this->headerMarkdownContent); + // this is what the apidoc.json file will point to to load the header.md + // write the actual file + $path = app_path($this->outputPath); + file_put_contents($path, $this->headerMarkdownContent); - return $path; - } + return $path; + } - private function replaceMarkdownContent($markdownContent, array $replaceArray) - { - foreach ($replaceArray as $key => $value) { - $markdownContent = $this->replace($markdownContent, $key, $value); - } - return $markdownContent; - } + private function replaceMarkdownContent($markdownContent, array $replaceArray) + { + foreach ($replaceArray as $key => $value) { + $markdownContent = $this->replace($markdownContent, $key, $value); + } - private function replace($markdownContent, $templateKey, $value) - { - return str_replace($templateKey, $value, $markdownContent); - } + return $markdownContent; + } + + private function replace($markdownContent, $templateKey, $value) + { + return str_replace($templateKey, $value, $markdownContent); + } }