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/
func.yaml
composer.lock

View File

@@ -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';
```

View File

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

View File

@@ -1,10 +1,11 @@
<?php
require 'vendor/autoload.php';
fwrite(STDERR, "--> 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";
}

View File

@@ -1,5 +1,5 @@
name: carimura2/hello
version: 0.0.1
version: 0.0.2
runtime: php
entrypoint: php func.php
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:
```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'