Skip to content

Commit dd08c35

Browse files
committed
Replace all $_ENV accesses by getenv()
Fixes #10
1 parent 14d451c commit dd08c35

File tree

5 files changed

+38
-41
lines changed

5 files changed

+38
-41
lines changed

ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ AWS Lambda change log
33

44
## ?.?.? / ????-??-??
55

6+
## 2.1.1 / 2021-09-15
7+
8+
* Fixed issue #10: lang.IndexOutOfBoundsException (Undefined index: XP_VERSION)
9+
on systems where the PHP *variables_order* setting does not include `E`.
10+
(@thekid)
11+
612
## 2.1.0 / 2021-08-30
713

814
* Sped up build by using `-j $(nproc)` flag for `make`, see #7 - @thekid

src/main/php/com/amazon/aws/lambda/Environment.class.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ class Environment {
1515
public $root, $writer, $properties;
1616

1717
/** Creates a new environment */
18-
public function __construct(string $root, StringWriter $writer= null) {
18+
public function __construct(string $root, StringWriter $writer= null, array $variables= null) {
1919
$this->root= $root;
20+
$this->variables= $variables ?? (PHP_VERSION_ID >= 70100 ? getenv() : $_SERVER);
2021
$this->writer= $writer ?? Console::$out;
2122
$this->properties= new FilesystemPropertySource($root);
2223
}
@@ -29,8 +30,8 @@ public function path(string $path): Path { return new Path($this->root, $path);
2930

3031
/** Returns temporary directory */
3132
public function tempDir(): Path {
32-
foreach (['TEMP', 'TMP', 'TMPDIR', 'TEMPDIR'] as $variant) {
33-
if (isset($_ENV[$variant])) return new Path($_ENV[$variant]);
33+
foreach (['TEMP', 'TMP', 'TMPDIR', 'TEMPDIR'] as $variable) {
34+
if (isset($this->variables[$variable])) return new Path($this->variables[$variable]);
3435
}
3536
return new Path(sys_get_temp_dir());
3637
}
@@ -42,7 +43,7 @@ public function tempDir(): Path {
4243
* @return ?string
4344
*/
4445
public function variable($name) {
45-
return $_ENV[$name] ?? null;
46+
return $this->variables[$name] ?? null;
4647
}
4748

4849
/**

src/main/php/xp/lambda/AwsRunner.class.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Throwable;
44
use com\amazon\aws\lambda\{Context, Environment};
55
use io\IOException;
6-
use lang\{XPClass, XPException};
6+
use lang\{XPClass, XPException, Environment as System};
77
use peer\http\{HttpConnection, RequestData};
88
use text\json\{Json, StreamInput};
99
use util\cmd\Console;
@@ -16,25 +16,32 @@
1616
class AwsRunner {
1717

1818
/**
19-
* Returns the lambda handler class using the `_HANDLER` environment
20-
* variable.
19+
* Returns the lambda handler instance using the `_HANDLER` and
20+
* `LAMBDA_TASK_ROOT` environment variables.
2121
*
22-
* @return lang.XPClass
22+
* @param [:string] $environment
23+
* @param io.streams.StringWriter $writer
24+
* @return com.amazon.aws.lambda.Handler
2325
*/
24-
private static function handler() {
25-
return XPClass::forName($_ENV['_HANDLER']);
26+
private static function handler($environment, $writer) {
27+
return XPClass::forName($environment['_HANDLER'])->newInstance(new Environment(
28+
$environment['LAMBDA_TASK_ROOT'] ?? '.',
29+
$writer,
30+
$environment
31+
));
2632
}
2733

2834
/**
2935
* Returns a lambda API endpoint using the `AWS_LAMBDA_RUNTIME_API`
3036
* environment variable.
3137
*
3238
* @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html
39+
* @param [:string] $environment
3340
* @param string $path
3441
* @return peer.http.HttpConnection
3542
*/
36-
private static function endpoint($path) {
37-
$c= new HttpConnection("http://{$_ENV['AWS_LAMBDA_RUNTIME_API']}/2018-06-01/runtime/{$path}");
43+
private static function endpoint($environment, $path) {
44+
$c= new HttpConnection("http://{$environment['AWS_LAMBDA_RUNTIME_API']}/2018-06-01/runtime/{$path}");
3845

3946
// Use a 15 minute timeout, this is the maximum lambda runtime, see
4047
// https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html
@@ -95,12 +102,13 @@ private static function error($e) {
95102
* @return int
96103
*/
97104
public static function main($args) {
105+
$environment= System::variables();
98106

99107
// Initialization
100108
try {
101-
$lambda= self::handler()->newInstance(new Environment($_ENV['LAMBDA_TASK_ROOT'], Console::$out))->lambda();
109+
$lambda= self::handler($environment, Console::$out)->lambda();
102110
} catch (Throwable $t) {
103-
self::endpoint('init/error')->post(
111+
self::endpoint($environment, 'init/error')->post(
104112
new RequestData(self::error($t)),
105113
['Content-Type' => 'application/json']
106114
);
@@ -110,13 +118,13 @@ public static function main($args) {
110118
// Process events using the lambda runtime interface
111119
do {
112120
try {
113-
$r= self::endpoint('invocation/next')->get();
121+
$r= self::endpoint($environment, 'invocation/next')->get();
114122
} catch (IOException $e) {
115123
Console::$err->writeLine($e);
116124
break;
117125
}
118126

119-
$context= new Context($r->headers(), $_ENV);
127+
$context= new Context($r->headers(), $environment);
120128
try {
121129
$event= 0 === $context->payloadLength ? null : self::read($r->in());
122130

@@ -127,7 +135,7 @@ public static function main($args) {
127135
$response= self::error($t);
128136
}
129137

130-
self::endpoint("invocation/{$context->awsRequestId}/{$type}")->post(
138+
self::endpoint($environment, "invocation/{$context->awsRequestId}/{$type}")->post(
131139
new RequestData($response),
132140
['Content-Type' => 'application/json']
133141
);

src/main/php/xp/lambda/Docker.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private function image(string $name, string $version, array $dependencies= [], b
2020
if (empty($out)) {
2121

2222
// Support 3-digit `6.1.0` as well as 4-digit `6.1.0.1234` formats
23-
$runners= preg_replace('/^(\d+\.\d+\.\d+)(.*)/', '$1', $_ENV['XP_VERSION']);
23+
$runners= preg_replace('/^(\d+\.\d+\.\d+)(.*)/', '$1', getenv('XP_VERSION'));
2424

2525
// Ensure dependencies exist
2626
foreach ($dependencies as $dependency => $transitive) {

src/test/php/com/amazon/aws/lambda/unittest/EnvironmentTest.class.php

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,10 @@
55
use io\{Path, File, Files};
66
use lang\ElementNotFoundException;
77
use lang\Environment as System;
8-
use unittest\{Assert, After, Before, Test};
8+
use unittest\{Assert, Test};
99
use util\Properties;
1010

1111
class EnvironmentTest {
12-
private $temp= [];
13-
14-
#[Before]
15-
public function cleanTemp() {
16-
foreach (['TEMP', 'TMP', 'TMPDIR', 'TEMPDIR'] as $variable) {
17-
$this->temp[$variable]= $_ENV[$variable] ?? null;
18-
unset($_ENV[$variable]);
19-
}
20-
}
21-
22-
#[After]
23-
public function restoreTemp() {
24-
$_ENV += $this->temp;
25-
}
2612

2713
#[Test]
2814
public function can_create() {
@@ -41,26 +27,22 @@ public function path() {
4127

4228
#[Test]
4329
public function tempDir_prefers_using_environment() {
44-
$_ENV['TEMP']= 'tmp';
45-
Assert::equals(new Path('tmp'), (new Environment('.'))->tempDir());
30+
Assert::equals(new Path('tmp'), (new Environment('.', null, ['TEMP' => 'tmp']))->tempDir());
4631
}
4732

4833
#[Test]
4934
public function tempDir_falls_back_to_sys_get_temp_dir() {
50-
unset($_ENV['TEMP']);
51-
Assert::equals(new Path(sys_get_temp_dir()), (new Environment('.'))->tempDir());
35+
Assert::equals(new Path(sys_get_temp_dir()), (new Environment('.', null, []))->tempDir());
5236
}
5337

5438
#[Test]
5539
public function variable() {
56-
$_ENV['TEST']= 'true';
57-
Assert::equals('true', (new Environment('.'))->variable('TEST'));
40+
Assert::equals('true', (new Environment('.', null, ['TEST' => 'true']))->variable('TEST'));
5841
}
5942

6043
#[Test]
6144
public function non_existant_variable() {
62-
unset($_ENV['TEST']);
63-
Assert::null((new Environment('.'))->variable('TEST'));
45+
Assert::null((new Environment('.', null, []))->variable('TEST'));
6446
}
6547

6648
#[Test]

0 commit comments

Comments
 (0)