Skip to content

Commit

Permalink
add testing api using docs functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Jan 21, 2022
1 parent 2b6a8f9 commit f2c18c7
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 66 deletions.
31 changes: 18 additions & 13 deletions Actions/GenerateDocumentationAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
11 changes: 11 additions & 0 deletions Configs/vendor-documentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,15 @@
*/

'access-private-docs-roles' => [],

/*
|--------------------------------------------------------------------------
| Enable Sending Sample Request
|--------------------------------------------------------------------------
|
| Enable sending of sample request
|
*/

'enable-sending-sample-request' => true,
];
2 changes: 1 addition & 1 deletion Tasks/GenerateAPIDocsTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function run($type, $console): string
"app",
"-o",
$path,
"--single"
"--single",
];

$process = new Process($command);
Expand Down
53 changes: 53 additions & 0 deletions Tasks/RenderApidocJsonTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Containers\Vendor\Documentation\Tasks;

use App\Ship\Parents\Tasks\Task;

class RenderApidocJsonTask extends Task
{
private string $templatePath;
private string $outputPath;
// ['templateKey' => 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;
}
}
105 changes: 53 additions & 52 deletions Tasks/RenderTemplatesTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit f2c18c7

Please sign in to comment.