From b719f27bd11e9ee1ebe7fc22b7dee697980fd63e Mon Sep 17 00:00:00 2001 From: Max Gaurav Date: Fri, 19 Jun 2020 12:04:00 +0530 Subject: [PATCH 1/4] add: nested reponse file tag replacement for json files --- .../Responses/UseResponseFileTag.php | 59 ++++++++++++++++--- 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/src/Extracting/Strategies/Responses/UseResponseFileTag.php b/src/Extracting/Strategies/Responses/UseResponseFileTag.php index 007f4fc1..e40581ae 100644 --- a/src/Extracting/Strategies/Responses/UseResponseFileTag.php +++ b/src/Extracting/Strategies/Responses/UseResponseFileTag.php @@ -20,12 +20,17 @@ class UseResponseFileTag extends Strategy * @param array $routeRules * @param array $context * + * @return array|null * @throws \Exception If the response file does not exist * - * @return array|null */ - public function __invoke(Route $route, \ReflectionClass $controller, \ReflectionMethod $method, array $routeRules, array $context = []) - { + public function __invoke( + Route $route, + \ReflectionClass $controller, + \ReflectionMethod $method, + array $routeRules, + array $context = [] + ) { $docBlocks = RouteDocBlocker::getDocBlocksFromRoute($route); /** @var DocBlock $methodDocBlock */ $methodDocBlock = $docBlocks['method']; @@ -57,17 +62,57 @@ protected function getFileResponses(array $tags) preg_match('/^(\d{3})?\s?([\S]*[\s]*?)(\{.*\})?$/', $responseFileTag->getContent(), $result); $relativeFilePath = trim($result[2]); $filePath = storage_path($relativeFilePath); - if (! file_exists($filePath)) { + if (!file_exists($filePath)) { throw new \Exception('@responseFile ' . $relativeFilePath . ' does not exist'); } $status = $result[1] ?: 200; $content = $result[2] ? file_get_contents($filePath, true) : '{}'; - $json = ! empty($result[3]) ? str_replace("'", '"', $result[3]) : '{}'; + $json = !empty($result[3]) ? str_replace("'", '"', $result[3]) : '{}'; $merged = array_merge(json_decode($content, true), json_decode($json, true)); - - return ['content' => json_encode($merged), 'status' => (int) $status]; + $content = json_encode($merged); + $contentWithReplacedTags = $this->replaceJsonFileTags($content); + return ['content' => $content, 'status' => (int)$status]; }, $responseFileTags); return $responses; } + + /** + * Replaces nested file tags @responseFile:path/to/file.json + * + * @param string $content + * @return string + */ + protected function replaceJsonFileTags(string $content): string + { + // finding all matching responseFile tags + preg_match_all('/@responseFile:[\S]*[\s]*\.json?/', $content, $result); + + // continuing if we get any result + if (count($result) > 0) { + + foreach ($result[0] as $replaceValuePath) { + + $relativeFilePath = str_replace('@responseFile:', '', $replaceValuePath); + $relativeFilePath = str_replace('\\', '', $relativeFilePath); + $filePath = storage_path($relativeFilePath); + + if (!file_exists($filePath)) { + throw new \Exception('@responseFile ' . $relativeFilePath . ' does not exist'); + } + + // fetching the file content and recursively replacing any matches within the file tagged + $fileContent = file_get_contents($filePath, true); + $normalizedFileContent = json_encode(json_decode($fileContent, true)); + $nestedReplacedFileContent = $this->replaceJsonFileTags($normalizedFileContent); + $content = str_replace( + '"' . $replaceValuePath . '"', + $nestedReplacedFileContent, + $content + ); + } + } + + return $content; + } } From 9bc31f1f726eb46874fb77ea05c9a0595d70c6fd Mon Sep 17 00:00:00 2001 From: Max Gaurav Date: Fri, 19 Jun 2020 12:10:57 +0530 Subject: [PATCH 2/4] add: documentation for nested file replacement --- docs/getting-started/documenting-your-api.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/docs/getting-started/documenting-your-api.md b/docs/getting-started/documenting-your-api.md index c11a4600..847f42ac 100644 --- a/docs/getting-started/documenting-your-api.md +++ b/docs/getting-started/documenting-your-api.md @@ -337,6 +337,25 @@ public function getUser(int $id) { // ... } + +``` + +The package can also replace nested response file tags provided within the json files. + +Just like doc block add `@responseFile` tag within the json content separated with by a colon and proceeded by the file to use + +Main File: +```json +{"id":5,"name":"Jessica Jones","gender":"female", "profile": "@responseFile:responses/profile/profile.json"} +``` +The reference file: +```json +{"id":1,"active":true,"userId":5} +``` + +The Final Result: +```json +{"id":5,"name":"Jessica Jones","gender":"female", "profile": {"id":1,"active":true,"userId":5}} ``` ## Generating responses automatically From e52bf33bc46f7a4496045d122ef95877175bd4d3 Mon Sep 17 00:00:00 2001 From: Max Gaurav Date: Fri, 19 Jun 2020 12:28:18 +0530 Subject: [PATCH 3/4] fix: incorrect content variable --- src/Extracting/Strategies/Responses/UseResponseFileTag.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Extracting/Strategies/Responses/UseResponseFileTag.php b/src/Extracting/Strategies/Responses/UseResponseFileTag.php index e40581ae..295722e0 100644 --- a/src/Extracting/Strategies/Responses/UseResponseFileTag.php +++ b/src/Extracting/Strategies/Responses/UseResponseFileTag.php @@ -71,7 +71,7 @@ protected function getFileResponses(array $tags) $merged = array_merge(json_decode($content, true), json_decode($json, true)); $content = json_encode($merged); $contentWithReplacedTags = $this->replaceJsonFileTags($content); - return ['content' => $content, 'status' => (int)$status]; + return ['content' => $contentWithReplacedTags, 'status' => (int)$status]; }, $responseFileTags); return $responses; From a17115d001bbe600b10d12a28a2fcf13e3de65b5 Mon Sep 17 00:00:00 2001 From: Max Gaurav Date: Fri, 19 Jun 2020 16:52:15 +0530 Subject: [PATCH 4/4] fix: style errors --- src/Extracting/Strategies/Responses/UseResponseFileTag.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Extracting/Strategies/Responses/UseResponseFileTag.php b/src/Extracting/Strategies/Responses/UseResponseFileTag.php index 295722e0..7719696d 100644 --- a/src/Extracting/Strategies/Responses/UseResponseFileTag.php +++ b/src/Extracting/Strategies/Responses/UseResponseFileTag.php @@ -90,9 +90,7 @@ protected function replaceJsonFileTags(string $content): string // continuing if we get any result if (count($result) > 0) { - foreach ($result[0] as $replaceValuePath) { - $relativeFilePath = str_replace('@responseFile:', '', $replaceValuePath); $relativeFilePath = str_replace('\\', '', $relativeFilePath); $filePath = storage_path($relativeFilePath);