Skip to content

Commit 94a69ac

Browse files
renepardonwing328
authored andcommitted
feat (PHP LARAVEL) 8417: initial PHP-laravel codegen integration (OpenAPITools#574)
* feat (PHP LARAVEL) 8417: initial PHP-laravel codegen integration * feat (PHP LARAVEL) 8417: code review adjustments * feat (PHP LARAVEL) 8417: fix typos; add missing files; adjust readme for those unfamilar with laravel to get started quickly * feat (PHP LARAVEL) 8417: add sample petstore server * feat (PHP LARAVEL) 8417: adjust route service provdider and model generation
1 parent bbe7b9a commit 94a69ac

File tree

205 files changed

+8380
-0
lines changed

Some content is hidden

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

205 files changed

+8380
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ samples/client/petstore/silex/SwaggerServer/venodr/
9999
samples/server/petstore/php-symfony/SymfonyBundle-php/Tests/cache/
100100
samples/server/petstore/php-symfony/SymfonyBundle-php/Tests/logs/
101101

102+
#PHP-laravel
103+
samples/server/petstore/php-laravel/node_modules
102104

103105
# Perl
104106
samples/client/petstore/perl/deep_module_test/

README.md

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

320+
##### Troubleshooting
321+
322+
If an error like this occurs, just execute the **mvn clean install -U** command:
323+
324+
> 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
325+
326+
```sh
327+
./run-in-docker.sh mvn clean install -U
328+
```
329+
330+
> 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'
331+
332+
Right now: no solution for this one :|
333+
320334
#### Run Docker in Vagrant
321335
Prerequisite: install [Vagrant](https://www.vagrantup.com/downloads.html) and [VirtualBox](https://www.virtualbox.org/wiki/Downloads).
322336
```sh

bin/openapi3/php-laravel-petstore.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

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpLaravelServerCodegen.java

Lines changed: 285 additions & 0 deletions
Large diffs are not rendered by default.

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
@@ -56,6 +56,7 @@ org.openapitools.codegen.languages.OpenAPIGenerator
5656
org.openapitools.codegen.languages.OpenAPIYamlGenerator
5757
org.openapitools.codegen.languages.PerlClientCodegen
5858
org.openapitools.codegen.languages.PhpClientCodegen
59+
org.openapitools.codegen.languages.PhpLaravelServerCodegen
5960
org.openapitools.codegen.languages.PhpLumenServerCodegen
6061
org.openapitools.codegen.languages.PhpSlimServerCodegen
6162
org.openapitools.codegen.languages.PhpSilexServerCodegen
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
APP_NAME=Laravel
2+
APP_ENV=local
3+
APP_KEY=
4+
APP_DEBUG=true
5+
APP_URL=http://localhost
6+
7+
LOG_CHANNEL=stack
8+
9+
DB_CONNECTION=mysql
10+
DB_HOST=127.0.0.1
11+
DB_PORT=3306
12+
DB_DATABASE=homestead
13+
DB_USERNAME=homestead
14+
DB_PASSWORD=secret
15+
16+
BROADCAST_DRIVER=log
17+
CACHE_DRIVER=file
18+
SESSION_DRIVER=file
19+
SESSION_LIFETIME=120
20+
QUEUE_DRIVER=sync
21+
22+
REDIS_HOST=127.0.0.1
23+
REDIS_PASSWORD=null
24+
REDIS_PORT=6379
25+
26+
MAIL_DRIVER=smtp
27+
MAIL_HOST=smtp.mailtrap.io
28+
MAIL_PORT=2525
29+
MAIL_USERNAME=null
30+
MAIL_PASSWORD=null
31+
MAIL_ENCRYPTION=null
32+
33+
PUSHER_APP_ID=
34+
PUSHER_APP_KEY=
35+
PUSHER_APP_SECRET=
36+
PUSHER_APP_CLUSTER=mt1
37+
38+
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
39+
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
15+
### post installation steps
16+
17+
Change into application folder and execute following commands to get started:
18+
19+
```sh
20+
cp .env.example .env
21+
php artisan key:generate
22+
php artisan serve
23+
```
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
{{>licenseInfo}}
4+
5+
namespace App\Http\Controllers;
6+
7+
use Illuminate\Support\Facades\Request;
8+
9+
{{#operations}}class {{classname}} extends Controller
10+
{
11+
/**
12+
* Constructor
13+
*/
14+
public function __construct()
15+
{
16+
}
17+
18+
{{#operation}}
19+
/**
20+
* Operation {{{operationId}}}
21+
*
22+
* {{{summary}}}.
23+
*
24+
{{#pathParams}} * @param {{dataType}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
25+
{{/pathParams}} *
26+
* @return Http response
27+
*/
28+
public function {{operationId}}({{#pathParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/pathParams}})
29+
{
30+
$input = Request::all();
31+
32+
//path params validation
33+
{{#pathParams}}
34+
{{#hasValidation}}
35+
{{#maxLength}}
36+
if (strlen(${{paramName}}) > {{maxLength}}) {
37+
throw new \InvalidArgumentException('invalid length for ${{paramName}} when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maxLength}}.');
38+
}
39+
{{/maxLength}}
40+
{{#minLength}}
41+
if (strlen(${{paramName}}) < {{minLength}}) {
42+
throw new \InvalidArgumentException('invalid length for ${{paramName}} when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minLength}}.');
43+
}
44+
{{/minLength}}
45+
{{#maximum}}
46+
if (${{paramName}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}) {
47+
throw new \InvalidArgumentException('invalid value for ${{paramName}} when calling {{classname}}.{{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.');
48+
}
49+
{{/maximum}}
50+
{{#minimum}}
51+
if (${{paramName}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}) {
52+
throw new \InvalidArgumentException('invalid value for ${{paramName}} when calling {{classname}}.{{operationId}}, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.');
53+
}
54+
{{/minimum}}
55+
{{#pattern}}
56+
if (!preg_match("{{{pattern}}}", ${{paramName}})) {
57+
throw new \InvalidArgumentException('invalid value for ${{paramName}} when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}.');
58+
}
59+
{{/pattern}}
60+
{{#maxItems}}
61+
if (count(${{paramName}}) > {{maxItems}}) {
62+
throw new \InvalidArgumentException('invalid length for ${{paramName}} when calling {{classname}}.{{operationId}}, number of items must be less than or equal to {{maxItems}}.');
63+
}
64+
{{/maxItems}}
65+
{{#minItems}}
66+
if (count(${{paramName}}) < {{minItems}}) {
67+
throw new \InvalidArgumentException('invalid length for ${{paramName}} when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{minItems}}.');
68+
}
69+
{{/minItems}}
70+
{{/hasValidation}}
71+
{{/pathParams}}
72+
73+
74+
//not path params validation
75+
{{#allParams}}
76+
{{^pathParams}}
77+
{{#required}}
78+
if (!isset($input['{{paramName}}'])) {
79+
throw new \InvalidArgumentException('Missing the required parameter ${{paramName}} when calling {{operationId}}');
80+
}
81+
{{/required}}
82+
{{#hasValidation}}
83+
{{#maxLength}}
84+
if (strlen($input['{{paramName}}']) > {{maxLength}}) {
85+
throw new \InvalidArgumentException('invalid length for ${{paramName}} when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maxLength}}.');
86+
}
87+
{{/maxLength}}
88+
{{#minLength}}
89+
if (strlen($input['{{paramName}}']) < {{minLength}}) {
90+
throw new \InvalidArgumentException('invalid length for ${{paramName}} when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minLength}}.');
91+
}
92+
{{/minLength}}
93+
{{#maximum}}
94+
if ($input['{{paramName}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}) {
95+
throw new \InvalidArgumentException('invalid value for ${{paramName}} when calling {{classname}}.{{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.');
96+
}
97+
{{/maximum}}
98+
{{#minimum}}
99+
if ($input['{{paramName}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}) {
100+
throw new \InvalidArgumentException('invalid value for ${{paramName}} when calling {{classname}}.{{operationId}}, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.');
101+
}
102+
{{/minimum}}
103+
{{#pattern}}
104+
if (!preg_match("{{{pattern}}}", $input['{{paramName}}'])) {
105+
throw new \InvalidArgumentException('invalid value for ${{paramName}} when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}.');
106+
}
107+
{{/pattern}}
108+
{{#maxItems}}
109+
if (count($input['{{paramName}}']) > {{maxItems}}) {
110+
throw new \InvalidArgumentException('invalid length for ${{paramName}} when calling {{classname}}.{{operationId}}, number of items must be less than or equal to {{maxItems}}.');
111+
}
112+
{{/maxItems}}
113+
{{#minItems}}
114+
if (count($input['{{paramName}}']) < {{minItems}}) {
115+
throw new \InvalidArgumentException('invalid length for ${{paramName}} when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{minItems}}.');
116+
}
117+
{{/minItems}}
118+
{{/hasValidation}}
119+
${{paramName}} = $input['{{paramName}}'];
120+
121+
{{/pathParams}}
122+
{{/allParams}}
123+
124+
return response('How about implementing {{nickname}} as a {{httpMethod}} method ?');
125+
}
126+
{{/operation}}
127+
}
128+
{{/operations}}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Console;
4+
5+
use Illuminate\Console\Scheduling\Schedule;
6+
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
7+
8+
class Kernel extends ConsoleKernel
9+
{
10+
/**
11+
* The Artisan commands provided by your application.
12+
*
13+
* @var array
14+
*/
15+
protected $commands = [
16+
//
17+
];
18+
19+
/**
20+
* Define the application's command schedule.
21+
*
22+
* @param \Illuminate\Console\Scheduling\Schedule $schedule
23+
* @return void
24+
*/
25+
protected function schedule(Schedule $schedule)
26+
{
27+
// $schedule->command('inspire')
28+
// ->hourly();
29+
}
30+
31+
/**
32+
* Register the commands for the application.
33+
*
34+
* @return void
35+
*/
36+
protected function commands()
37+
{
38+
$this->load(__DIR__.'/Commands');
39+
40+
require base_path('routes/console.php');
41+
}
42+
}

0 commit comments

Comments
 (0)