11<?php
2+
23/**
34 * Class Application
45 *
78 *
89 * Integrates the Tcl/Tk event processing with PHP application logic via FFI.
910 */
11+
1012namespace PhpGui ;
1113
12- class Application {
14+ class Application
15+ {
1316 private ProcessTCL $ tcl ;
1417 private bool $ running = false ;
1518 private string $ appId ;
@@ -20,13 +23,18 @@ class Application {
2023 * Initializes the Tcl interpreter, configures the root window,
2124 * and sets up the exit procedure using a quit signal file.
2225 */
23- public function __construct () {
26+ public function __construct ()
27+ {
2428 $ this ->tcl = ProcessTCL::getInstance ();
2529 $ this ->appId = uniqid ('app_ ' );
2630 $ 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 } " );
3038 }
3139
3240 /**
@@ -35,27 +43,33 @@ public function __construct() {
3543 * Processes Tcl events continuously, checks for callback and quit signals,
3644 * and terminates the loop when a quit signal is received.
3745 */
38- public function run (): void {
46+ public function run (): void
47+ {
3948 if ($ this ->running ) {
4049 return ;
4150 }
4251
4352 $ this ->running = true ;
44- // Main loop: process Tcl events, callback file, and quit file
4553 while ($ this ->running ) {
4654 $ 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 " ;
4859 if (file_exists ($ callbackFile )) {
4960 $ id = trim (file_get_contents ($ callbackFile ));
5061 unlink ($ callbackFile );
5162 ProcessTCL::getInstance ()->executeCallback ($ id );
5263 }
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 " ;
5468 if (file_exists ($ quitFile )) {
5569 unlink ($ quitFile );
5670 $ this ->running = false ;
5771 }
58- usleep (100000 ); // sleep for 0.1 second
72+ usleep (100000 );
5973 }
6074 $ this ->quit ();
6175 }
@@ -65,12 +79,13 @@ public function run(): void {
6579 *
6680 * Stops the main event loop and exits the application.
6781 */
68- public function quit (): void {
82+ public function quit (): void
83+ {
6984 if (!$ this ->running ) {
7085 return ;
7186 }
72-
87+
7388 $ this ->running = false ;
74- exit (0 );
89+ exit (0 );
7590 }
7691}
0 commit comments