From 557cbe42def3242745b5a61460ce6ceaccdea11c Mon Sep 17 00:00:00 2001
From: Pietro Iglio <igliop@gmail.com>
Date: Fri, 1 Feb 2019 07:50:21 +0100
Subject: [PATCH 1/4] Files for PHP 7.2 and 7.3 have been merged together

---
 Makefile                            |   4 +-
 README.md                           |  60 ++++---
 bootstrap                           |   3 +-
 bootstrap-php72                     | 235 ----------------------------
 bootstrap-php73                     | 235 ----------------------------
 build-php72.sh => build-php-remi.sh |  17 +-
 build-php73.sh                      |  40 -----
 php.ini                             |  35 +----
 php72.ini                           |  38 -----
 php73.ini                           |  38 -----
 tmp/test/due                        |   0
 tmp/test/uno                        |   0
 12 files changed, 50 insertions(+), 655 deletions(-)
 delete mode 100644 bootstrap-php72
 delete mode 100644 bootstrap-php73
 rename build-php72.sh => build-php-remi.sh (53%)
 delete mode 100644 build-php73.sh
 delete mode 100644 php72.ini
 delete mode 100644 php73.ini
 create mode 100644 tmp/test/due
 create mode 100644 tmp/test/uno

diff --git a/Makefile b/Makefile
index 269a84f..fc8714e 100644
--- a/Makefile
+++ b/Makefile
@@ -4,10 +4,10 @@ php71.zip:
 	docker run --rm -v $(ROOT_DIR):/opt/layer lambci/lambda:build-nodejs8.10 /opt/layer/build.sh
 
 php72.zip:
-	docker run --rm -v $(ROOT_DIR):/opt/layer lambci/lambda:build-nodejs8.10 /opt/layer/build-php72.sh
+	docker run --rm -e http_proxy=${http_proxy} -v $(ROOT_DIR):/opt/layer lambci/lambda:build-nodejs8.10 /opt/layer/build-php-remi.sh 2
 
 php73.zip:
-	docker run --rm -v $(ROOT_DIR):/opt/layer lambci/lambda:build-nodejs8.10 /opt/layer/build-php73.sh
+	docker run --rm -e http_proxy=${http_proxy} -v $(ROOT_DIR):/opt/layer lambci/lambda:build-nodejs8.10 /opt/layer/build-php-remi.sh 3
 
 upload: php71.zip
 	./upload.sh
diff --git a/README.md b/README.md
index b60f70b..fe97169 100644
--- a/README.md
+++ b/README.md
@@ -1,15 +1,20 @@
 # PHP Layer For AWS Lambda
 
-Ever wanted to run PHP websites in AWS Lambda? It's your lucky day! This Lambda Runtime Layer runs the [PHP 7.1/7.2/7.3 webserver](http://php.net/manual/en/features.commandline.webserver.php) in response to [AWS API Gateway](https://aws.amazon.com/api-gateway/) or [AWS Application Load Balancer](https://aws.amazon.com/elasticloadbalancing/features/#Details_for_Elastic_Load_Balancing_Products) requests.
+Ever wanted to run PHP websites in AWS Lambda? It's your lucky day! This Lambda Runtime Layer runs the [PHP 7.1/7.2/7.3  webserver](http://php.net/manual/en/features.commandline.webserver.php) in response to [AWS API Gateway](https://aws.amazon.com/api-gateway/) or [AWS Application Load Balancer](https://aws.amazon.com/elasticloadbalancing/features/#Details_for_Elastic_Load_Balancing_Products) requests.
 
