Skip to content

Commit a2f9888

Browse files
committed
feat: php.ts run with bundle
1 parent 08a3726 commit a2f9888

File tree

4 files changed

+210
-100
lines changed

4 files changed

+210
-100
lines changed

resources/js/electron-plugin/dist/index.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,6 @@ class NativePHP {
6767
}
6868
event.preventDefault();
6969
});
70-
if (process.platform === 'win32') {
71-
app.on('second-instance', (event, commandLine, workingDirectory) => {
72-
if (this.mainWindow) {
73-
if (this.mainWindow.isMinimized())
74-
this.mainWindow.restore();
75-
this.mainWindow.focus();
76-
}
77-
this.handleDeepLink(commandLine.pop());
78-
});
79-
}
8070
}
8171
bootstrapApp(app) {
8272
return __awaiter(this, void 0, void 0, function* () {
@@ -135,12 +125,28 @@ class NativePHP {
135125
else {
136126
app.setAsDefaultProtocolClient(deepLinkProtocol);
137127
}
138-
if (process.platform === 'win32') {
128+
if (process.platform !== "darwin") {
139129
const gotTheLock = app.requestSingleInstanceLock();
140130
if (!gotTheLock) {
141131
app.quit();
142132
return;
143133
}
134+
else {
135+
app.on("second-instance", (event, commandLine, workingDirectory) => {
136+
if (this.mainWindow) {
137+
if (this.mainWindow.isMinimized())
138+
this.mainWindow.restore();
139+
this.mainWindow.focus();
140+
}
141+
notifyLaravel("events", {
142+
event: "\\Native\\Laravel\\Events\\App\\OpenedFromURL",
143+
payload: {
144+
url: commandLine[commandLine.length - 1],
145+
workingDirectory: workingDirectory,
146+
},
147+
});
148+
});
149+
}
144150
}
145151
}
146152
}

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { Notification } from 'electron';
33
import { notifyLaravel } from "../utils.js";
44
const router = express.Router();
55
router.post('/', (req, res) => {
6-
const { title, body, subtitle, silent, icon, hasReply, timeoutType, replyPlaceholder, sound, urgency, actions, closeButtonText, toastXml, event: customEvent } = req.body;
6+
const { title, body, subtitle, silent, icon, hasReply, timeoutType, replyPlaceholder, sound, urgency, actions, closeButtonText, toastXml, event: customEvent, reference, } = req.body;
77
const eventName = customEvent !== null && customEvent !== void 0 ? customEvent : '\\Native\\Laravel\\Events\\Notifications\\NotificationClicked';
8+
const notificationReference = reference !== null && reference !== void 0 ? reference : (Date.now() + '.' + Math.random().toString(36).slice(2, 9));
89
const notification = new Notification({
910
title,
1011
body,
@@ -22,11 +23,45 @@ router.post('/', (req, res) => {
2223
});
2324
notification.on("click", (event) => {
2425
notifyLaravel('events', {
25-
event: eventName,
26-
payload: JSON.stringify(event)
26+
event: eventName || '\\Native\\Laravel\\Events\\Notifications\\NotificationClicked',
27+
payload: {
28+
reference: notificationReference,
29+
event: JSON.stringify(event),
30+
},
31+
});
32+
});
33+
notification.on("action", (event, index) => {
34+
notifyLaravel('events', {
35+
event: '\\Native\\Laravel\\Events\\Notifications\\NotificationActionClicked',
36+
payload: {
37+
reference: notificationReference,
38+
index,
39+
event: JSON.stringify(event),
40+
},
41+
});
42+
});
43+
notification.on("reply", (event, reply) => {
44+
notifyLaravel('events', {
45+
event: '\\Native\\Laravel\\Events\\Notifications\\NotificationReply',
46+
payload: {
47+
reference: notificationReference,
48+
reply,
49+
event: JSON.stringify(event),
50+
},
51+
});
52+
});
53+
notification.on("close", (event) => {
54+
notifyLaravel('events', {
55+
event: '\\Native\\Laravel\\Events\\Notifications\\NotificationClosed',
56+
payload: {
57+
reference: notificationReference,
58+
event: JSON.stringify(event),
59+
},
2760
});
2861
});
2962
notification.show();
30-
res.sendStatus(200);
63+
res.status(200).json({
64+
reference: notificationReference,
65+
});
3166
});
3267
export default router;

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

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ const databasePath = join(app.getPath('userData'), 'database');
2222
const databaseFile = join(databasePath, 'database.sqlite');
2323
const argumentEnv = getArgumentEnv();
2424
const appPath = getAppPath();
25+
function runningSecureBuild() {
26+
return existsSync(join(appPath, 'build', '__nativephp_app_bundle'));
27+
}
28+
function shouldMigrateDatabase(store) {
29+
return store.get('migrated_version') !== app.getVersion()
30+
&& process.env.NODE_ENV !== 'development';
31+
}
2532
function getPhpPort() {
2633
return __awaiter(this, void 0, void 0, function* () {
2734
return yield getPort({
@@ -41,7 +48,11 @@ function retrievePhpIniSettings() {
4148
cwd: appPath,
4249
env
4350
};
44-
return yield promisify(execFile)(state.php, ['artisan', 'native:php-ini'], phpOptions);
51+
let command = ['artisan', 'native:php-ini'];
52+
if (runningSecureBuild()) {
53+
command.unshift(join(appPath, 'build', '__nativephp_app_bundle'));
54+
}
55+
return yield promisify(execFile)(state.php, command, phpOptions);
4556
});
4657
}
4758
function retrieveNativePHPConfig() {
@@ -55,14 +66,24 @@ function retrieveNativePHPConfig() {
5566
cwd: appPath,
5667
env
5768
};
58-
return yield promisify(execFile)(state.php, ['artisan', 'native:config'], phpOptions);
69+
let command = ['artisan', 'native:config'];
70+
if (runningSecureBuild()) {
71+
command.unshift(join(appPath, 'build', '__nativephp_app_bundle'));
72+
}
73+
return yield promisify(execFile)(state.php, command, phpOptions);
5974
});
6075
}
6176
function callPhp(args, options, phpIniSettings = {}) {
77+
if (args[0] === 'artisan' && runningSecureBuild()) {
78+
args.unshift(join(appPath, 'build', '__nativephp_app_bundle'));
79+
}
6280
let iniSettings = Object.assign(getDefaultPhpIniSettings(), phpIniSettings);
6381
Object.keys(iniSettings).forEach(key => {
6482
args.unshift('-d', `${key}=${iniSettings[key]}`);
6583
});
84+
if (parseInt(process.env.SHELL_VERBOSITY) > 0) {
85+
console.log('Calling PHP', state.php, args);
86+
}
6687
return spawn(state.php, args, {
6788
cwd: options.cwd,
6889
env: Object.assign(Object.assign({}, process.env), options.env),
@@ -116,6 +137,7 @@ function getDefaultEnvironmentVariables(secret, apiPort) {
116137
return {
117138
APP_ENV: process.env.NODE_ENV === 'development' ? 'local' : 'production',
118139
APP_DEBUG: process.env.NODE_ENV === 'development' ? 'true' : 'false',
140+
LARAVEL_STORAGE_PATH: storagePath,
119141
NATIVEPHP_STORAGE_PATH: storagePath,
120142
NATIVEPHP_DATABASE_PATH: databaseFile,
121143
NATIVEPHP_API_URL: `http://localhost:${apiPort}/api/`,
@@ -152,8 +174,10 @@ function serveApp(secret, apiPort, phpIniSettings) {
152174
env
153175
};
154176
const store = new Store();
155-
callPhp(['artisan', 'storage:link', '--force'], phpOptions, phpIniSettings);
156-
if (store.get('migrated_version') !== app.getVersion() && process.env.NODE_ENV !== 'development') {
177+
if (!runningSecureBuild()) {
178+
callPhp(['artisan', 'storage:link', '--force'], phpOptions, phpIniSettings);
179+
}
180+
if (shouldMigrateDatabase(store)) {
157181
console.log('Migrating database...');
158182
callPhp(['artisan', 'migrate', '--force'], phpOptions, phpIniSettings);
159183
store.set('migrated_version', app.getVersion());
@@ -162,40 +186,36 @@ function serveApp(secret, apiPort, phpIniSettings) {
162186
console.log('Skipping Database migration while in development.');
163187
console.log('You may migrate manually by running: php artisan native:migrate');
164188
}
189+
console.log('Starting PHP server...');
165190
const phpPort = yield getPhpPort();
166-
const serverPath = join(appPath, 'vendor', 'laravel', 'framework', 'src', 'Illuminate', 'Foundation', 'resources', 'server.php');
191+
let serverPath = join(appPath, 'build', '__nativephp_app_bundle');
192+
if (!runningSecureBuild()) {
193+
console.log('* * * Running from source * * *');
194+
serverPath = join(appPath, 'vendor', 'laravel', 'framework', 'src', 'Illuminate', 'Foundation', 'resources', 'server.php');
195+
}
167196
const phpServer = callPhp(['-S', `127.0.0.1:${phpPort}`, serverPath], {
168197
cwd: join(appPath, 'public'),
169198
env
170199
}, phpIniSettings);
171200
const portRegex = /Development Server \(.*:([0-9]+)\) started/gm;
172201
phpServer.stdout.on('data', (data) => {
173-
const match = portRegex.exec(data.toString());
174-
if (match) {
175-
console.log("PHP Server started on port: ", match[1]);
176-
const port = parseInt(match[1]);
177-
resolve({
178-
port,
179-
process: phpServer
180-
});
181-
}
182202
});
183203
phpServer.stderr.on('data', (data) => {
184204
const error = data.toString();
185-
const match = portRegex.exec(error);
205+
const match = portRegex.exec(data.toString());
186206
if (match) {
187207
const port = parseInt(match[1]);
188208
console.log("PHP Server started on port: ", port);
189209
resolve({
190210
port,
191-
process: phpServer
211+
process: phpServer,
192212
});
193213
}
194214
else {
195-
if (error.startsWith('[NATIVE_EXCEPTION]: ', 27)) {
215+
if (error.includes('[NATIVE_EXCEPTION]:')) {
196216
console.log();
197217
console.error('Error in PHP:');
198-
console.error(' ' + error.slice(47));
218+
console.error(' ' + error.split('[NATIVE_EXCEPTION]:')[1].trim());
199219
console.log('Please check your log file:');
200220
console.log(' ' + join(appPath, 'storage', 'logs', 'laravel.log'));
201221
console.log();
@@ -205,6 +225,9 @@ function serveApp(secret, apiPort, phpIniSettings) {
205225
phpServer.on('error', (error) => {
206226
reject(error);
207227
});
228+
phpServer.on('close', (code) => {
229+
console.log(`PHP server exited with code ${code}`);
230+
});
208231
}));
209232
}
210233
export { startScheduler, serveApp, getAppPath, retrieveNativePHPConfig, retrievePhpIniSettings, getDefaultEnvironmentVariables, getDefaultPhpIniSettings };

0 commit comments

Comments
 (0)