1
1
<?php
2
+
2
3
/**
3
4
* Class Application
4
5
*
7
8
*
8
9
* Integrates the Tcl/Tk event processing with PHP application logic via FFI.
9
10
*/
11
+
10
12
namespace PhpGui ;
11
13
12
- class Application {
14
+ class Application
15
+ {
13
16
private ProcessTCL $ tcl ;
14
17
private bool $ running = false ;
15
18
private string $ appId ;
@@ -20,13 +23,18 @@ class Application {
20
23
* Initializes the Tcl interpreter, configures the root window,
21
24
* and sets up the exit procedure using a quit signal file.
22
25
*/
23
- public function __construct () {
26
+ public function __construct ()
27
+ {
24
28
$ this ->tcl = ProcessTCL::getInstance ();
25
29
$ this ->appId = uniqid ('app_ ' );
26
30
$ this ->tcl ->evalTcl ("package require Tk " );
27
- $ this ->tcl ->evalTcl ("wm withdraw . " ); // Suppress the default root window
28
- // Update exit_app to write a quit signal file
29
- $ this ->tcl ->evalTcl ("proc ::exit_app {} { set ::forever 1; set f [open \"/tmp/phpgui_quit.txt \" w]; puts \$f 1; close \$f } " );
31
+ $ this ->tcl ->evalTcl ("wm withdraw . " );
32
+
33
+
34
+ $ quitFile = (PHP_OS_FAMILY === 'Windows ' )
35
+ ? str_replace ('\\' , '/ ' , sys_get_temp_dir ()) . "/phpgui_quit.txt "
36
+ : "/tmp/phpgui_quit.txt " ;
37
+ $ this ->tcl ->evalTcl ("proc ::exit_app {} { set ::forever 1; set f [open \"$ quitFile \" w]; puts \$f 1; close \$f } " );
30
38
}
31
39
32
40
/**
@@ -35,27 +43,33 @@ public function __construct() {
35
43
* Processes Tcl events continuously, checks for callback and quit signals,
36
44
* and terminates the loop when a quit signal is received.
37
45
*/
38
- public function run (): void {
46
+ public function run (): void
47
+ {
39
48
if ($ this ->running ) {
40
49
return ;
41
50
}
42
51
43
52
$ this ->running = true ;
44
- // Main loop: process Tcl events, callback file, and quit file
45
53
while ($ this ->running ) {
46
54
$ this ->tcl ->evalTcl ("update " );
47
- $ callbackFile = '/tmp/phpgui_callback.txt ' ;
55
+ // Use OS-based callback file path.
56
+ $ callbackFile = (PHP_OS_FAMILY === 'Windows ' )
57
+ ? str_replace ('\\' , '/ ' , sys_get_temp_dir ()) . "/phpgui_callback.txt "
58
+ : "/tmp/phpgui_callback.txt " ;
48
59
if (file_exists ($ callbackFile )) {
49
60
$ id = trim (file_get_contents ($ callbackFile ));
50
61
unlink ($ callbackFile );
51
62
ProcessTCL::getInstance ()->executeCallback ($ id );
52
63
}
53
- $ quitFile = '/tmp/phpgui_quit.txt ' ;
64
+ // Use OS-based quit file path.
65
+ $ quitFile = (PHP_OS_FAMILY === 'Windows ' )
66
+ ? str_replace ('\\' , '/ ' , sys_get_temp_dir ()) . "/phpgui_quit.txt "
67
+ : "/tmp/phpgui_quit.txt " ;
54
68
if (file_exists ($ quitFile )) {
55
69
unlink ($ quitFile );
56
70
$ this ->running = false ;
57
71
}
58
- usleep (100000 ); // sleep for 0.1 second
72
+ usleep (100000 );
59
73
}
60
74
$ this ->quit ();
61
75
}
@@ -65,12 +79,13 @@ public function run(): void {
65
79
*
66
80
* Stops the main event loop and exits the application.
67
81
*/
68
- public function quit (): void {
82
+ public function quit (): void
83
+ {
69
84
if (!$ this ->running ) {
70
85
return ;
71
86
}
72
-
87
+
73
88
$ this ->running = false ;
74
- exit (0 );
89
+ exit (0 );
75
90
}
76
91
}
0 commit comments