update php tutorial

This commit is contained in:
Chad Arimura
2017-05-26 15:51:14 -07:00
parent b49337d4e0
commit 63b9e1ce20
6 changed files with 33 additions and 17 deletions

View File

@@ -1 +1,3 @@
vendor/ vendor/
func.yaml
composer.lock

View File

@@ -37,14 +37,21 @@ That's it!
### Note on Dependencies ### Note on Dependencies
```yml In PHP, you can create a [composer](https://getcomposer.org/) file in your function directory, then run:
name: USERNAME/hello
version: 0.0.1 ```sh
path: /hello fn build
build:
- docker run --rm -v "$PWD":/worker -w /worker funcy/php:dev composer install
``` ```
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 ### 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. 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** 2. We received our function input through **stdin**
```node ```php
obj = JSON.parse(fs.readFileSync('/dev/stdin').toString()) $payload = json_decode(file_get_contents("php://stdin"), true);
``` ```
3. We wrote our output to **stdout** 3. We wrote our output to **stdout**
```node ```php
console.log echo "Hello World!\n";
``` ```
4. We sent **stderr** to the server logs 4. We sent **stderr** to the server logs
```node ```php
console.error fwrite(STDERR, "--> this will go to stderr (server logs)\n");
```
5. We added PHP dependencies and enabled them using:
```php
require 'vendor/autoload.php';
``` ```

View File

@@ -1,4 +1,5 @@
{ {
"require": { "require": {
"monolog/monolog": "1.0.*"
} }
} }

View File

@@ -1,10 +1,11 @@
<?php <?php
require 'vendor/autoload.php'; require 'vendor/autoload.php';
fwrite(STDERR, "--> this will go to stderr (server logs)\n");
stream_set_blocking(STDIN, 0); stream_set_blocking(STDIN, 0);
$payload = json_decode(file_get_contents("php://stdin"), true); $payload = json_decode(file_get_contents("php://stdin"), true);
if (isset($payload['name'])) { if (isset($payload['name'])) {
echo "Hello ", $payload['name'],"!\n\n"; echo "Hello ", $payload['name'],"!\n";
} else { } else {
echo "Hello World!\n\n"; echo "Hello World!\n";
} }

View File

@@ -1,5 +1,5 @@
name: carimura2/hello name: carimura2/hello
version: 0.0.1 version: 0.0.2
runtime: php runtime: php
entrypoint: php func.php entrypoint: php func.php
path: /hello path: /hello

View File

@@ -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: In Ruby, we create a [Gemfile](http://bundler.io/gemfile.html) file in your function directory, then run:
```sh ```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 ```ruby
require_relative 'bundle/bundler/setup' require_relative 'bundle/bundler/setup'