@@ -20,6 +20,16 @@ class Server
2020 */
2121 protected $ master ;
2222
23+ /**
24+ * @var string $host Host the server will be bound to.
25+ */
26+ private string $ host = '' ;
27+
28+ /**
29+ * @var int $port Port the server will listen on.
30+ */
31+ private int $ port = 0 ;
32+
2333 /**
2434 * @var resource $icpSocket
2535 */
@@ -30,6 +40,21 @@ class Server
3040 */
3141 private string $ ipcSocketPath ;
3242
43+ /**
44+ * @var string $ipcOwner If set, owner of the ipc socket will be changed to this value.
45+ */
46+ private string $ ipcOwner = '' ;
47+
48+ /**
49+ * @var string $ipcGroup If set, group of the ipc socket will be changed to this value.
50+ */
51+ private string $ ipcGroup = '' ;
52+
53+ /**
54+ * @var int $ipcMode If set, chmod of the ipc socket will be changed to this value.
55+ */
56+ private int $ ipcMode = 0 ;
57+
3358 /**
3459 * @var array Holds all connected sockets
3560 */
@@ -90,11 +115,9 @@ public function __construct(
90115 int $ port = 8000 ,
91116 string $ ipcSocketPath = '/tmp/phpwss.sock '
92117 ) {
93- ob_implicit_flush (); // php7.x -> int, php8.x => bool, default 1 or true
94- $ this ->createSocket ($ host , $ port );
95- $ this ->openIPCSocket ($ ipcSocketPath );
96- $ this ->timers = new TimerCollection ();
97- $ this ->log ('Server created ' );
118+ $ this ->host = $ host ;
119+ $ this ->port = $ port ;
120+ $ this ->ipcSocketPath = $ ipcSocketPath ;
98121 }
99122
100123 /**
@@ -105,6 +128,12 @@ public function __construct(
105128 */
106129 public function run (): void
107130 {
131+ ob_implicit_flush ();
132+ $ this ->createSocket ($ this ->host , $ this ->port );
133+ $ this ->openIPCSocket ($ this ->ipcSocketPath );
134+ $ this ->timers = new TimerCollection ();
135+ $ this ->log ('Server created ' );
136+
108137 while (true ) {
109138 $ this ->timers ->runAll ();
110139
@@ -562,7 +591,15 @@ private function openIPCSocket(string $ipcSocketPath): void
562591 if (socket_bind ($ this ->icpSocket , $ ipcSocketPath ) === false ) {
563592 throw new \RuntimeException ('Could not bind to ipc socket. ' );
564593 }
565- $ this ->ipcSocketPath = $ ipcSocketPath ;
594+ if ($ this ->ipcOwner !== '' ) {
595+ chown ($ ipcSocketPath , $ this ->ipcOwner );
596+ }
597+ if ($ this ->ipcGroup !== '' ) {
598+ chgrp ($ ipcSocketPath , $ this ->ipcGroup );
599+ }
600+ if ($ this ->ipcMode !== 0 ) {
601+ chmod ($ ipcSocketPath , $ this ->ipcMode );
602+ }
566603 }
567604
568605 /**
@@ -594,4 +631,37 @@ private function handleIPC(): void
594631 throw new \RuntimeException ('Invalid IPC message received. ' );
595632 }
596633 }
634+
635+ /**
636+ * Sets the icpOwner value.
637+ *
638+ * @param string $owner
639+ * @return void
640+ */
641+ public function setIPCOwner (string $ owner ): void
642+ {
643+ $ this ->ipcOwner = $ owner ;
644+ }
645+
646+ /**
647+ * Sets the ipcGroup value.
648+ *
649+ * @param string $group
650+ * @return void
651+ */
652+ public function setIPCGroup (string $ group ): void
653+ {
654+ $ this ->ipcGroup = $ group ;
655+ }
656+
657+ /**
658+ * Sets the ipcMode value.
659+ *
660+ * @param int $mode
661+ * @return void
662+ */
663+ public function setIPCMode (int $ mode ): void
664+ {
665+ $ this ->ipcMode = $ mode ;
666+ }
597667}
0 commit comments