From 06332e5dce1c6be739671c2ff162dde31cd1063c Mon Sep 17 00:00:00 2001 From: Laurent Sarrazin Date: Thu, 30 Oct 2014 22:13:27 +0100 Subject: [PATCH] Add a basic PHP server-generator example --- .../php/PHPServerGenerator.scala | 41 ++++++++ samples/server-generator/php/README.md | 59 +++++++++++ samples/server-generator/php/output/.htaccess | 5 + samples/server-generator/php/output/README.md | 10 ++ .../server-generator/php/output/composer.json | 5 + samples/server-generator/php/output/index.php | 98 +++++++++++++++++++ .../server-generator/php/templates/.htaccess | 5 + .../php/templates/README.mustache | 10 ++ .../php/templates/composer.json | 5 + .../php/templates/index.mustache | 26 +++++ 10 files changed, 264 insertions(+) create mode 100644 samples/server-generator/php/PHPServerGenerator.scala create mode 100644 samples/server-generator/php/README.md create mode 100644 samples/server-generator/php/output/.htaccess create mode 100644 samples/server-generator/php/output/README.md create mode 100644 samples/server-generator/php/output/composer.json create mode 100644 samples/server-generator/php/output/index.php create mode 100644 samples/server-generator/php/templates/.htaccess create mode 100644 samples/server-generator/php/templates/README.mustache create mode 100644 samples/server-generator/php/templates/composer.json create mode 100644 samples/server-generator/php/templates/index.mustache diff --git a/samples/server-generator/php/PHPServerGenerator.scala b/samples/server-generator/php/PHPServerGenerator.scala new file mode 100644 index 0000000000..65353ca82e --- /dev/null +++ b/samples/server-generator/php/PHPServerGenerator.scala @@ -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")) +} diff --git a/samples/server-generator/php/README.md b/samples/server-generator/php/README.md new file mode 100644 index 0000000000..d8f8df4ff7 --- /dev/null +++ b/samples/server-generator/php/README.md @@ -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: + +
  • - 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: + +
  • - 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! diff --git a/samples/server-generator/php/output/.htaccess b/samples/server-generator/php/output/.htaccess new file mode 100644 index 0000000000..e47b5fb8a0 --- /dev/null +++ b/samples/server-generator/php/output/.htaccess @@ -0,0 +1,5 @@ + + RewriteEngine On + RewriteCond %{REQUEST_FILENAME} !-f + RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L] + \ No newline at end of file diff --git a/samples/server-generator/php/output/README.md b/samples/server-generator/php/output/README.md new file mode 100644 index 0000000000..a352afb938 --- /dev/null +++ b/samples/server-generator/php/output/README.md @@ -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) \ No newline at end of file diff --git a/samples/server-generator/php/output/composer.json b/samples/server-generator/php/output/composer.json new file mode 100644 index 0000000000..73f5b42998 --- /dev/null +++ b/samples/server-generator/php/output/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "silex/silex": "~1.2" + } +} \ No newline at end of file diff --git a/samples/server-generator/php/output/index.php b/samples/server-generator/php/output/index.php new file mode 100644 index 0000000000..6801a2afee --- /dev/null +++ b/samples/server-generator/php/output/index.php @@ -0,0 +1,98 @@ +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(); diff --git a/samples/server-generator/php/templates/.htaccess b/samples/server-generator/php/templates/.htaccess new file mode 100644 index 0000000000..e47b5fb8a0 --- /dev/null +++ b/samples/server-generator/php/templates/.htaccess @@ -0,0 +1,5 @@ + + RewriteEngine On + RewriteCond %{REQUEST_FILENAME} !-f + RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L] + \ No newline at end of file diff --git a/samples/server-generator/php/templates/README.mustache b/samples/server-generator/php/templates/README.mustache new file mode 100644 index 0000000000..a352afb938 --- /dev/null +++ b/samples/server-generator/php/templates/README.mustache @@ -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) \ No newline at end of file diff --git a/samples/server-generator/php/templates/composer.json b/samples/server-generator/php/templates/composer.json new file mode 100644 index 0000000000..73f5b42998 --- /dev/null +++ b/samples/server-generator/php/templates/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "silex/silex": "~1.2" + } +} \ No newline at end of file diff --git a/samples/server-generator/php/templates/index.mustache b/samples/server-generator/php/templates/index.mustache new file mode 100644 index 0000000000..26e1dc7248 --- /dev/null +++ b/samples/server-generator/php/templates/index.mustache @@ -0,0 +1,26 @@ +{{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(); \ No newline at end of file