|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Helpers\MetaFormats; |
| 4 | + |
| 5 | +use App\Exceptions\InternalServerException; |
| 6 | +use App\Helpers\Swagger\ParenthesesBuilder; |
| 7 | + |
| 8 | +class AnnotationToAttributeConverter |
| 9 | +{ |
| 10 | + /** |
| 11 | + * A regex that matches @Param annotations and captures its parameters. Can capture up to 7 parameters. |
| 12 | + * Contains 6 copies of the following sub-regex: '(?:([a-z]+?=.+?),?\s*\*?\s*)?', which |
| 13 | + * matches 'name=value' assignments followed by an optional comma, whitespace, |
| 14 | + * star (multi-line annotation support), whitespace. The capture contains only 'name=value'. |
| 15 | + * The regex ends with '([a-z]+?=.+)\)', which is similar to the above, but instead of ending with |
| 16 | + * an optional comma etc., it ends with the closing parentheses of the @Param annotation. |
| 17 | + */ |
| 18 | + private static string $postRegex = "/\*\s*@Param\((?:([a-z]+?=.+?),?\s*\*?\s*)?(?:([a-z]+?=.+?),?\s*\*?\s*)?(?:([a-z]+?=.+?),?\s*\*?\s*)?(?:([a-z]+?=.+?),?\s*\*?\s*)?(?:([a-z]+?=.+?),?\s*\*?\s*)?(?:([a-z]+?=.+?),?\s*\*?\s*)?([a-z]+?=.+)\)/"; |
| 19 | + |
| 20 | + /** |
| 21 | + * Converts an array of preg_replace_callback matches to an attribute string. |
| 22 | + * @param array $matches An array of matches, with empty captures as NULL (PREG_UNMATCHED_AS_NULL flag). |
| 23 | + * @return string Returns an attribute string. |
| 24 | + */ |
| 25 | + private static function regexCaptureToAttributeCallback(array $matches) |
| 26 | + { |
| 27 | + // convert the string assignments in $matches to an associative array |
| 28 | + $annotationParameters = []; |
| 29 | + // the first element is the matched string |
| 30 | + for ($i = 1; $i < count($matches); $i++) { |
| 31 | + $capture = $matches[$i]; |
| 32 | + if ($capture === null) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + // the regex extracts the key as the first capture, and the value as the second or third (depends |
| 37 | + // whether the value is enclosed in double quotes) |
| 38 | + $parseResult = preg_match('/([a-z]+)=(?:(?:"(.+?)")|(?:(.+)))/', $capture, $tokens, PREG_UNMATCHED_AS_NULL); |
| 39 | + if ($parseResult !== 1) { |
| 40 | + throw new InternalServerException("Unexpected assignment format: $capture"); |
| 41 | + } |
| 42 | + |
| 43 | + $key = $tokens[1]; |
| 44 | + $value = $tokens[2] ?? $tokens[3]; |
| 45 | + $annotationParameters[$key] = $value; |
| 46 | + } |
| 47 | + |
| 48 | + // serialize the parameters to an attribute |
| 49 | + $parenthesesBuilder = new ParenthesesBuilder(); |
| 50 | + |
| 51 | + // add type |
| 52 | + $typeStr = $annotationParameters["type"]; |
| 53 | + $type = null; |
| 54 | + switch ($typeStr) { |
| 55 | + case "post": |
| 56 | + $type = "RequestParamType::Post"; |
| 57 | + break; |
| 58 | + case "query": |
| 59 | + $type = "RequestParamType::Query"; |
| 60 | + break; |
| 61 | + default: |
| 62 | + throw new InternalServerException("Unknown request type: $typeStr"); |
| 63 | + } |
| 64 | + $parenthesesBuilder->addValue($type); |
| 65 | + |
| 66 | + // add name |
| 67 | + if (!array_key_exists("name", $annotationParameters)) { |
| 68 | + throw new InternalServerException("Missing name parameter."); |
| 69 | + } |
| 70 | + $parenthesesBuilder->addValue("\"{$annotationParameters["name"]}\""); |
| 71 | + |
| 72 | + if (array_key_exists("description", $annotationParameters)) { |
| 73 | + $parenthesesBuilder->addValue("description: \"{$annotationParameters["description"]}\""); |
| 74 | + } |
| 75 | + |
| 76 | + if (array_key_exists("validation", $annotationParameters)) { |
| 77 | + ///TODO |
| 78 | + $parenthesesBuilder->addValue("validation: [ \"{$annotationParameters["validation"]}\" ]"); |
| 79 | + } |
| 80 | + |
| 81 | + if (array_key_exists("required", $annotationParameters)) { |
| 82 | + $parenthesesBuilder->addValue("required: " . $annotationParameters["required"]); |
| 83 | + } |
| 84 | + |
| 85 | + if (!array_key_exists("type", $annotationParameters)) { |
| 86 | + throw new InternalServerException("Missing type parameter."); |
| 87 | + } |
| 88 | + |
| 89 | + return "#[ParamAttribute{$parenthesesBuilder->toString()}]"; |
| 90 | + } |
| 91 | + |
| 92 | + public static function convertFile(string $path) |
| 93 | + { |
| 94 | + // read file and replace @Param annotations with attributes |
| 95 | + $content = file_get_contents($path); |
| 96 | + $withInterleavedAttributes = preg_replace_callback(self::$postRegex, function ($matches) { |
| 97 | + return self::regexCaptureToAttributeCallback($matches); |
| 98 | + }, $content, -1, $count, PREG_UNMATCHED_AS_NULL); |
| 99 | + |
| 100 | + // move the attribute lines below the comment block |
| 101 | + $lines = []; |
| 102 | + $attributeLinesBuffer = []; |
| 103 | + $usingsAdded = false; |
| 104 | + foreach (preg_split("/((\r?\n)|(\r\n?))/", $withInterleavedAttributes) as $line) { |
| 105 | + // add usings for new types |
| 106 | + if (!$usingsAdded && strlen($line) > 3 && substr($line, 0, 3) === "use") { |
| 107 | + $lines[] = "use App\Helpers\MetaFormats\Attributes\ParamAttribute;"; |
| 108 | + $lines[] = "use App\Helpers\MetaFormats\RequestParamType;"; |
| 109 | + $lines[] = $line; |
| 110 | + $usingsAdded = true; |
| 111 | + // store attribute lines in the buffer and do not write them |
| 112 | + } elseif (preg_match("/#\[ParamAttribute/", $line) === 1) { |
| 113 | + $attributeLinesBuffer[] = $line; |
| 114 | + // flush attribute lines |
| 115 | + } elseif (trim($line) === "*/") { |
| 116 | + $lines[] = $line; |
| 117 | + foreach ($attributeLinesBuffer as $attributeLine) { |
| 118 | + // the attribute lines are shifted by one space to the right (due to the comment block origin) |
| 119 | + $lines[] = substr($attributeLine, 1); |
| 120 | + } |
| 121 | + $attributeLinesBuffer = []; |
| 122 | + } else { |
| 123 | + $lines[] = $line; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + ///TODO: add usings for used validators |
| 128 | + ///TODO: handle too long lines |
| 129 | + return implode("\n", $lines); |
| 130 | + } |
| 131 | +} |
0 commit comments