-This project is fork of the [Stackery](https://stackery.io) [php-lambda-layer](https://github.com/stackery/php-lambda-layer) that has been modified to:
+And, if you're looking for a great way to build serverless apps of all kinds, be sure to check out [Stackery](https://stackery.io)!
 
-- support PHP 7.1, 7.2, and 7.3 (the original supports 7.1 only) - using the [Remi RPM Repository](https://rpms.remirepo.net/)
-- add support for mbstring, MySQL (pdo-mysql) and Postgres (pdo-pgsql)
+This is an early iteration of the PHP runtime Layer which is not yet ready for production. Please feel free to use this Layer to learn about the Lambda Layers feature and begin experimenting with PHP functions. We welcome feedback and stay tuned for the production-ready version coming soon.
 
-that are requirements for running frameworks such as [Lumen](https://lumen.laravel.com/) and [Laravel](https://laravel.com/) frameworks in serverless mode.
+## Current Layer Version ARN
+When creating/updating a Lambda function you must specify  a specific version of the layer. This readme will be kept up to date with the latest version available. The latest available Lambda Layer Version ARN for PHP 7.1 is:
 
-This is an early iteration of the PHP runtime Layer which is not yet ready for production. Please feel free to use this Layer to learn about the Lambda Layers feature and begin experimenting with PHP functions. We welcome feedback and stay tuned for the production-ready version coming soon.
+**arn:aws:lambda:\<region\>:887080169480:layer:php71:7**
+
+If you need a layer for PHP 7.2 or 7.3, you can build your own.
+You can find an example [here](http://tinyurl.com/ycsbfpbr).
+
+See [Releases](https://github.com/stackery/php-lambda-layer/releases) for release notes.
 
 ### Usage
 #### General Usage
@@ -24,14 +29,14 @@ The Lambda Function Handler property specifies the location of the the script ex
 #### Configuration Files
 There are three locations where PHP configuration may be located:
 
-* Files in layer code packages located under /etc/php-<PHP_VERSION>.d/
-* Files in function code package located under /php-<PHP_VERSION>.d/
+* Files in layer code packages located under /etc/php-${PHP_VERSION}.d/
+* Files in function code package located under /php-${PHP_VERSION}.d/
 * php.ini located at the root of the function code package
 
-Replace <PHP_VERSION> with 7.1, 7.2, or 7.3 according to your preferred runtime.
+Replace ${PHP_VERSION} with '7.1', '7.2', or '7.3' according to your preferred runtime.
 
 ##### Extensions
-The following extensions are built into the layer and available in /opt/lib/php/7.1/modules (PHP 7.1) or /opt/lib/php/modules (PHP 7.2/7.3):
+The following extensions are built into the layer and available in /opt/lib/php/${PHP_VERSION}/modules:
 
 ```
 bz2.so
@@ -45,20 +50,11 @@ ftp.so
 gettext.so
 iconv.so
 json.so
-mbstring.so
-mysqli.so
-mysqlnd.so
-pdo.so
-pdo_mysql.so
-pdo_pgsql.so
-pdo_sqlite.so
-pgsql.so
 phar.so
 posix.so
 shmop.so
 simplexml.so
 sockets.so
-sqlite3.so
 sysvmsg.so
 sysvsem.so
 sysvshm.so
@@ -77,7 +73,7 @@ These extensions are not loaded by default. You must add the extension to a php.
 extension=json.so
 ```
 
-Extensions can be built using the lambci/lambda:build-nodejs8.10 Docker image. It is recommended that custom extensions be provided by a separate Lambda Layer with the extension .so files placed in /lib/php/7.1/modules/ so they can be loaded alongside the built-in extensions listed above.
+Extensions can be built using the lambci/lambda:build-nodejs8.10 Docker image. It is recommended that custom extensions be provided by a separate Lambda Layer with the extension .so files placed in /lib/php/${PHP_VERSION}/modules/ so they can be loaded alongside the built-in extensions listed above.
 
 #### SAM Example
 Let's create an AWS SAM PHP application. We suggest using [Stackery](https://stackery.io) to make this super simple. It automates all the scaffolding shown below. But you may also choose to roll your own application from scratch.
@@ -110,7 +106,7 @@ Resources:
       Timeout: 30
       Tracing: Active
       Layers:
-        - !Sub arn:aws:lambda:${AWS::Region}:887080169480:layer:php71:6
+        - !Sub arn:aws:lambda:${AWS::Region}:887080169480:layer:php71:7
       Events:
         api:
           Type: Api
@@ -154,22 +150,34 @@ $ sam deploy \
 Build the layer by:
 
 1. Installing a Docker environment
-2. Running `make`
+1. Running `make`
 
-This will launch a Docker container that will build php71.zip, based on PHP 7.1 (default).
+This will launch a Docker container that will build php71.zip.
 
 You can run `make php72.zip` and `make php73.zip` to create a layer that is based on PHP 7.2/7.3.
 
+If you are behind a proxy server, just set the environment variable `http_proxy` before
+invoking `make`, eg.:
+
+```sh
+	$ export http_proyx=http://myproxy.acme.com:8080
+	$ make php73.zip
+```
+
 ### Debugging
 
 Run:
 
+```sh
 	$ docker run --rm -it -v `pwd`:/opt/layer lambci/lambda:build-nodejs8.10 /bin/bash
-	
+```
+
 If you are on Windows, run this instead:
 
+```sh
 	> docker run --rm -it -v %cd%:/opt/layer lambci/lambda:build-nodejs8.10 /bin/bash
-	
+```
+
 and execute manually the commands in the build.sh file.
 
 ### Disclaimer
@@ -185,4 +193,4 @@ and execute manually the commands in the build.sh file.
 > HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 > STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 > ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
-> OF THE POSSIBILITY OF SUCH DAMAGE.
+> OF THE POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
diff --git a/bootstrap b/bootstrap
index 73cce3f..c6f4d53 100755
--- a/bootstrap
+++ b/bootstrap
@@ -18,7 +18,8 @@ function start_webserver() {
       // exec the command
       $HANDLER = getenv('_HANDLER');
       chdir('/var/task');
-      exec("PHP_INI_SCAN_DIR=/opt/etc/php-7.1.d/:/var/task/php-7.1.d/ php -S localhost:8000 -c /var/task/php.ini -d extension_dir=/opt/lib/php/7.1/modules '$HANDLER'");
+	  $phpMinorVersion = PHP_MINOR_VERSION;
+      exec("PHP_INI_SCAN_DIR=/opt/etc/php-7.${phpMinorVersion}.d/:/var/task/php-7.${phpMinorVersion}.d/ php -S localhost:8000 -c /var/task/php.ini -d extension_dir=/opt/lib/php/7.${phpMinorVersion}/modules '$HANDLER'");
       exit;
 
     // return the child pid to parent
diff --git a/bootstrap-php72 b/bootstrap-php72
deleted file mode 100644
index 3abb20a..0000000
--- a/bootstrap-php72
+++ /dev/null
@@ -1,235 +0,0 @@
-#!/opt/bin/php -c/opt/php.ini
-<?php
-
-error_reporting(E_ALL | E_STRICT);
-
-$AWS_LAMBDA_RUNTIME_API = getenv('AWS_LAMBDA_RUNTIME_API');
-
-/* https://gist.github.com/henriquemoody/6580488 */
-$http_codes = [100=>'Continue',101=>'Switching Protocols',102=>'Processing',200=>'OK',201=>'Created',202=>'Accepted',203=>'Non-Authoritative Information',204=>'No Content',205=>'Reset Content',206=>'Partial Content',207=>'Multi-Status',208=>'Already Reported',226=>'IM Used',300=>'Multiple Choices',301=>'Moved Permanently',302=>'Found',303=>'See Other',304=>'Not Modified',305=>'Use Proxy',306=>'Switch Proxy',307=>'Temporary Redirect',308=>'Permanent Redirect',400=>'Bad Request',401=>'Unauthorized',402=>'Payment Required',403=>'Forbidden',404=>'Not Found',405=>'Method Not Allowed',406=>'Not Acceptable',407=>'Proxy Authentication Required',408=>'Request Timeout',409=>'Conflict',410=>'Gone',411=>'Length Required',412=>'Precondition Failed',413=>'Request Entity Too Large',414=>'Request-URI Too Long',415=>'Unsupported Media Type',416=>'Requested Range Not Satisfiable',417=>'Expectation Failed',418=>'I\'m a teapot',419=>'Authentication Timeout',420=>'Enhance Your Calm',420=>'Method Failure',422=>'Unprocessable Entity',423=>'Locked',424=>'Failed Dependency',424=>'Method Failure',425=>'Unordered Collection',426=>'Upgrade Required',428=>'Precondition Required',429=>'Too Many Requests',431=>'Request Header Fields Too Large',444=>'No Response',449=>'Retry With',450=>'Blocked by Windows Parental Controls',451=>'Redirect',451=>'Unavailable For Legal Reasons',494=>'Request Header Too Large',495=>'Cert Error',496=>'No Cert',497=>'HTTP to HTTPS',499=>'Client Closed Request',500=>'Internal Server Error',501=>'Not Implemented',502=>'Bad Gateway',503=>'Service Unavailable',504=>'Gateway Timeout',505=>'HTTP Version Not Supported',506=>'Variant Also Negotiates',507=>'Insufficient Storage',508=>'Loop Detected',509=>'Bandwidth Limit Exceeded',510=>'Not Extended',511=>'Network Authentication Required',598=>'Network read timeout error',599=>'Network connect timeout error'];
-
-function start_webserver() {
-  $pid = pcntl_fork();
-  switch($pid) {
-    case -1:
-      die('Failed to fork webserver process');
-
-    case 0:
-      // exec the command
-      $HANDLER = getenv('_HANDLER');
-      chdir('/var/task');
-      exec("PHP_INI_SCAN_DIR=/opt/etc/php-7.2.d/:/var/task/php-7.2.d/ php -S localhost:8000 -c /var/task/php.ini -d extension_dir=/opt/lib/php/modules '$HANDLER'");
-      exit;
-
-    // return the child pid to parent
-    default:
-      // Wait for child server to start
-      sleep(1);
-      return $pid;
-  }
-}
-
-function fail($AWS_LAMBDA_RUNTIME_API, $invocation_id, $message) {
-  $ch = curl_init("http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/$invocation_id/response");
-
-  $response = array();
-
-  $response['statusCode'] = 500;
-  $response['body'] = $message;
-
-  $response_json = json_encode($response);
-
-  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $response_json);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
-    'Content-Type: application/json',
-    'Content-Length: ' . strlen($response_json)
-  ));
-
-  curl_exec($ch);
-  curl_close($ch);
-}
-
-start_webserver();
-
-while (true) {
-  $ch = curl_init("http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/next");
-
-  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
-  curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
-
-  $invocation_id = '';
-
-  curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$invocation_id) {
-    if (!preg_match('/:\s*/', $header)) {
-      return strlen($header);
-    }
-
-    [$name, $value] = preg_split('/:\s*/', $header, 2);
-
-    if (strtolower($name) == 'lambda-runtime-aws-request-id') {
-      $invocation_id = trim($value);
-    }
-
-    return strlen($header);
-  });
-
-  $body = '';
-
-  curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $chunk) use (&$body) {
-    $body .= $chunk;
-
-    return strlen($chunk);
-  });
-
-  curl_exec($ch);
-
-  if (curl_error($ch)) {
-    die('Failed to fetch next Lambda invocation: ' . curl_error($ch) . "\n");
-  }
-
-  if ($invocation_id == '') {
-    die('Failed to determine Lambda invocation ID');
-  }
-
-  curl_close($ch);
-
-  if (!$body) {
-    die("Empty Lambda invocation response\n");
-  }
-
-  $event = json_decode($body, TRUE);
-
-  if (!array_key_exists('requestContext', $event)) {
-    fail($AWS_LAMBDA_RUNTIME_API, $invocation_id, 'Event is not an API Gateway request');
-    continue;
-  }
-
-  $uri = $event['path'];
-
-  if (array_key_exists('multiValueQueryStringParameters', $event) && $event['multiValueQueryStringParameters']) {
-    $first = TRUE;
-    foreach ($event['multiValueQueryStringParameters'] as $name => $values) {
-      foreach ($values as $value) {
-        if ($first) {
-          $uri .= "?";
-          $first = FALSE;
-        } else {
-          $uri .= "&";
-        }
-
-        $uri .= $name;
-
-        if ($value != '') {
-          $uri .= '=' . $value;
-        }
-      }
-    }
-  }
-
-  $ch = curl_init("http://localhost:8000$uri");
-
-  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
-
-  if (array_key_exists('multiValueHeaders', $event)) {
-    $headers = array();
-
-    foreach ($event['multiValueHeaders'] as $name => $values) {
-      foreach ($values as $value) {
-        array_push($headers, "${name}: ${value}");
-      }
-    }
-
-    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-  }
-
-  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $event['httpMethod']);
-
-  if (array_key_exists('body', $event)) {
-    $body = $event['body'];
-    if (array_key_exists('isBase64Encoded', $event) && $event['isBase64Encoded']) {
-      $body = base64_decode($body);
-    }
-  } else {
-    $body = '';
-  }
-
-  if (strlen($body) > 0) {
-    if($event['httpMethod'] === 'POST'){
-      curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
-    }    
-    curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body));
-    curl_setopt($ch, CURLOPT_READFUNCTION, function ($ch, $fd, $length) use ($body) {
-      return $body;
-    });
-  }
-
-  $response = array();
-  $response['multiValueHeaders'] = array();
-  $response['body'] = '';
-
-  curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$response) {
-    if (preg_match('/HTTP\/1.1 (\d+) .*/', $header, $matches)) {
-      $response['statusCode'] = intval($matches[1]);
-      return strlen($header);
-    }
-
-    if (!preg_match('/:\s*/', $header)) {
-      return strlen($header);
-    }
-
-    [$name, $value] = preg_split('/:\s*/', $header, 2);
-
-    $name = trim($name);
-    $value = trim($value);
-
-    if ($name == '') {
-      return strlen($header);
-    }
-
-    if (!array_key_exists($name, $response['multiValueHeaders'])) {
-      $response['multiValueHeaders'][$name] = array();
-    }
-
-    array_push($response['multiValueHeaders'][$name], $value);
-
-    return strlen($header);
-  });
-
-  curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $chunk) use (&$response) {
-    $response['body'] .= $chunk;
-
-    return strlen($chunk);
-  });
-
-  curl_exec($ch);
-  curl_close($ch);
-
-  $ch = curl_init("http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/$invocation_id/response");
-
-  $isALB = array_key_exists("elb", $event['requestContext']);
-  if ($isALB) { // Add Headers For ALB
-    $status = $response["statusCode"];
-    if (array_key_exists($status, $http_codes)) {
-        $response["statusDescription"] = "$status ". $http_codes[$status];
-    } else {
-        $response["statusDescription"] = "$status Unknown";
-    }
-    $response["isBase64Encoded"] = false;
-  }
-  $response_json = json_encode($response);
-  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $response_json);
-  if (!$isALB){
-    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
-      'Content-Type: application/json',
-      'Content-Length: ' . strlen($response_json)
-    ));
-  }
-  curl_exec($ch);
-  curl_close($ch);
-}
-
-?>
diff --git a/bootstrap-php73 b/bootstrap-php73
deleted file mode 100644
index 2d7ae93..0000000
--- a/bootstrap-php73
+++ /dev/null
@@ -1,235 +0,0 @@
-#!/opt/bin/php -c/opt/php.ini
-<?php
-
-error_reporting(E_ALL | E_STRICT);
-
-$AWS_LAMBDA_RUNTIME_API = getenv('AWS_LAMBDA_RUNTIME_API');
-
-/* https://gist.github.com/henriquemoody/6580488 */
-$http_codes = [100=>'Continue',101=>'Switching Protocols',102=>'Processing',200=>'OK',201=>'Created',202=>'Accepted',203=>'Non-Authoritative Information',204=>'No Content',205=>'Reset Content',206=>'Partial Content',207=>'Multi-Status',208=>'Already Reported',226=>'IM Used',300=>'Multiple Choices',301=>'Moved Permanently',302=>'Found',303=>'See Other',304=>'Not Modified',305=>'Use Proxy',306=>'Switch Proxy',307=>'Temporary Redirect',308=>'Permanent Redirect',400=>'Bad Request',401=>'Unauthorized',402=>'Payment Required',403=>'Forbidden',404=>'Not Found',405=>'Method Not Allowed',406=>'Not Acceptable',407=>'Proxy Authentication Required',408=>'Request Timeout',409=>'Conflict',410=>'Gone',411=>'Length Required',412=>'Precondition Failed',413=>'Request Entity Too Large',414=>'Request-URI Too Long',415=>'Unsupported Media Type',416=>'Requested Range Not Satisfiable',417=>'Expectation Failed',418=>'I\'m a teapot',419=>'Authentication Timeout',420=>'Enhance Your Calm',420=>'Method Failure',422=>'Unprocessable Entity',423=>'Locked',424=>'Failed Dependency',424=>'Method Failure',425=>'Unordered Collection',426=>'Upgrade Required',428=>'Precondition Required',429=>'Too Many Requests',431=>'Request Header Fields Too Large',444=>'No Response',449=>'Retry With',450=>'Blocked by Windows Parental Controls',451=>'Redirect',451=>'Unavailable For Legal Reasons',494=>'Request Header Too Large',495=>'Cert Error',496=>'No Cert',497=>'HTTP to HTTPS',499=>'Client Closed Request',500=>'Internal Server Error',501=>'Not Implemented',502=>'Bad Gateway',503=>'Service Unavailable',504=>'Gateway Timeout',505=>'HTTP Version Not Supported',506=>'Variant Also Negotiates',507=>'Insufficient Storage',508=>'Loop Detected',509=>'Bandwidth Limit Exceeded',510=>'Not Extended',511=>'Network Authentication Required',598=>'Network read timeout error',599=>'Network connect timeout error'];
-
-function start_webserver() {
-  $pid = pcntl_fork();
-  switch($pid) {
-    case -1:
-      die('Failed to fork webserver process');
-
-    case 0:
-      // exec the command
-      $HANDLER = getenv('_HANDLER');
-      chdir('/var/task');
-      exec("PHP_INI_SCAN_DIR=/opt/etc/php-7.3.d/:/var/task/php-7.3.d/ php -S localhost:8000 -c /var/task/php.ini -d extension_dir=/opt/lib/php/modules '$HANDLER'");
-      exit;
-
-    // return the child pid to parent
-    default:
-      // Wait for child server to start
-      sleep(1);
-      return $pid;
-  }
-}
-
-function fail($AWS_LAMBDA_RUNTIME_API, $invocation_id, $message) {
-  $ch = curl_init("http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/$invocation_id/response");
-
-  $response = array();
-
-  $response['statusCode'] = 500;
-  $response['body'] = $message;
-
-  $response_json = json_encode($response);
-
-  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $response_json);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
-    'Content-Type: application/json',
-    'Content-Length: ' . strlen($response_json)
-  ));
-
-  curl_exec($ch);
-  curl_close($ch);
-}
-
-start_webserver();
-
-while (true) {
-  $ch = curl_init("http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/next");
-
-  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
-  curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
-
-  $invocation_id = '';
-
-  curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$invocation_id) {
-    if (!preg_match('/:\s*/', $header)) {
-      return strlen($header);
-    }
-
-    [$name, $value] = preg_split('/:\s*/', $header, 2);
-
-    if (strtolower($name) == 'lambda-runtime-aws-request-id') {
-      $invocation_id = trim($value);
-    }
-
-    return strlen($header);
-  });
-
-  $body = '';
-
-  curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $chunk) use (&$body) {
-    $body .= $chunk;
-
-    return strlen($chunk);
-  });
-
-  curl_exec($ch);
-
-  if (curl_error($ch)) {
-    die('Failed to fetch next Lambda invocation: ' . curl_error($ch) . "\n");
-  }
-
-  if ($invocation_id == '') {
-    die('Failed to determine Lambda invocation ID');
-  }
-
-  curl_close($ch);
-
-  if (!$body) {
-    die("Empty Lambda invocation response\n");
-  }
-
-  $event = json_decode($body, TRUE);
-
-  if (!array_key_exists('requestContext', $event)) {
-    fail($AWS_LAMBDA_RUNTIME_API, $invocation_id, 'Event is not an API Gateway request');
-    continue;
-  }
-
-  $uri = $event['path'];
-
-  if (array_key_exists('multiValueQueryStringParameters', $event) && $event['multiValueQueryStringParameters']) {
-    $first = TRUE;
-    foreach ($event['multiValueQueryStringParameters'] as $name => $values) {
-      foreach ($values as $value) {
-        if ($first) {
-          $uri .= "?";
-          $first = FALSE;
-        } else {
-          $uri .= "&";
-        }
-
-        $uri .= $name;
-
-        if ($value != '') {
-          $uri .= '=' . $value;
-        }
-      }
-    }
-  }
-
-  $ch = curl_init("http://localhost:8000$uri");
-
-  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
-
-  if (array_key_exists('multiValueHeaders', $event)) {
-    $headers = array();
-
-    foreach ($event['multiValueHeaders'] as $name => $values) {
-      foreach ($values as $value) {
-        array_push($headers, "${name}: ${value}");
-      }
-    }
-
-    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-  }
-
-  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $event['httpMethod']);
-
-  if (array_key_exists('body', $event)) {
-    $body = $event['body'];
-    if (array_key_exists('isBase64Encoded', $event) && $event['isBase64Encoded']) {
-      $body = base64_decode($body);
-    }
-  } else {
-    $body = '';
-  }
-
-  if (strlen($body) > 0) {
-    if($event['httpMethod'] === 'POST'){
-      curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
-    }    
-    curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body));
-    curl_setopt($ch, CURLOPT_READFUNCTION, function ($ch, $fd, $length) use ($body) {
-      return $body;
-    });
-  }
-
-  $response = array();
-  $response['multiValueHeaders'] = array();
-  $response['body'] = '';
-
-  curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$response) {
-    if (preg_match('/HTTP\/1.1 (\d+) .*/', $header, $matches)) {
-      $response['statusCode'] = intval($matches[1]);
-      return strlen($header);
-    }
-
-    if (!preg_match('/:\s*/', $header)) {
-      return strlen($header);
-    }
-
-    [$name, $value] = preg_split('/:\s*/', $header, 2);
-
-    $name = trim($name);
-    $value = trim($value);
-
-    if ($name == '') {
-      return strlen($header);
-    }
-
-    if (!array_key_exists($name, $response['multiValueHeaders'])) {
-      $response['multiValueHeaders'][$name] = array();
-    }
-
-    array_push($response['multiValueHeaders'][$name], $value);
-
-    return strlen($header);
-  });
-
-  curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $chunk) use (&$response) {
-    $response['body'] .= $chunk;
-
-    return strlen($chunk);
-  });
-
-  curl_exec($ch);
-  curl_close($ch);
-
-  $ch = curl_init("http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/$invocation_id/response");
-
-  $isALB = array_key_exists("elb", $event['requestContext']);
-  if ($isALB) { // Add Headers For ALB
-    $status = $response["statusCode"];
-    if (array_key_exists($status, $http_codes)) {
-        $response["statusDescription"] = "$status ". $http_codes[$status];
-    } else {
-        $response["statusDescription"] = "$status Unknown";
-    }
-    $response["isBase64Encoded"] = false;
-  }
-  $response_json = json_encode($response);
-  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $response_json);
-  if (!$isALB){
-    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
-      'Content-Type: application/json',
-      'Content-Length: ' . strlen($response_json)
-    ));
-  }
-  curl_exec($ch);
-  curl_close($ch);
-}
-
-?>
diff --git a/build-php72.sh b/build-php-remi.sh
similarity index 53%
rename from build-php72.sh
rename to build-php-remi.sh
index 387ef8e..c0d0ee0 100644
--- a/build-php72.sh
+++ b/build-php-remi.sh
@@ -1,5 +1,9 @@
 #!/bin/bash
 
