mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-07 19:08:52 +00:00
Add a basic PHP server-generator example
This commit is contained in:
parent
88692ec42d
commit
06332e5dce
41
samples/server-generator/php/PHPServerGenerator.scala
Normal file
41
samples/server-generator/php/PHPServerGenerator.scala
Normal file
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Copyright 2014 Wordnik, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import com.wordnik.swagger.codegen.BasicPHPGenerator
|
||||
|
||||
import scala.collection.mutable.{ HashMap, ListBuffer }
|
||||
|
||||
object PHPServerGenerator extends BasicPHPGenerator {
|
||||
def main(args: Array[String]) = generateClient(args)
|
||||
|
||||
override def templateDir = "samples/server-generator/php/templates"
|
||||
|
||||
val outputFolder = "samples/server-generator/php/output"
|
||||
|
||||
// where to write generated code
|
||||
override def destinationDir = outputFolder + ""
|
||||
|
||||
apiTemplateFiles.clear
|
||||
|
||||
modelTemplateFiles.clear
|
||||
|
||||
// supporting classes
|
||||
override def supportingFiles = List(
|
||||
("README.mustache", outputFolder, "README.md"),
|
||||
("composer.json", outputFolder, "composer.json"),
|
||||
(".htaccess", outputFolder, ".htaccess"),
|
||||
("index.mustache", outputFolder, "index.php"))
|
||||
}
|
59
samples/server-generator/php/README.md
Normal file
59
samples/server-generator/php/README.md
Normal file
@ -0,0 +1,59 @@
|
||||
# Swagger generated server
|
||||
|
||||
## Overview
|
||||
Using the swagger-codegen, you can not only generate clients but servers as well! The same spec can be used to drive your
|
||||
development both ways. This is an example of generating a server for `PHP`.
|
||||
|
||||
### Prerequisites
|
||||
You need the following installed and available in your $PATH:
|
||||
|
||||
<li>- Scala 2.9.1 [available here](http://www.scala-lang.org)
|
||||
|
||||
You also need to add scala binary to your PATH.
|
||||
|
||||
You need an apache server running with mod_rewrite enabled
|
||||
|
||||
### Generating a server
|
||||
You first need to build the `swagger-codegen` project--this is done by running this command at the root of the swagger-codegen project:
|
||||
|
||||
```
|
||||
mvn package
|
||||
```
|
||||
|
||||
You can now generate a server from any valid[**](https://github.com/swagger-api/swagger-codegen/blob/master/README.md#validating-your-swagger-spec) swagger spec:
|
||||
|
||||
```
|
||||
./bin/runscala.sh samples/server-generator/php/PHPServerFromSpec.scala http://petstore.swagger.wordnik.com/api/api-docs special-key
|
||||
```
|
||||
|
||||
After executing this script, you will have an output directory with the server-generated files:
|
||||
|
||||
```
|
||||
$ cd samples/server-generator/php/output
|
||||
$ find . -type f
|
||||
./.htaccess
|
||||
./composer.json
|
||||
./index.php
|
||||
./README.md
|
||||
```
|
||||
|
||||
To install the dependencies, cd to the `samples/server-generator/php/output` folder and run:
|
||||
|
||||
```
|
||||
$ curl -s http://getcomposer.org/installer | php
|
||||
$ php composer.phar install
|
||||
```
|
||||
|
||||
You can now access the api by going to `http://localhost/path-to-output-dir/`. Of course this isn't a fully
|
||||
runnable server! You have to add the logic in the index.php file. But that's the easy part.
|
||||
|
||||
|
||||
### Making it your own
|
||||
Running the sample is easy, but how about making your own server? Easy! Just modify the `samples/server-generator/php/PHPServerGenerator.scala` file.
|
||||
|
||||
Don't like the templates? Don't worry, we're not offended! They're [mustache](http://mustache.github.com/) templates and are easy to modify.
|
||||
Take a look at the sample templates here:
|
||||
|
||||
<li> - Generator for the index.php file : [api.mustache](https://github.com/swagger-api/swagger-codegen/blob/master/samples/server-generator/php/templates/index.mustache)
|
||||
|
||||
Sound easy? It is!
|
5
samples/server-generator/php/output/.htaccess
Normal file
5
samples/server-generator/php/output/.htaccess
Normal file
@ -0,0 +1,5 @@
|
||||
<IfModule mod_rewrite.c>
|
||||
RewriteEngine On
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
|
||||
</IfModule>
|
10
samples/server-generator/php/output/README.md
Normal file
10
samples/server-generator/php/output/README.md
Normal file
@ -0,0 +1,10 @@
|
||||
# Swagger generated server
|
||||
|
||||
## Overview
|
||||
This server was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. By using the
|
||||
[swagger-spec](https://github.com/swagger-api/swagger-core/wiki) from a remote server, you can easily generate a server stub. This
|
||||
is an example of building a PHP server.
|
||||
|
||||
This example uses the [Silex](http://silex.sensiolabs.org/) micro-framework. To see how to make this your own, look here:
|
||||
|
||||
[README](https://github.com/swagger-api/swagger-codegen/tree/master/samples/server-generator/php)
|
5
samples/server-generator/php/output/composer.json
Normal file
5
samples/server-generator/php/output/composer.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"require": {
|
||||
"silex/silex": "~1.2"
|
||||
}
|
||||
}
|
98
samples/server-generator/php/output/index.php
Normal file
98
samples/server-generator/php/output/index.php
Normal file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Silex\Application;
|
||||
|
||||
$app = new Silex\Application();
|
||||
|
||||
$app->POST('/user/createWithArray', function(Application $app, Request $request) {
|
||||
return new Response('How about implementing createUsersWithArrayInput as a POST method ?');
|
||||
});
|
||||
|
||||
$app->POST('/user/createWithList', function(Application $app, Request $request) {
|
||||
return new Response('How about implementing createUsersWithListInput as a POST method ?');
|
||||
});
|
||||
|
||||
$app->PUT('/user/{username}', function(Application $app, Request $request, $username) {
|
||||
return new Response('How about implementing updateUser as a PUT method ?');
|
||||
});
|
||||
|
||||
$app->DELETE('/user/{username}', function(Application $app, Request $request, $username) {
|
||||
return new Response('How about implementing deleteUser as a DELETE method ?');
|
||||
});
|
||||
|
||||
$app->GET('/user/{username}', function(Application $app, Request $request, $username) {
|
||||
return new Response('How about implementing getUserByName as a GET method ?');
|
||||
});
|
||||
|
||||
$app->GET('/user/login', function(Application $app, Request $request) {
|
||||
$username = $request->get('username');
|
||||
$password = $request->get('password');
|
||||
return new Response('How about implementing loginUser as a GET method ?');
|
||||
});
|
||||
|
||||
$app->GET('/user/logout', function(Application $app, Request $request) {
|
||||
return new Response('How about implementing logoutUser as a GET method ?');
|
||||
});
|
||||
|
||||
$app->POST('/user', function(Application $app, Request $request) {
|
||||
return new Response('How about implementing createUser as a POST method ?');
|
||||
});
|
||||
|
||||
$app->PUT('/pet', function(Application $app, Request $request) {
|
||||
return new Response('How about implementing updatePet as a PUT method ?');
|
||||
});
|
||||
|
||||
$app->POST('/pet', function(Application $app, Request $request) {
|
||||
return new Response('How about implementing addPet as a POST method ?');
|
||||
});
|
||||
|
||||
$app->GET('/pet/findByStatus', function(Application $app, Request $request) {
|
||||
$status = $request->get('status');
|
||||
return new Response('How about implementing findPetsByStatus as a GET method ?');
|
||||
});
|
||||
|
||||
$app->GET('/pet/findByTags', function(Application $app, Request $request) {
|
||||
$tags = $request->get('tags');
|
||||
return new Response('How about implementing findPetsByTags as a GET method ?');
|
||||
});
|
||||
|
||||
$app->POST('/pet/{petId}', function(Application $app, Request $request, $petId) {
|
||||
$name = $request->get('name');
|
||||
$status = $request->get('status');
|
||||
return new Response('How about implementing updatePetWithForm as a POST method ?');
|
||||
});
|
||||
|
||||
$app->GET('/pet/{petId}', function(Application $app, Request $request, $petId) {
|
||||
return new Response('How about implementing getPetById as a GET method ?');
|
||||
});
|
||||
|
||||
$app->DELETE('/pet/{petId}', function(Application $app, Request $request, $petId) {
|
||||
return new Response('How about implementing deletePet as a DELETE method ?');
|
||||
});
|
||||
|
||||
$app->PATCH('/pet/{petId}', function(Application $app, Request $request, $petId) {
|
||||
return new Response('How about implementing partialUpdate as a PATCH method ?');
|
||||
});
|
||||
|
||||
$app->POST('/pet/uploadImage', function(Application $app, Request $request) {
|
||||
$additionalMetadata = $request->get('additionalMetadata');
|
||||
$file = $request->get('file');
|
||||
return new Response('How about implementing uploadFile as a POST method ?');
|
||||
});
|
||||
|
||||
$app->POST('/store/order', function(Application $app, Request $request) {
|
||||
return new Response('How about implementing placeOrder as a POST method ?');
|
||||
});
|
||||
|
||||
$app->DELETE('/store/order/{orderId}', function(Application $app, Request $request, $orderId) {
|
||||
return new Response('How about implementing deleteOrder as a DELETE method ?');
|
||||
});
|
||||
|
||||
$app->GET('/store/order/{orderId}', function(Application $app, Request $request, $orderId) {
|
||||
return new Response('How about implementing getOrderById as a GET method ?');
|
||||
});
|
||||
|
||||
$app->run();
|
5
samples/server-generator/php/templates/.htaccess
Normal file
5
samples/server-generator/php/templates/.htaccess
Normal file
@ -0,0 +1,5 @@
|
||||
<IfModule mod_rewrite.c>
|
||||
RewriteEngine On
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
|
||||
</IfModule>
|
10
samples/server-generator/php/templates/README.mustache
Normal file
10
samples/server-generator/php/templates/README.mustache
Normal file
@ -0,0 +1,10 @@
|
||||
# Swagger generated server
|
||||
|
||||
## Overview
|
||||
This server was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. By using the
|
||||
[swagger-spec](https://github.com/swagger-api/swagger-core/wiki) from a remote server, you can easily generate a server stub. This
|
||||
is an example of building a PHP server.
|
||||
|
||||
This example uses the [Silex](http://silex.sensiolabs.org/) micro-framework. To see how to make this your own, look here:
|
||||
|
||||
[README](https://github.com/swagger-api/swagger-codegen/tree/master/samples/server-generator/php)
|
5
samples/server-generator/php/templates/composer.json
Normal file
5
samples/server-generator/php/templates/composer.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"require": {
|
||||
"silex/silex": "~1.2"
|
||||
}
|
||||
}
|
26
samples/server-generator/php/templates/index.mustache
Normal file
26
samples/server-generator/php/templates/index.mustache
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Silex\Application;
|
||||
|
||||
$app = new Silex\Application();
|
||||
|
||||
{{#apiInfo}}
|
||||
{{#apis}}
|
||||
{{#operations}}
|
||||
{{#operation}}
|
||||
|
||||
$app->{{httpMethod}}('{{path}}', function(Application $app, Request $request{{#pathParams}}, ${{paramName}}{{/pathParams}}) {
|
||||
{{#queryParams}}${{paramName}} = $request->get('{{paramName}}');{{newline}} {{/queryParams}}
|
||||
{{#formParams}}${{paramName}} = $request->get('{{paramName}}');{{newline}} {{/formParams}}
|
||||
return new Response('How about implementing {{nickname}} as a {{httpMethod}} method ?');
|
||||
});
|
||||
|
||||
{{/operation}}
|
||||
{{/operations}}
|
||||
{{/apis}}
|
||||
{{/apiInfo}}
|
||||
|
||||
$app->run();
|
Loading…
Reference in New Issue
Block a user