From 63b9e1ce203f96b6f004a78cf44d1dde7571c884 Mon Sep 17 00:00:00 2001 From: Chad Arimura Date: Fri, 26 May 2017 15:51:14 -0700 Subject: [PATCH] update php tutorial --- examples/tutorial/hello/php/.gitignore | 2 ++ examples/tutorial/hello/php/README.md | 36 +++++++++++++++-------- examples/tutorial/hello/php/composer.json | 1 + examples/tutorial/hello/php/func.php | 5 ++-- examples/tutorial/hello/php/func.yaml | 2 +- examples/tutorial/hello/ruby/README.md | 4 +-- 6 files changed, 33 insertions(+), 17 deletions(-) diff --git a/examples/tutorial/hello/php/.gitignore b/examples/tutorial/hello/php/.gitignore index 48b8bf907..fa3f387e3 100644 --- a/examples/tutorial/hello/php/.gitignore +++ b/examples/tutorial/hello/php/.gitignore @@ -1 +1,3 @@ vendor/ +func.yaml +composer.lock diff --git a/examples/tutorial/hello/php/README.md b/examples/tutorial/hello/php/README.md index d00232b40..7c15ea6d4 100644 --- a/examples/tutorial/hello/php/README.md +++ b/examples/tutorial/hello/php/README.md @@ -37,14 +37,21 @@ That's it! ### Note on Dependencies -```yml -name: USERNAME/hello -version: 0.0.1 -path: /hello -build: -- docker run --rm -v "$PWD":/worker -w /worker funcy/php:dev composer install +In PHP, you can create a [composer](https://getcomposer.org/) file in your function directory, then run: + +```sh +fn build ``` +This will rebuild your gems and vendor them. PHP doesn't pick them up automatically, so you'll have to add this to the top of your `func.php` file: + +```php +require 'vendor/autoload.php'; +``` + +Open `func.php` to see it in action. + + ### 3. Queue jobs for your function Now you can start jobs on your function. Let's quickly queue up a job to try it out. @@ -63,18 +70,23 @@ cat hello.payload.json | fn call phpapp /hello ``` 2. We received our function input through **stdin** - ```node - obj = JSON.parse(fs.readFileSync('/dev/stdin').toString()) + ```php + $payload = json_decode(file_get_contents("php://stdin"), true); ``` 3. We wrote our output to **stdout** - ```node - console.log + ```php + echo "Hello World!\n"; ``` 4. We sent **stderr** to the server logs - ```node - console.error + ```php + fwrite(STDERR, "--> this will go to stderr (server logs)\n"); + ``` + +5. We added PHP dependencies and enabled them using: + ```php + require 'vendor/autoload.php'; ``` diff --git a/examples/tutorial/hello/php/composer.json b/examples/tutorial/hello/php/composer.json index df8dd9cf8..6f1595fec 100644 --- a/examples/tutorial/hello/php/composer.json +++ b/examples/tutorial/hello/php/composer.json @@ -1,4 +1,5 @@ { "require": { + "monolog/monolog": "1.0.*" } } diff --git a/examples/tutorial/hello/php/func.php b/examples/tutorial/hello/php/func.php index 7828f1f95..50781499f 100644 --- a/examples/tutorial/hello/php/func.php +++ b/examples/tutorial/hello/php/func.php @@ -1,10 +1,11 @@ this will go to stderr (server logs)\n"); stream_set_blocking(STDIN, 0); $payload = json_decode(file_get_contents("php://stdin"), true); if (isset($payload['name'])) { - echo "Hello ", $payload['name'],"!\n\n"; + echo "Hello ", $payload['name'],"!\n"; } else { - echo "Hello World!\n\n"; + echo "Hello World!\n"; } diff --git a/examples/tutorial/hello/php/func.yaml b/examples/tutorial/hello/php/func.yaml index b645b5960..6036f79c7 100644 --- a/examples/tutorial/hello/php/func.yaml +++ b/examples/tutorial/hello/php/func.yaml @@ -1,5 +1,5 @@ name: carimura2/hello -version: 0.0.1 +version: 0.0.2 runtime: php entrypoint: php func.php path: /hello diff --git a/examples/tutorial/hello/ruby/README.md b/examples/tutorial/hello/ruby/README.md index 5f2ca5253..f348f9699 100644 --- a/examples/tutorial/hello/ruby/README.md +++ b/examples/tutorial/hello/ruby/README.md @@ -40,10 +40,10 @@ That's it! In Ruby, we create a [Gemfile](http://bundler.io/gemfile.html) file in your function directory, then run: ```sh -docker run --rm -it -v ${pwd}:/worker -w /worker funcy/ruby:dev bundle install --standalone --clean +fn build ``` -Ruby doesn't pick up the gems automatically, so you'll have to add this to the top of your `func.rb` file: +This will rebuild your gems and vendor them. Ruby doesn't pick up the gems automatically, so you'll have to add this to the top of your `func.rb` file: ```ruby require_relative 'bundle/bundler/setup'