+PHP_MINOR_VERSION=$1
+
+echo "Building layer for PHP 7.$PHP_MINOR_VERSION - using Remi repository"
+
 yum install -y wget
 yum install -y yum-utils
 wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
@@ -8,19 +12,19 @@ rpm -Uvh remi-release-6.rpm
 rpm -Uvh epel-release-latest-6.noarch.rpm
 
 
-yum-config-manager --enable remi-php72
+yum-config-manager --enable remi-php7${PHP_MINOR_VERSION}
 
 yum install -y httpd
 yum install -y postgresql-devel
 yum install -y libargon2-devel
 
-yum install -y --disablerepo="*" --enablerepo="remi,remi-php72" php php-mbstring php-pdo php-mysql php-pgsql
+yum install -y --disablerepo="*" --enablerepo="remi,remi-php7${PHP_MINOR_VERSION}" php php-mbstring php-pdo php-mysql php-pgsql
 
 
 mkdir /tmp/layer
 cd /tmp/layer
-cp /opt/layer/bootstrap-php72 bootstrap
-cp /opt/layer/php72.ini php.ini
+cp /opt/layer/bootstrap bootstrap
+sed "s/PHP_MINOR_VERSION/${PHP_MINOR_VERSION}/g" /opt/layer/php.ini >php.ini
 
 mkdir bin
 cp /usr/bin/php bin/
