Skip to content

Commit dc1e5b5

Browse files
committed
Update binary copying logic
As there are multiple versions of PHP for each platform, this is needed to pick the correct one The binaries are also now zipped to save space, so this process unzips the relevant zip file into the right location It relies upon the new `NATIVEPHP_PHP_BINARY_VERSION` parameter to be passed into the process. By default, this will be the version that matches the one the developer has used to start this process
1 parent 1289caa commit dc1e5b5

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"illuminate/contracts": "^10.0|^11.0",
2121
"laravel/prompts": "^0.1.1",
2222
"nativephp/laravel": "*",
23-
"nativephp/php-bin": "*",
23+
"nativephp/php-bin": "^0.4",
2424
"spatie/laravel-package-tools": "^1.14.0"
2525
},
2626
"require-dev": {

resources/js/php.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
const {copySync, removeSync, existsSync} = require("fs-extra");
1+
const fs = require("fs");
2+
const {copySync, removeSync, existsSync, ensureDirSync} = require("fs-extra");
23
const {join} = require("path");
4+
const unzip = require("yauzl");
5+
36
const isBuilding = process.env.NATIVEPHP_BUILDING;
47
const phpBinaryPath = process.env.NATIVEPHP_PHP_BINARY_PATH;
8+
const phpVersion = process.env.NATIVEPHP_PHP_BINARY_VERSION;
59
const certificatePath = process.env.NATIVEPHP_CERTIFICATE_FILE_PATH;
610

711
// Differentiates for Serving and Building
@@ -30,28 +34,50 @@ if (isArm64) {
3034
binaryArch = 'arm64';
3135
}
3236

33-
34-
35-
const binarySrcDir = join(phpBinaryPath, targetOs, binaryArch);
37+
const phpVersionZip = 'php-' + phpVersion + '.zip';
38+
const binarySrcDir = join(phpBinaryPath, targetOs, binaryArch, phpVersionZip);
3639
const binaryDestDir = join(__dirname, 'resources/php');
3740

3841
console.log('Binary Source: ', binarySrcDir);
3942
console.log('Binary Filename: ', phpBinaryFilename);
43+
console.log('PHP version: ' + phpVersion);
4044

4145
if (phpBinaryPath) {
4246
try {
43-
console.log('Copying PHP file(s) from ' + binarySrcDir + ' to ' + binaryDestDir);
47+
console.log('Unzipping PHP binary from ' + binarySrcDir + ' to ' + binaryDestDir);
4448
removeSync(binaryDestDir);
45-
copySync(binarySrcDir, binaryDestDir);
4649

50+
ensureDirSync(binaryDestDir);
51+
52+
// Unzip the files
53+
unzip.open(binarySrcDir, {lazyEntries: true}, function (err, zipfile) {
54+
if (err) throw err;
55+
zipfile.readEntry();
56+
zipfile.on("entry", function (entry) {
57+
zipfile.openReadStream(entry, function (err, readStream) {
58+
if (err) throw err;
59+
60+
const writeStream = fs.createWriteStream(join(binaryDestDir, 'php'));
61+
62+
readStream.pipe(writeStream);
63+
64+
writeStream.on("close", function() {
65+
console.log('Copied PHP binary to ', binaryDestDir);
66+
67+
// Add execute permissions
68+
fs.chmod(join(binaryDestDir, 'php'), 0o755, (err) => {
69+
if (err) {
70+
console.log(`Error setting permissions: ${err}`);
71+
}
72+
});
4773

48-
// If we're building for Windows, copy the php.exe from the dest dir to `php`.
49-
// This allows the same import command to work on all platforms (same binary filename)
50-
if (isWindows && existsSync(join(binaryDestDir, phpBinaryFilename))) {
51-
copySync(join(binaryDestDir, phpBinaryFilename), join(binaryDestDir, 'php'));
52-
}
74+
zipfile.readEntry();
75+
});
76+
});
77+
});
78+
});
5379
} catch (e) {
54-
console.log('Error copying PHP binary', e);
80+
console.error('Error copying PHP binary', e);
5581
}
5682
}
5783

0 commit comments

Comments
 (0)