Skip to content

Commit a11f839

Browse files
authored
Merge pull request #41 from brefphp/fix-ini-override
2 parents b4e3533 + 622f3d6 commit a11f839

6 files changed

Lines changed: 90 additions & 3 deletions

File tree

layers/fpm/bootstrap.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ set -e
55

66
# We don't compile PHP anymore, so the only way to configure where PHP looks for
77
# .ini files is via the PHP_INI_SCAN_DIR environment variable.
8-
export PHP_INI_SCAN_DIR="/opt/bref/etc/php/conf.d:/var/task/php/conf.d"
8+
# Note: we use that weird syntax to set the variable only if it's not already set (can be overridden)
9+
: "${PHP_INI_SCAN_DIR:='/opt/bref/etc/php/conf.d:/var/task/php/conf.d'}"
10+
export PHP_INI_SCAN_DIR
911

1012
# We redirect stderr to stdout so that everything
1113
# written on the output ends up in Cloudwatch automatically

layers/function/bootstrap.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ set -e
55

66
# We don't compile PHP anymore, so the only way to configure where PHP looks for
77
# .ini files is via the PHP_INI_SCAN_DIR environment variable.
8-
export PHP_INI_SCAN_DIR="/opt/bref/etc/php/conf.d:/var/task/php/conf.d"
8+
# Note: we use that weird syntax to set the variable only if it's not already set (can be overridden)
9+
: "${PHP_INI_SCAN_DIR:='/opt/bref/etc/php/conf.d:/var/task/php/conf.d'}"
10+
export PHP_INI_SCAN_DIR
911

1012
while true
1113
do

tests/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ test-%: vendor
2525
docker-compose up --detach php-$*-console-handler
2626
docker-compose exec -T php-$*-console-handler php test_6_console_invocation.php \
2727
|| (docker-compose logs php-$*-console-handler && exit 1) # print logs in case of failure
28+
# Test that we can override PHP_INI_SCAN_DIR
29+
docker-compose up --detach php-$*-handler-test7
30+
docker-compose exec -T php-$*-handler-test7 php test_7_custom_ini_scan_dir.php \
31+
|| (docker-compose logs php-$*-handler && exit 1) # print logs in case of failure
2832
# Clean up containers
2933
docker-compose down
3034
echo "\033[1;32m✓ Tests succeeded\033[0m"

tests/docker-compose.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ services:
1313
ports: [ '9001:8080' ]
1414
command: test_4_function_handler.php
1515

16+
php-80-handler-test7:
17+
image: bref/${CPU_PREFIX}php-80
18+
volumes: [ '.:/var/task:ro' ]
19+
ports: [ '9004:8080' ]
20+
command: test_4_function_handler.php
21+
environment:
22+
# Override for test 7
23+
PHP_INI_SCAN_DIR: "/opt/bref/etc/php/conf.d/:/var/task/"
24+
1625
php-80-fpm-handler:
1726
image: bref/${CPU_PREFIX}php-80-fpm
1827
volumes: [ '.:/var/task:ro' ]
@@ -36,6 +45,15 @@ services:
3645
ports: [ '9001:8080' ]
3746
command: test_4_function_handler.php
3847

48+
php-81-handler-test7:
49+
image: bref/${CPU_PREFIX}php-81
50+
volumes: [ '.:/var/task:ro' ]
51+
ports: [ '9004:8080' ]
52+
command: test_4_function_handler.php
53+
environment:
54+
# Override for test 7
55+
PHP_INI_SCAN_DIR: "/opt/bref/etc/php/conf.d/:/var/task/"
56+
3957
php-81-fpm-handler:
4058
image: bref/${CPU_PREFIX}php-81-fpm
4159
volumes: [ '.:/var/task:ro' ]
@@ -59,6 +77,15 @@ services:
5977
ports: [ '9001:8080' ]
6078
command: test_4_function_handler.php
6179

80+
php-82-handler-test7:
81+
image: bref/${CPU_PREFIX}php-82
82+
volumes: [ '.:/var/task:ro' ]
83+
ports: [ '9004:8080' ]
84+
command: test_4_function_handler.php
85+
environment:
86+
# Override for test 7
87+
PHP_INI_SCAN_DIR: "/opt/bref/etc/php/conf.d/:/var/task/"
88+
6289
php-82-fpm-handler:
6390
image: bref/${CPU_PREFIX}php-82-fpm
6491
volumes: [ '.:/var/task:ro' ]

tests/test_4_function_handler.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
<?php declare(strict_types=1);
22

33
return function ($event, \Bref\Context\Context $context) {
4+
// Support for test 7
5+
if ($event === 'list_extensions') {
6+
return get_loaded_extensions();
7+
}
8+
49
return [
510
'event' => $event,
611
'server' => $_SERVER,
712
'memory_limit' => ini_get('memory_limit'),
813
];
9-
};
14+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types=1);
2+
3+
require_once __DIR__ . '/utils.php';
4+
5+
function post(string $url, string $params)
6+
{
7+
$ch = curl_init();
8+
9+
$jsonData = json_encode($params);
10+
11+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
12+
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Content-Length: ' . strlen($jsonData)]);
13+
curl_setopt($ch, CURLOPT_URL, $url);
14+
curl_setopt($ch, CURLOPT_POST, true);
15+
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
16+
17+
$response = curl_exec($ch);
18+
19+
curl_close($ch);
20+
21+
if ($response === false) {
22+
throw new Exception('Curl error: ' . curl_error($ch));
23+
}
24+
25+
return $response;
26+
}
27+
28+
$body = 'list_extensions';
29+
30+
try {
31+
$response = post('http://127.0.0.1:8080/2015-03-31/functions/function/invocations', $body);
32+
$response = json_decode($response, true, 512, JSON_THROW_ON_ERROR);
33+
} catch (Throwable $e) {
34+
error($e->getMessage() . PHP_EOL . $e->getTraceAsString());
35+
}
36+
37+
if (! is_array($response)) {
38+
error('The response is not an array');
39+
}
40+
41+
// We changed PHP_INI_SCAN_DIR to `/var/task` to load `test_3_manual_extensions.ini`
42+
// We check one of the extensions was indeed loaded
43+
if (! in_array('intl', $response, true)) {
44+
error('Could not override PHP_INI_SCAN_DIR, test_3_manual_extensions.ini was not loaded');
45+
}
46+
47+
success('[Invoke] Function');

0 commit comments

Comments
 (0)