- Notifications
You must be signed in to change notification settings - Fork2.5k
feat: improves PHP generation.#7513
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
9a3a395d85685014ef925efb1d57f697b9eFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Signed-off-by: José Carlos Chávez <jcchavezs@gmail.com>
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -34,6 +34,9 @@ | ||
| import java.util.ResourceBundle; | ||
| import java.util.stream.Collectors; | ||
| /* | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
| ||
| * Generates PHP API code for PHP 7.4+ | ||
| */ | ||
| public class PhpAPIGenerator extends AbstractAPIGenerator { | ||
| private static final String DEFAULT_OUTPUT_DIR = "../zap-api-php/src/Zap/"; | ||
| @@ -110,27 +113,27 @@ private void generatePhpElement(ApiElement element, String component, String typ | ||
| String desc = getMessages().getString(descTag); | ||
| String commentBlock = ""; | ||
| if (!desc.isEmpty()) { | ||
| commentBlock = " * " + desc + "\n"; | ||
| } | ||
| if (isOptional()) { | ||
| commentBlock += " * " + OPTIONAL_MESSAGE + "\n"; | ||
| } | ||
| if (!commentBlock.isEmpty()) { | ||
| out.write("/**\n" + commentBlock + " */\n"); | ||
| } | ||
| } catch (Exception e) { | ||
| // Might not be set, so just print out the ones that are missing | ||
| System.out.println("No i18n for: " + descTag); | ||
| if (isOptional()) { | ||
| out.write("/**\n"); | ||
| out.write(" * " + OPTIONAL_MESSAGE + "\n"); | ||
| out.write(" */\n"); | ||
| } | ||
| } | ||
| out.write(" public function " + createMethodName(element.getName()) + "("); | ||
| out.write( | ||
| element.getParameters().stream() | ||
| @@ -181,17 +184,17 @@ private void generatePhpElement(ApiElement element, String component, String typ | ||
| .filter(e -> !e.isRequired()) | ||
| .collect(Collectors.toList()); | ||
| if (!optionalParameters.isEmpty()) { | ||
| out.write("$params = "); | ||
| out.write(reqParams.toString()); | ||
| out.write(";\n"); | ||
| reqParams.replace(0, reqParams.length(), "$params"); | ||
| for (ApiParameter parameter : optionalParameters) { | ||
| String name = parameter.getName(); | ||
| String varName = name.toLowerCase(Locale.ROOT); | ||
Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Switched tabs into spaces as that is what is being used by standards. In any case, all the code is reformatted by proper PHP linters in the PHP API client. | ||
| out.write(" if ($" + varName + " !== NULL) {\n"); | ||
| out.write("$params['" + name + "'] = $" + varName + ";\n"); | ||
| out.write("}\n"); | ||
| } | ||
| } | ||
| @@ -203,7 +206,7 @@ private void generatePhpElement(ApiElement element, String component, String typ | ||
| } | ||
| out.write( | ||
| " return $this->zap->" | ||
| + method | ||
| + "($this->zap->" | ||
| + baseUrl | ||
| @@ -220,14 +223,14 @@ private void generatePhpElement(ApiElement element, String component, String typ | ||
| out.write(")"); | ||
| if (type.equals(VIEW_ENDPOINT)) { | ||
| if (element.getName().startsWith("option")) { | ||
| out.write("->" + element.getName().substring(6) +" ?? null;\n"); | ||
| } else { | ||
| out.write("->" + element.getName() + " ?? null;\n"); | ||
| } | ||
| } else { | ||
| out.write(";\n"); | ||
| } | ||
| out.write("}\n\n"); | ||
| } | ||
| private static String createMethodName(String name) { | ||
| @@ -260,11 +263,11 @@ protected void generateAPIFiles(ApiImplementor imp) throws IOException { | ||
| out.write(" */\n"); | ||
| out.write("class " + className + " {\n\n"); | ||
| out.write(" private Zap $zap;\n\n"); | ||
| out.write(" public function __construct (Zap $zap) {\n"); | ||
| out.write("$this->zap = $zap;\n"); | ||
| out.write("}\n\n"); | ||
| for (ApiElement view : imp.getApiViews()) { | ||
| this.generatePhpElement(view, imp.getPrefix(), VIEW_ENDPOINT, out); | ||