@@ -34,7 +38,8 @@ cp /usr/lib64/libedit.so.0 lib/
 cp /usr/lib64/libargon2.so.0 lib/
 cp /usr/lib64/libpq.so.5 lib/
 
-cp -a /usr/lib64/php lib/
+mkdir -p lib/php/7.${PHP_MINOR_VERSION}
+cp -a /usr/lib64/php/modules lib/php/7.${PHP_MINOR_VERSION}/
 
-zip -r /opt/layer/php72.zip .
+zip -r /opt/layer/php7${PHP_MINOR_VERSION}.zip .
 
diff --git a/build-php73.sh b/build-php73.sh
deleted file mode 100644
index 079816d..0000000
--- a/build-php73.sh
+++ /dev/null
@@ -1,40 +0,0 @@
-#!/bin/bash
-
-yum install -y wget
-yum install -y yum-utils
-wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
-wget https://rpms.remirepo.net/enterprise/remi-release-6.rpm
-rpm -Uvh remi-release-6.rpm
-rpm -Uvh epel-release-latest-6.noarch.rpm
-
-
-yum-config-manager --enable remi-php73
-
-yum install -y httpd
-yum install -y postgresql-devel
-yum install -y libargon2-devel
-
-yum install -y --disablerepo="*" --enablerepo="remi,remi-php73" php php-mbstring php-pdo php-mysql php-pgsql
-
-
-mkdir /tmp/layer
-cd /tmp/layer
-cp /opt/layer/bootstrap-php73 bootstrap
-cp /opt/layer/php73.ini php.ini
-
-mkdir bin
-cp /usr/bin/php bin/
-
-mkdir lib
-for lib in libncurses.so.5 libtinfo.so.5 libpcre.so.0; do
-  cp "/lib64/${lib}" lib/
-done
-
-cp /usr/lib64/libedit.so.0 lib/
-cp /usr/lib64/libargon2.so.0 lib/
-cp /usr/lib64/libpq.so.5 lib/
-
-cp -a /usr/lib64/php lib/
-
-zip -r /opt/layer/php73.zip .
-
diff --git a/php.ini b/php.ini
index b81ef93..2f19388 100644
--- a/php.ini
+++ b/php.ini
@@ -1,38 +1,5 @@
-extension_dir=/opt/lib/php/7.1/modules
+extension_dir=/opt/lib/php/7.PHP_MINOR_VERSION/modules
 display_errors=On
 
