Skip to content

Commit 44f34cc

Browse files
committed
feat (PHP LARAVEL) 8417: initial PHP-laravel codegen integration
1 parent deb5dca commit 44f34cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2515
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ samples/client/petstore/silex/SwaggerServer/venodr/
9696
samples/server/petstore/php-symfony/SymfonyBundle-php/Tests/cache/
9797
samples/server/petstore/php-symfony/SymfonyBundle-php/Tests/logs/
9898

99+
#PHP-laravel
100+
samples/server/petstore/php-laravel/node_modules
101+
samples/server/petstore/php-laravel/vendor
99102

100103
# Perl
101104
samples/client/petstore/perl/deep_module_test/

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,20 @@ Once built, `run-in-docker.sh` will act as an executable for openapi-generator-c
276276
-g go -o /gen/out/go-petstore -DpackageName=petstore # generates go client, outputs locally to ./out/go-petstore
277277
```
278278

279+
##### Troubleshooting
280+
281+
If an error like this occurs, just execute the **mvn clean install -U** command:
282+
283+
> org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project openapi-generator: A type incompatibility occurred while executing org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test: java.lang.ExceptionInInitializerError cannot be cast to java.io.IOException
284+
285+
```sh
286+
./run-in-docker.sh mvn clean install -U
287+
```
288+
289+
> Failed to execute goal org.fortasoft:gradle-maven-plugin:1.0.8:invoke (default) on project openapi-generator-gradle-plugin-mvn-wrapper: org.gradle.tooling.BuildException: Could not execute build using Gradle distribution 'https://services.gradle.org/distributions/gradle-4.7-bin.zip'
290+
291+
Right now: no solution for this one :|
292+
279293
#### Run Docker in Vagrant
280294
Prerequisite: install [Vagrant](https://www.vagrantup.com/downloads.html) and [VirtualBox](https://www.virtualbox.org/wiki/Downloads).
281295
```sh
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
SCRIPT="$0"
4+
echo "# START SCRIPT: $SCRIPT"
5+
6+
while [ -h "$SCRIPT" ] ; do
7+
ls=`ls -ld "$SCRIPT"`
8+
link=`expr "$ls" : '.*-> \(.*\)$'`
9+
if expr "$link" : '/.*' > /dev/null; then
10+
SCRIPT="$link"
11+
else
12+
SCRIPT=`dirname "$SCRIPT"`/"$link"
13+
fi
14+
done
15+
16+
# Make sure that the working directory is the root dir
17+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
18+
cd "${SCRIPT_DIR}/../"
19+
20+
if [ ! -d "${APP_DIR}" ]; then
21+
APP_DIR=`dirname "$SCRIPT"`/..
22+
APP_DIR=`cd "${APP_DIR}"; pwd`
23+
fi
24+
25+
# Make sure that we are regenerating the sample by removing any existing target directory
26+
TARGET_DIR="$SCRIPT_DIR/../../samples/server/petstore/php-laravel"
27+
if [ -d "$TARGET_DIR" ]; then
28+
rm -rf $TARGET_DIR
29+
fi
30+
31+
executable="$SCRIPT_DIR/../../modules/openapi-generator-cli/target/openapi-generator-cli.jar"
32+
33+
if [ ! -f "$executable" ]
34+
then
35+
mvn clean package
36+
fi
37+
38+
# if you've executed sbt assembly previously it will use that instead.
39+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
40+
ags="generate -t $SCRIPT_DIR/../../modules/openapi-generator/src/main/resources/php-laravel -i $SCRIPT_DIR/../../modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g php-laravel -o $TARGET_DIR $@"
41+
42+
java $JAVA_OPTS -jar $executable $ags

