Skip to content

Commit ac057e1

Browse files
committed
Add tempDir() accessor
1 parent cf902eb commit ac057e1

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ AWS Lambda change log
55

66
## 1.1.0 / 2021-08-28
77

8+
* Added accessor for temporary directory, `Environment::tempDir()`
9+
(@thekid)
810
* Added accessor for environment root path, `Environment::taskroot()`
911
(@thekid)
1012

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public function taskroot(): Path { return new Path($this->root); }
2727
/** Returns a path inside this environment's root path */
2828
public function path(string $path): Path { return new Path($this->root, $path); }
2929

30+
/** Returns temporary directory */
31+
public function tempDir(): Path {
32+
foreach (['TEMP', 'TMP', 'TMPDIR', 'TEMPDIR'] as $variant) {
33+
if (isset($_ENV[$variant])) return new Path($_ENV[$variant]);
34+
}
35+
return new Path(sys_get_temp_dir());
36+
}
37+
3038
/**
3139
* Writes a trace message
3240
*

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public function path() {
2525
Assert::equals(new Path('./etc'), (new Environment('.'))->path('etc'));
2626
}
2727

28+
#[Test]
29+
public function tempDir() {
30+
Assert::instance(Path::class, (new Environment('.'))->tempDir());
31+
}
32+
2833
#[Test]
2934
public function trace() {
3035
$stream= new MemoryOutputStream();

0 commit comments

Comments
 (0)