1010/**
1111 * Ubiquity\servers\workerman$WorkermanServer
1212 * This class is part of Ubiquity
13- * @author jcheron <myaddressmail@gmail.com>
14- * @version 1.0.0
1513 *
14+ * @author jcheron <myaddressmail@gmail.com>
15+ * @version 1.0.2
1616 */
1717class WorkermanServer {
1818
1919 /**
20+ *
2021 * @var \Workerman\Worker
2122 */
2223 private $ server ;
@@ -28,9 +29,9 @@ class WorkermanServer {
2829 private $ basedir ;
2930
3031 private $ options ;
31-
32+
3233 private $ wCount ;
33-
34+
3435 public $ onWorkerStart ;
3536
3637 /**
@@ -75,11 +76,11 @@ private function removePidFile(): void {
7576 private function configure ($ http ) {
7677 $ http ->set ($ this ->options );
7778 }
78-
79+
7980 public function init ($ config , $ basedir ) {
8081 $ this ->config = $ config ;
8182 $ this ->basedir = $ basedir ;
82- $ this ->httpInstance = new WorkermanHttp ();
83+ $ this ->httpInstance = new WorkermanHttp ();
8384 \Ubiquity \controllers \Startup::init ($ config );
8485 \Ubiquity \controllers \Startup::setHttpInstance ($ this ->httpInstance );
8586 }
@@ -99,61 +100,61 @@ public function getOption(string $key) {
99100 }
100101
101102 public function setOptions ($ options = []) {
102- $ this ->options = $ options ;
103+ $ this ->options = $ options ;
103104 }
104105
105106 public function run ($ host , $ port , $ options = []) {
106107 $ this ->setOptions ($ options );
107- $ this ->server = new Worker ("http:// $ host: $ port " ,$ this ->options );
108- $ this ->server ->count = $ this ->wCount ?? 4 ;
109- if (isset ($ this ->onWorkerStart )){
110- $ this ->server ->onWorkerStart = $ this ->onWorkerStart ;
108+ $ this ->server = new Worker ("http:// $ host: $ port " , $ this ->options );
109+ $ this ->server ->count = $ this ->wCount ?? 4 ;
110+ if (isset ($ this ->onWorkerStart )) {
111+ $ this ->server ->onWorkerStart = $ this ->onWorkerStart ;
111112 }
112- $ this ->server ->onMessage =function ($ connection ,$ datas ){
113- return $ this ->handle ($ connection ,$ datas );
113+ $ this ->server ->onMessage = function ($ connection , $ datas ) {
114+ return $ this ->handle ($ connection , $ datas );
114115 };
115116 Worker::runAll ();
116117 }
117-
118118
119- protected function handle (ConnectionInterface $ connection ,$ datas ) {
120- //$_REQUEST['REQUEST_TIME_FLOAT']=\microtime(true);
121- Http::header ('Date: ' . \gmdate ('D, d M Y H:i:s ' ). ' GMT ' );
119+ protected function handle (ConnectionInterface $ connection , $ datas ) {
120+ // $_REQUEST['REQUEST_TIME_FLOAT']=\microtime(true);
121+ Http::header ('Date: ' . \gmdate ('D, d M Y H:i:s ' ) . ' GMT ' );
122122 $ _GET ['c ' ] = '' ;
123123 $ uri = \ltrim (\urldecode (\parse_url ($ _SERVER ['REQUEST_URI ' ], PHP_URL_PATH )), '/ ' );
124- if (($ uri == null || ! ($ fe= \file_exists ($ this ->basedir . '/../ ' . $ uri ))) && ($ uri!= 'favicon.ico ' )) {
124+ if (($ uri == null || ! ($ fe = \file_exists ($ this ->basedir . '/../ ' . $ uri ))) && ($ uri != 'favicon.ico ' )) {
125125 $ _GET ['c ' ] = $ uri ;
126126 } else {
127- if ($ fe ){
128- Http::header ('Content-Type: ' . HttpCache::$ header ['Accept ' ] ?? 'text/html; charset=utf-8 ' ,true );
127+ if ($ fe ) {
128+ Http::header ('Content-Type: ' . HttpCache::$ header ['Accept ' ] ?? 'text/html; charset=utf-8 ' , true );
129129 return $ connection ->send (\file_get_contents ($ this ->basedir . '/../ ' . $ uri ));
130- }else {
131- Http::header ('Content-Type: ' . HttpCache::$ header ['Accept ' ] ?? 'text/html; charset=utf-8 ' ,true ,404 );
132- return $ connection ->send ($ uri. ' not found! ' );
130+ } else {
131+ Http::header ('Content-Type: ' . HttpCache::$ header ['Accept ' ] ?? 'text/html; charset=utf-8 ' , true , 404 );
132+ return $ connection ->send ($ uri . ' not found! ' );
133133 }
134134 return ;
135135 }
136136
137137 $ this ->httpInstance ->setDatas ($ datas );
138138 \ob_start ();
139- \Ubiquity \controllers \Startup ::forward ($ _GET ['c ' ]);
139+ \Ubiquity \controllers \StartupAsync ::forward ($ _GET ['c ' ]);
140140 return $ connection ->send (\ob_get_clean ());
141141 }
142+
142143 /**
143144 * Sets the worker count
145+ *
144146 * @param int $wCount
145147 */
146148 public function setWCount ($ wCount ) {
147149 $ this ->wCount = $ wCount ;
148150 }
149-
150- public function setDefaultCount ($ multi =1 ){
151- $ this ->wCount = ((int ) \shell_exec ('nproc ' ) ??4 )*$ multi ;
152- }
153-
154- public function daemonize ($ value =true ){
155- Worker::$ daemonize =$ value ;
151+
152+ public function setDefaultCount ($ multi = 1 ) {
153+ $ this ->wCount = ((int ) \shell_exec ('nproc ' ) ?? 4 ) * $ multi ;
156154 }
157155
156+ public function daemonize ($ value = true ) {
157+ Worker::$ daemonize = $ value ;
158+ }
158159}
159160
0 commit comments