bin/php-laravel-petstore-server.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
echo "# START SCRIPT: $SCRIPT"
5+
6+
while [ -h "$SCRIPT" ] ; do
7+
ls=`ls -ld "$SCRIPT"`
8+
link=`expr "$ls" : '.*-> \(.*\)$'`
9+
if expr "$link" : '/.*' > /dev/null; then
10+
SCRIPT="$link"
11+
else
12+
SCRIPT=`dirname "$SCRIPT"`/"$link"
13+
fi
14+
done
15+
16+
if [ ! -d "${APP_DIR}" ]; then
17+
APP_DIR=`dirname "$SCRIPT"`/..
18+
APP_DIR=`cd "${APP_DIR}"; pwd`
19+
fi
20+
21+
executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"
22+
23+
if [ ! -f "$executable" ]
24+
then
25+
mvn -B clean package
26+
fi
27+
28+
# if you've executed sbt assembly previously it will use that instead.
29+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
30+
ags="generate -t modules/openapi-generator/src/main/resources/php-laravel -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g php-laravel -o samples/server/petstore/php-laravel $@"
31+
32+
java $JAVA_OPTS -jar $executable $ags
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*
2+
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+
* Copyright 2018 SmartBear Software
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.openapitools.codegen.languages;
19+
20+
import io.swagger.models.properties.*;
21+
import io.swagger.v3.oas.models.*;
22+
import io.swagger.v3.oas.models.media.*;
23+
import io.swagger.v3.oas.models.parameters.*;
24+
import org.openapitools.codegen.*;
25+
26+
import java.io.File;
27+
import java.util.*;
28+
29+
import io.swagger.v3.oas.models.media.*;
30+
import io.swagger.v3.oas.models.PathItem;
31+
import io.swagger.v3.oas.models.PathItem.HttpMethod;
32+
import io.swagger.v3.oas.models.*;
33+
import io.swagger.v3.oas.models.parameters.*;
34+
import io.swagger.v3.core.util.Yaml;
35+
36+
import org.apache.commons.lang3.StringUtils;
37+
import org.slf4j.Logger;
38+
import org.slf4j.LoggerFactory;
39+
40+
41+
public class PhpLaravelServerCodegen extends AbstractPhpCodegen implements CodegenConfig {
42+
@SuppressWarnings("hiding")
43+
protected String apiVersion = "1.0.0";
44+
protected String variableNamingConvention = "camelCase";
45+
46+
/**
47+
* Configures the type of generator.
48+
*
49+
* @return the CodegenType for this generator
50+
* @see org.openapitools.codegen.CodegenType
51+
*/
52+
public CodegenType getTag() {
53+
return CodegenType.SERVER;
54+
}
55+
56+
/**
57+
* Configures a friendly name for the generator. This will be used by the generator
58+
* to select the library with the -g flag.
59+
*
60+
* @return the friendly name for the generator
61+
*/
62+
public String getName() {
63+
return "php-laravel";
64+
}
65+
66+
/**
67+
* Returns human-friendly help for the generator. Provide the consumer with help
68+
* tips, parameters here
69+
*
70+
* @return A string value for the help message
71+
*/
72+
public String getHelp() {
73+
return "Generates a PHP laravel server library.";
74+
}
75+
76+
/**
77+
* Class constructor
78+
*/
79+
public PhpLaravelServerCodegen() {
80+
super();
81+
82+
embeddedTemplateDir = templateDir = "php-laravel";
83+
84+
/*
85+
* packPath
86+
*/
87+
invokerPackage = "php-laravel";
88+
packagePath = "";
89+
outputFolder = packagePath + File.separator + srcBasePath;
90+
91+
/*
92+
* Api Package. Optional, if needed, this can be used in templates
93+
*/
94+
apiPackage = "app.Http.Controllers";
95+
96+
/*
97+
* Model Package. Optional, if needed, this can be used in templates
98+
*/
99+
modelPackage = "models";
100+
101+
// template files want to be ignored
102+
modelTemplateFiles.clear();
103+
apiTestTemplateFiles.clear();
104+
apiDocTemplateFiles.clear();
105+
modelDocTemplateFiles.clear();
106+
107+
/*
108+
* Additional Properties. These values can be passed to the templates and
109+
* are available in models, apis, and supporting files
110+
*/
111+
additionalProperties.put("apiVersion", apiVersion);
112+
113+
/*
114+
* Supporting Files. You can write single files for the generator with the
115+
* entire object tree available. If the input file has a suffix of `.mustache
116+
* it will be processed by the template engine. Otherwise, it will be copied
117+
*/
118+
supportingFiles.add(new SupportingFile("composer.mustache", outputFolder, "composer.json"));
119+
supportingFiles.add(new SupportingFile("README.md", outputFolder, "README.md"));
120+
supportingFiles.add(new SupportingFile("artisan", outputFolder, "artisan"));
121+
122+
supportingFiles.add(new SupportingFile("bootstrap/cache/.gitignore", outputFolder + File.separator + "bootstrap" + File.separator + "cache", ".gitignore"));
123+
supportingFiles.add(new SupportingFile("bootstrap/app.php", outputFolder + File.separator + "bootstrap", "app.php"));
124+
supportingFiles.add(new SupportingFile("bootstrap/testingAutoload.php", outputFolder + File.separator + "bootstrap", "testingAutoload.php"));
125+
126+
/* /public/ */
127+
supportingFiles.add(new SupportingFile("public/.htaccess", outputFolder + File.separator + "public", ".htaccess"));
128+
supportingFiles.add(new SupportingFile("public/favicon.ico", outputFolder + File.separator + "public", "favicon.ico"));
129+
supportingFiles.add(new SupportingFile("public/index.php", outputFolder + File.separator + "public", "index.php"));
130+
supportingFiles.add(new SupportingFile("public/robots.txt", outputFolder + File.separator + "public", "robots.txt"));
131+
132+
/* routes */
133+
supportingFiles.add(new SupportingFile("routes/api.mustache", outputFolder + File.separator + "routes", "api.php"));
134+
supportingFiles.add(new SupportingFile("routes/web.mustache", outputFolder + File.separator + "routes", "web.php"));
135+
136+
/* /app/Http/Controllers/ */
137+
supportingFiles.add(new SupportingFile("app/Http/Controllers/Controller.php", outputFolder + File.separator + "app" + File.separator + "Http" + File.separator + "Controllers", "Controller.php"));
138+
}
139+
140+
// override with any special post-processing
141+
@Override
142+
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
143+
@SuppressWarnings("unchecked")
144+
Map<String, Object> objectMap = (Map<String, Object>) objs.get("operations");
145+
@SuppressWarnings("unchecked")
146+
List<CodegenOperation> operations = (List<CodegenOperation>) objectMap.get("operation");
147+
148+
for (CodegenOperation op : operations) {
149+
op.httpMethod = op.httpMethod.toLowerCase();
150+
// check to see if the path contains ".", which is not supported by PHP laravel
151+
// ref: https://github.com/swagger-api/swagger-codegen/issues/6897
152+
if (op.path != null && op.path.contains(".")) {
153+
throw new IllegalArgumentException("'.' (dot) is not supported by PHP laravel. Please refer to https://github.com/swagger-api/swagger-codegen/issues/6897 for more info.");
154+
}
155+
}
156+
157+
// sort the endpoints in ascending to avoid the route priority issure.
158+
// https://github.com/swagger-api/swagger-codegen/issues/2643
159+
Collections.sort(operations, new Comparator<CodegenOperation>() {
160+
@Override
161+
public int compare(CodegenOperation lhs, CodegenOperation rhs) {
162+
return lhs.path.compareTo(rhs.path);
163+
}
164+
});
165+
166+
return objs;
167+
}
168+
169+
@Override
170+
public String toApiName(String name) {
171+
if (name.isEmpty()) {
172+
return "DefaultController";
173+
}
174+
175+
return camelize(name, false) + "Controller";
176+
}
177+
178+
protected String controllerFileFolder() {
179+
return (outputFolder + File.separator + srcBasePath + File.separator + "app" + File.separator + "Http" + File.separator + "Controllers");
180+
}
181+
182+
@Override
183+
public String apiFilename(String templateName, String tag) {
184+
String suffix = apiTemplateFiles().get(templateName);
185+
if (templateName.equals("api.mustache")) {
186+
return controllerFileFolder() + '/' + toControllerName(tag) + suffix;
187+
}
188+
189+
return apiFileFolder() + '/' + toApiFilename(tag) + suffix;
190+
}
191+
192+
protected String toControllerName(String name) {
193+
if (name.isEmpty()) {
194+
return "DefaultController";
195+
}
196+
197+
return camelize(name, false) + "Controller";
198+
}
199+
}

modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ org.openapitools.codegen.languages.OpenAPIGenerator
5555
org.openapitools.codegen.languages.OpenAPIYamlGenerator
5656
org.openapitools.codegen.languages.PerlClientCodegen
5757
org.openapitools.codegen.languages.PhpClientCodegen
58+
org.openapitools.codegen.languages.PhpLaravelServerCodegen
5859
org.openapitools.codegen.languages.PhpLumenServerCodegen
5960
org.openapitools.codegen.languages.PhpSlimServerCodegen
6061
org.openapitools.codegen.languages.PhpSilexServerCodegen
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# OpenAPI generated server
2+
3+
## Overview
4+
This server was generated by the [openapi-generator](https://github.com/openapitools/openapi-generator) project. By using the
5+
[OpenAPI-Spec](https://github.com/swagger-api/swagger-core/wiki) from a remote server, you can easily generate a server stub. This
6+
is an example of building a PHP server.
7+
8+
This example uses the [laravel Framework](http://laravel.com/). To see how to make this your own, please take a look at the template here:
9+
10+
## Installation & Usage
11+
### Composer
12+
13+
Using `composer install` to install the framework and dependencies via [Composer](http://getcomposer.org/).
14+

0 commit comments

Comments
 (0)