Skip to content

Commit ff9c48f

Browse files
Fix / Forward native ENV variables to child process (#129)
* remove commented env merge * fix typo * forward native env variables to child process * Build plugin * Update resources/js/electron-plugin/src/server/api/childProcess.ts Co-authored-by: Simon Hamp <[email protected]> * Build plugin * add php endpoint with native env variables * extract defaultPhpIniSettings function * mix in php ini settings * boyscouting - cleanup startQueueWorker * Build plugin * Code style * merge defaults with user configured ini settings * Build plugin * tidy * Build plugin --------- Co-authored-by: Simon Hamp <[email protected]>
1 parent 1b56529 commit ff9c48f

File tree

4 files changed

+73
-38
lines changed

4 files changed

+73
-38
lines changed

resources/js/electron-plugin/dist/server/api/childProcess.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { utilityProcess } from 'electron';
1212
import state from '../state';
1313
import { notifyLaravel } from "../utils";
1414
import { join } from 'path';
15+
import { getDefaultEnvironmentVariables, getDefaultPhpIniSettings } from "../php";
1516
const router = express.Router();
1617
const killSync = require('kill-sync');
1718
function startProcess(settings) {
@@ -21,8 +22,8 @@ function startProcess(settings) {
2122
}
2223
const proc = utilityProcess.fork(join(__dirname, '../../electron-plugin/dist/server/childProcess.js'), cmd, {
2324
cwd,
24-
serviceName: alias,
2525
stdio: 'pipe',
26+
serviceName: alias,
2627
env: Object.assign(Object.assign({}, process.env), env)
2728
});
2829
proc.stdout.on('data', (data) => {
@@ -68,7 +69,7 @@ function startProcess(settings) {
6869
const settings = Object.assign({}, getSettings(alias));
6970
delete state.processes[alias];
7071
if (settings.persistent) {
71-
console.log('Process [' + alias + '] wathchdog restarting...');
72+
console.log('Process [' + alias + '] watchdog restarting...');
7273
startProcess(settings);
7374
}
7475
});
@@ -78,6 +79,15 @@ function startProcess(settings) {
7879
settings
7980
};
8081
}
82+
function startPhpProcess(settings) {
83+
const defaultEnv = getDefaultEnvironmentVariables(state.randomSecret, state.electronApiPort);
84+
const iniSettings = Object.assign(Object.assign({}, getDefaultPhpIniSettings()), state.phpIni);
85+
const iniArgs = Object.keys(iniSettings).map(key => {
86+
return ['-d', `${key}=${iniSettings[key]}`];
87+
}).flat();
88+
settings = Object.assign(Object.assign({}, settings), { cmd: [state.php, ...iniArgs, ...settings.cmd], env: Object.assign(Object.assign({}, settings.env), defaultEnv) });
89+
return startProcess(settings);
90+
}
8191
function stopProcess(alias) {
8292
const proc = getProcess(alias);
8393
if (proc === undefined) {
@@ -105,6 +115,10 @@ router.post('/start', (req, res) => {
105115
const proc = startProcess(req.body);
106116
res.json(proc);
107117
});
118+
router.post('/start-php', (req, res) => {
119+
const proc = startPhpProcess(req.body);
120+
res.json(proc);
121+
});
108122
router.post('/stop', (req, res) => {
109123
const { alias } = req.body;
110124
stopProcess(alias);

resources/js/electron-plugin/dist/server/php.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,7 @@ function retrieveNativePHPConfig() {
5858
});
5959
}
6060
function callPhp(args, options, phpIniSettings = {}) {
61-
let defaultIniSettings = {
62-
'memory_limit': '512M',
63-
'curl.cainfo': state.caCert,
64-
'openssl.cafile': state.caCert
65-
};
66-
let iniSettings = Object.assign(defaultIniSettings, phpIniSettings);
61+
let iniSettings = Object.assign(getDefaultPhpIniSettings(), phpIniSettings);
6762
Object.keys(iniSettings).forEach(key => {
6863
args.unshift('-d', `${key}=${iniSettings[key]}`);
6964
});
@@ -101,15 +96,7 @@ function ensureAppFoldersAreAvailable() {
10196
}
10297
}
10398
function startQueueWorker(secret, apiPort, phpIniSettings = {}) {
104-
const env = {
105-
APP_ENV: process.env.NODE_ENV === 'development' ? 'local' : 'production',
106-
APP_DEBUG: process.env.NODE_ENV === 'development' ? 'true' : 'false',
107-
NATIVEPHP_STORAGE_PATH: storagePath,
108-
NATIVEPHP_DATABASE_PATH: databaseFile,
109-
NATIVEPHP_API_URL: `http://localhost:${apiPort}/api/`,
110-
NATIVEPHP_RUNNING: true,
111-
NATIVEPHP_SECRET: secret
112-
};
99+
const env = getDefaultEnvironmentVariables(secret, apiPort);
113100
const phpOptions = {
114101
cwd: appPath,
115102
env
@@ -139,7 +126,7 @@ function getDefaultEnvironmentVariables(secret, apiPort) {
139126
NATIVEPHP_STORAGE_PATH: storagePath,
140127
NATIVEPHP_DATABASE_PATH: databaseFile,
141128
NATIVEPHP_API_URL: `http://localhost:${apiPort}/api/`,
142-
NATIVEPHP_RUNNING: true,
129+
NATIVEPHP_RUNNING: 'true',
143130
NATIVEPHP_SECRET: secret,
144131
NATIVEPHP_USER_HOME_PATH: getPath('home'),
145132
NATIVEPHP_APP_DATA_PATH: getPath('appData'),
@@ -153,6 +140,13 @@ function getDefaultEnvironmentVariables(secret, apiPort) {
153140
NATIVEPHP_RECENT_PATH: getPath('recent'),
154141
};
155142
}
143+
function getDefaultPhpIniSettings() {
144+
return {
145+
'memory_limit': '512M',
146+
'curl.cainfo': state.caCert,
147+
'openssl.cafile': state.caCert
148+
};
149+
}
156150
function serveApp(secret, apiPort, phpIniSettings) {
157151
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
158152
const appPath = getAppPath();
@@ -220,4 +214,4 @@ function serveApp(secret, apiPort, phpIniSettings) {
220214
});
221215
}));
222216
}
223-
export { startQueueWorker, startScheduler, serveApp, getAppPath, retrieveNativePHPConfig, retrievePhpIniSettings };
217+
export { startQueueWorker, startScheduler, serveApp, getAppPath, retrieveNativePHPConfig, retrievePhpIniSettings, getDefaultEnvironmentVariables, getDefaultPhpIniSettings };

resources/js/electron-plugin/src/server/api/childProcess.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { utilityProcess } from 'electron';
33
import state from '../state';
44
import { notifyLaravel } from "../utils";
55
import { join } from 'path';
6+
import { getDefaultEnvironmentVariables, getDefaultPhpIniSettings } from "../php";
7+
68

79
const router = express.Router();
810
const killSync = require('kill-sync');
@@ -19,8 +21,8 @@ function startProcess(settings) {
1921
cmd,
2022
{
2123
cwd,
22-
serviceName: alias,
2324
stdio: 'pipe',
25+
serviceName: alias,
2426
env: {
2527
...process.env,
2628
...env,
@@ -81,7 +83,7 @@ function startProcess(settings) {
8183
delete state.processes[alias];
8284

8385
if (settings.persistent) {
84-
console.log('Process [' + alias + '] wathchdog restarting...');
86+
console.log('Process [' + alias + '] watchdog restarting...');
8587
startProcess(settings);
8688
}
8789
});
@@ -93,6 +95,30 @@ function startProcess(settings) {
9395
};
9496
}
9597

98+
function startPhpProcess(settings) {
99+
const defaultEnv = getDefaultEnvironmentVariables(
100+
state.randomSecret,
101+
state.electronApiPort
102+
);
103+
104+
// Construct command args from ini settings
105+
const iniSettings = { ...getDefaultPhpIniSettings(), ...state.phpIni };
106+
const iniArgs = Object.keys(iniSettings).map(key => {
107+
return ['-d', `${key}=${iniSettings[key]}`];
108+
}).flat();
109+
110+
111+
settings = {
112+
...settings,
113+
// Prepend cmd with php executable path & ini settings
114+
cmd: [ state.php, ...iniArgs, ...settings.cmd ],
115+
// Mix in the internal NativePHP env
116+
env: { ...settings.env, ...defaultEnv }
117+
};
118+
119+
return startProcess(settings);
120+
}
121+
96122
function stopProcess(alias) {
97123
const proc = getProcess(alias);
98124

@@ -129,6 +155,12 @@ router.post('/start', (req, res) => {
129155
res.json(proc);
130156
});
131157

158+
router.post('/start-php', (req, res) => {
159+
const proc = startPhpProcess(req.body);
160+
161+
res.json(proc);
162+
});
163+
132164
router.post('/stop', (req, res) => {
133165
const {alias} = req.body;
134166

resources/js/electron-plugin/src/server/php.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,8 @@ async function retrieveNativePHPConfig() {
5353
}
5454

5555
function callPhp(args, options, phpIniSettings = {}) {
56-
let defaultIniSettings = {
57-
'memory_limit': '512M',
58-
'curl.cainfo': state.caCert,
59-
'openssl.cafile': state.caCert
60-
}
6156

62-
let iniSettings = Object.assign(defaultIniSettings, phpIniSettings);
57+
let iniSettings = Object.assign(getDefaultPhpIniSettings(), phpIniSettings);
6358

6459
Object.keys(iniSettings).forEach(key => {
6560
args.unshift('-d', `${key}=${iniSettings[key]}`);
@@ -120,15 +115,7 @@ function ensureAppFoldersAreAvailable() {
120115
}
121116

122117
function startQueueWorker(secret, apiPort, phpIniSettings = {}) {
123-
const env = {
124-
APP_ENV: process.env.NODE_ENV === 'development' ? 'local' : 'production',
125-
APP_DEBUG: process.env.NODE_ENV === 'development' ? 'true' : 'false',
126-
NATIVEPHP_STORAGE_PATH: storagePath,
127-
NATIVEPHP_DATABASE_PATH: databaseFile,
128-
NATIVEPHP_API_URL: `http://localhost:${apiPort}/api/`,
129-
NATIVEPHP_RUNNING: true,
130-
NATIVEPHP_SECRET: secret
131-
};
118+
const env = getDefaultEnvironmentVariables(secret, apiPort);
132119

133120
const phpOptions = {
134121
cwd: appPath,
@@ -165,7 +152,7 @@ function getDefaultEnvironmentVariables(secret, apiPort) {
165152
NATIVEPHP_STORAGE_PATH: storagePath,
166153
NATIVEPHP_DATABASE_PATH: databaseFile,
167154
NATIVEPHP_API_URL: `http://localhost:${apiPort}/api/`,
168-
NATIVEPHP_RUNNING: true,
155+
NATIVEPHP_RUNNING: 'true',
169156
NATIVEPHP_SECRET: secret,
170157
NATIVEPHP_USER_HOME_PATH: getPath('home'),
171158
NATIVEPHP_APP_DATA_PATH: getPath('appData'),
@@ -180,6 +167,14 @@ function getDefaultEnvironmentVariables(secret, apiPort) {
180167
};
181168
}
182169

170+
function getDefaultPhpIniSettings() {
171+
return {
172+
'memory_limit': '512M',
173+
'curl.cainfo': state.caCert,
174+
'openssl.cafile': state.caCert
175+
}
176+
}
177+
183178
function serveApp(secret, apiPort, phpIniSettings): Promise<ProcessResult> {
184179
return new Promise(async (resolve, reject) => {
185180
const appPath = getAppPath();
@@ -267,4 +262,4 @@ function serveApp(secret, apiPort, phpIniSettings): Promise<ProcessResult> {
267262
})
268263
}
269264

270-
export {startQueueWorker, startScheduler, serveApp, getAppPath, retrieveNativePHPConfig, retrievePhpIniSettings}
265+
export {startQueueWorker, startScheduler, serveApp, getAppPath, retrieveNativePHPConfig, retrievePhpIniSettings, getDefaultEnvironmentVariables, getDefaultPhpIniSettings}

0 commit comments

Comments
 (0)