-;extension=bz2.so
-;extension=calendar.so
-;extension=ctype.so
 extension=curl.so
-;extension=dom.so
-;extension=exif.so
-;extension=fileinfo.so
-;extension=ftp.so
-;extension=gettext.so
-;extension=iconv.so
 extension=json.so
-;extension=mbstring.so
-;extension=mysqli.so
-;extension=mysqlnd.so
-;extension=pdo.so
-;extension=pdo_mysql.so
-;extension=pdo_pgsql.so
-;extension=pdo_sqlite.so
-;extension=pgsql.so
-;extension=phar.so
-;extension=posix.so
-;extension=shmop.so
-;extension=simplexml.so
-;extension=sockets.so
-;extension=sqlite3.so
-;extension=sysvmsg.so
-;extension=sysvsem.so
-;extension=sysvshm.so
-;extension=tokenizer.so
-;extension=wddx.so
-;extension=xml.so
-;extension=xmlreader.so
-;extension=xmlwriter.so
-;extension=xsl.so
-;extension=zip.so
diff --git a/php72.ini b/php72.ini
deleted file mode 100644
index a2f7c65..0000000
--- a/php72.ini
+++ /dev/null
@@ -1,38 +0,0 @@
-extension_dir=/opt/lib/php/modules
-display_errors=On
-
-;extension=bz2.so
-;extension=calendar.so
-;extension=ctype.so
-extension=curl.so
-;extension=dom.so
-;extension=exif.so
-;extension=fileinfo.so
-;extension=ftp.so
-;extension=gettext.so
-;extension=iconv.so
-extension=json.so
-;extension=mbstring.so
-;extension=mysqli.so
-;extension=mysqlnd.so
-;extension=pdo.so
-;extension=pdo_mysql.so
-;extension=pdo_pgsql.so
-;extension=pdo_sqlite.so
-;extension=pgsql.so
-;extension=phar.so
-;extension=posix.so
-;extension=shmop.so
-;extension=simplexml.so
-;extension=sockets.so
-;extension=sqlite3.so
-;extension=sysvmsg.so
-;extension=sysvsem.so
-;extension=sysvshm.so
-;extension=tokenizer.so
-;extension=wddx.so
-;extension=xml.so
-;extension=xmlreader.so
-;extension=xmlwriter.so
-;extension=xsl.so
-;extension=zip.so
diff --git a/php73.ini b/php73.ini
deleted file mode 100644
index a2f7c65..0000000
--- a/php73.ini
+++ /dev/null
@@ -1,38 +0,0 @@
-extension_dir=/opt/lib/php/modules
-display_errors=On
-
-;extension=bz2.so
-;extension=calendar.so
-;extension=ctype.so
-extension=curl.so
-;extension=dom.so
-;extension=exif.so
-;extension=fileinfo.so
-;extension=ftp.so
-;extension=gettext.so
-;extension=iconv.so
-extension=json.so
-;extension=mbstring.so
-;extension=mysqli.so
-;extension=mysqlnd.so
-;extension=pdo.so
-;extension=pdo_mysql.so
-;extension=pdo_pgsql.so
-;extension=pdo_sqlite.so
-;extension=pgsql.so
-;extension=phar.so
-;extension=posix.so
-;extension=shmop.so
-;extension=simplexml.so
-;extension=sockets.so
-;extension=sqlite3.so
-;extension=sysvmsg.so
-;extension=sysvsem.so
-;extension=sysvshm.so
-;extension=tokenizer.so
-;extension=wddx.so
-;extension=xml.so
-;extension=xmlreader.so
-;extension=xmlwriter.so
-;extension=xsl.so
-;extension=zip.so
diff --git a/tmp/test/due b/tmp/test/due
new file mode 100644
index 0000000..e69de29
diff --git a/tmp/test/uno b/tmp/test/uno
new file mode 100644
index 0000000..e69de29

