Skip to content

Commit 5a971d5

Browse files
committed
Move IS_[FILE|FOLDER|LINK] constants to Sources class
1 parent 036bcb7 commit 5a971d5

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
class PackageLambda {
1010
const COMPRESSION_THRESHOLD= 24;
1111

12-
// See https://www.php.net/manual/en/function.fileperms.php
13-
const IS_LINK= 0120000;
14-
const IS_FILE= 0100000;
15-
const IS_FOLDER= 0040000;
16-
1712
private $target, $sources, $exclude, $compression;
1813

1914
public function __construct(Path $target, Sources $sources, string $exclude= '#(^|/)(\..+|src/test|src/it)(/|$)#') {
@@ -42,13 +37,13 @@ private function add(ZipArchiveWriter $zip, Path $path, Path $base, $prefix= '')
4237
// - Links: Resolve, then handle link targets
4338
// - Files: Add to ZIP
4439
// - Folders: Recursively add all subfolders and files therein
45-
if (self::IS_LINK === ($stat['mode'] & self::IS_LINK)) {
40+
if (Sources::IS_LINK === ($stat['mode'] & Sources::IS_LINK)) {
4641
$resolved= Path::real([dirname($path), readlink($path)], $base);
4742
if ($resolved->exists()) {
4843
$base= $resolved->isFile() ? new Path(dirname($resolved)) : $resolved;
4944
yield from $this->add($zip, $resolved, $base, $relative.DIRECTORY_SEPARATOR);
5045
}
51-
} else if (self::IS_FILE === ($stat['mode'] & self::IS_FILE)) {
46+
} else if (Sources::IS_FILE === ($stat['mode'] & Sources::IS_FILE)) {
5247
$file= $zip->add(new ZipFileEntry($relative));
5348

5449
// See https://stackoverflow.com/questions/46716095/minimum-file-size-for-compression-algorithms
@@ -57,7 +52,7 @@ private function add(ZipArchiveWriter $zip, Path $path, Path $base, $prefix= '')
5752
}
5853
(new StreamTransfer($path->asFile()->in(), $file->out()))->transferAll();
5954
yield $file;
60-
} else if (self::IS_FOLDER === ($stat['mode'] & self::IS_FOLDER)) {
55+
} else if (Sources::IS_FOLDER === ($stat['mode'] & Sources::IS_FOLDER)) {
6156
yield $zip->add(new ZipDirEntry($relative));
6257
foreach ($path->asFolder()->entries() as $entry) {
6358
yield from $this->add($zip, $entry, $base, $prefix);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
/** Returns a unique list of sources */
77
class Sources implements IteratorAggregate {
8+
9+
// See https://www.php.net/manual/en/function.fileperms.php
10+
const IS_LINK= 0120000;
11+
const IS_FILE= 0100000;
12+
const IS_FOLDER= 0040000;
13+
814
public $base;
915
private $sources;
1016

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ private function cleanup(Folder $folder= null) {
2222
$folder ?? $folder= $this->tempDir;
2323
foreach ($folder->entries() as $entry) {
2424
switch ($m= lstat($entry)['mode'] & 0170000) {
25-
case PackageLambda::IS_LINK: unlink($entry); break;
26-
case PackageLambda::IS_FILE: $entry->asFile()->unlink(); break;
27-
case PackageLambda::IS_FOLDER: $this->cleanup($entry->asFolder()); break;
25+
case Sources::IS_LINK: unlink($entry); break;
26+
case Sources::IS_FILE: $entry->asFile()->unlink(); break;
27+
case Sources::IS_FOLDER: $this->cleanup($entry->asFolder()); break;
2828
}
2929
}
3030
}
@@ -35,15 +35,15 @@ private function create(array $definitions): Path {
3535
// Create sources from definitions
3636
foreach ($definitions as $name => $definition) {
3737
switch ($definition[0]) {
38-
case PackageLambda::IS_FILE:
38+
case Sources::IS_FILE:
3939
Files::write(new File($this->tempDir, $name), $definition[1]);
4040
break;
4141

42-
case PackageLambda::IS_FOLDER:
42+
case Sources::IS_FOLDER:
4343
(new Folder($this->tempDir, $name))->create($definition[1]);
4444
break;
4545

46-
case PackageLambda::IS_LINK:
46+
case Sources::IS_LINK:
4747
symlink($definition[1], new Path($this->tempDir, $name));
4848
break;
4949
}
@@ -71,7 +71,7 @@ private function package(Sources $sources): ZipIterator {
7171

7272
#[Test]
7373
public function single_file() {
74-
$zip= $this->package(new Sources($this->create(['file.txt' => [PackageLambda::IS_FILE, 'Test']]), ['file.txt']));
74+
$zip= $this->package(new Sources($this->create(['file.txt' => [Sources::IS_FILE, 'Test']]), ['file.txt']));
7575

7676
$file= $zip->next();
7777
Assert::equals('file.txt', $file->getName());
@@ -81,7 +81,7 @@ public function single_file() {
8181

8282
#[Test]
8383
public function single_directory() {
84-
$zip= $this->package(new Sources($this->create(['src' => [PackageLambda::IS_FOLDER, 0755]]), ['src']));
84+
$zip= $this->package(new Sources($this->create(['src' => [Sources::IS_FOLDER, 0755]]), ['src']));
8585

8686
$dir= $zip->next();
8787
Assert::equals('src/', $dir->getName());
@@ -92,8 +92,8 @@ public function single_directory() {
9292
#[Test]
9393
public function file_inside_directory() {
9494
$path= $this->create([
95-
'src' => [PackageLambda::IS_FOLDER, 0755],
96-
'src/file.txt' => [PackageLambda::IS_FILE, 'Test']
95+
'src' => [Sources::IS_FOLDER, 0755],
96+
'src/file.txt' => [Sources::IS_FILE, 'Test']
9797
]);
9898
$zip= $this->package(new Sources($path, ['src']));
9999

@@ -111,13 +111,13 @@ public function file_inside_directory() {
111111
#[Test]
112112
public function link_inside_directory() {
113113
$path= $this->create([
114-
'core/' => [PackageLambda::IS_FOLDER, 0755],
115-
'core/composer.json' => [PackageLambda::IS_FILE, '{"require":{"php":">=7.0"}}'],
116-
'project' => [PackageLambda::IS_FOLDER, 0755],
117-
'project/src' => [PackageLambda::IS_FOLDER, 0755],
118-
'project/src/file.txt' => [PackageLambda::IS_FILE, 'Test'],
119-
'project/lib' => [PackageLambda::IS_FOLDER, 0755],
120-
'project/lib/core' => [PackageLambda::IS_LINK, '../../core'],
114+
'core/' => [Sources::IS_FOLDER, 0755],
115+
'core/composer.json' => [Sources::IS_FILE, '{"require":{"php":">=7.0"}}'],
116+
'project' => [Sources::IS_FOLDER, 0755],
117+
'project/src' => [Sources::IS_FOLDER, 0755],
118+
'project/src/file.txt' => [Sources::IS_FILE, 'Test'],
119+
'project/lib' => [Sources::IS_FOLDER, 0755],
120+
'project/lib/core' => [Sources::IS_LINK, '../../core'],
121121
]);
122122
$zip= $this->package(new Sources(new Path($path, 'project'), ['src', 'lib']));
123123

0 commit comments

Comments
 (0)