From be24d6f54907f313ef66c9f056074fbcb14e4651 Mon Sep 17 00:00:00 2001
From: Pietro Iglio <igliop@gmail.com>
Date: Fri, 1 Feb 2019 07:54:04 +0100
Subject: [PATCH 2/4] removed test files

---
 tmp/test/due | 0
 tmp/test/uno | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 tmp/test/due
 delete mode 100644 tmp/test/uno

diff --git a/tmp/test/due b/tmp/test/due
deleted file mode 100644
index e69de29..0000000
diff --git a/tmp/test/uno b/tmp/test/uno
deleted file mode 100644
index e69de29..0000000

From 3e6f2d8ba0e9c07b8fe45cdb48c5275df84b878a Mon Sep 17 00:00:00 2001
From: Pietro Iglio <igliop@gmail.com>
Date: Fri, 1 Feb 2019 08:24:17 +0100
Subject: [PATCH 3/4] minor changes

---
 bootstrap | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bootstrap b/bootstrap
index c6f4d53..527a1df 100755
--- a/bootstrap
+++ b/bootstrap
@@ -18,7 +18,7 @@ function start_webserver() {
       // exec the command
       $HANDLER = getenv('_HANDLER');
       chdir('/var/task');
-	  $phpMinorVersion = PHP_MINOR_VERSION;
+      $phpMinorVersion = PHP_MINOR_VERSION;
       exec("PHP_INI_SCAN_DIR=/opt/etc/php-7.${phpMinorVersion}.d/:/var/task/php-7.${phpMinorVersion}.d/ php -S localhost:8000 -c /var/task/php.ini -d extension_dir=/opt/lib/php/7.${phpMinorVersion}/modules '$HANDLER'");
       exit;
 

From 064470715a526bc65f0a870a5e412e39be58874f Mon Sep 17 00:00:00 2001
From: Pietro Iglio <igliop@gmail.com>
Date: Fri, 1 Feb 2019 08:25:13 +0100
Subject: [PATCH 4/4] minor changes

---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index fc8714e..9d4e5ce 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
 ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
 
 php71.zip:
-	docker run --rm -v $(ROOT_DIR):/opt/layer lambci/lambda:build-nodejs8.10 /opt/layer/build.sh
+	docker run --rm -e http_proxy=${http_proxy} -v $(ROOT_DIR):/opt/layer lambci/lambda:build-nodejs8.10 /opt/layer/build.sh
 
 php72.zip:
 	docker run --rm -e http_proxy=${http_proxy} -v $(ROOT_DIR):/opt/layer lambci/lambda:build-nodejs8.10 /opt/layer/build-php-remi.sh 2
