1
1
<?php
2
2
namespace Beanbun ;
3
3
4
- use Beanbun \Lib \Helper ;
5
4
use Beanbun \Exception \BeanbunException ;
5
+ use Beanbun \Lib \Helper ;
6
6
use Exception ;
7
7
use GuzzleHttp \Client ;
8
8
use Workerman \Lib \Timer ;
9
9
use Workerman \Worker ;
10
10
11
11
class Beanbun
12
12
{
13
- const VERSION = '1.0.3 ' ;
13
+ const VERSION = '1.0.4 ' ;
14
14
15
15
public $ id = null ;
16
16
public $ name = null ;
@@ -86,8 +86,8 @@ public function __construct($config = [])
86
86
global $ argv ;
87
87
$ this ->commands = $ argv ;
88
88
$ this ->name = isset ($ config ['name ' ])
89
- ? $ config ['name ' ]
90
- : current (explode ('. ' , $ this ->commands [0 ]));
89
+ ? $ config ['name ' ]
90
+ : current (explode ('. ' , $ this ->commands [0 ]));
91
91
$ this ->logFile = isset ($ config ['logFile ' ]) ? $ config ['logFile ' ] : __DIR__ . '/ ' . $ this ->name . '_access.log ' ;
92
92
$ this ->setQueue ();
93
93
$ this ->setDownloader ();
@@ -162,17 +162,17 @@ public function check()
162
162
$ error = false ;
163
163
$ text = '' ;
164
164
$ version_ok = $ pcntl_loaded = $ posix_loaded = true ;
165
- if (!version_compare (phpversion (), "5.3.3 " , ">= " )) {
165
+ if (!version_compare (phpversion (), "5.3.3 " , ">= " )) {
166
166
$ text .= "PHP Version >= 5.3.3 \033[31;40m [fail] \033[0m \n" ;
167
167
$ error = true ;
168
168
}
169
169
170
- if (!in_array ("pcntl " , get_loaded_extensions ())) {
170
+ if (!in_array ("pcntl " , get_loaded_extensions ())) {
171
171
$ text .= "Extension posix check \033[31;40m [fail] \033[0m \n" ;
172
172
$ error = true ;
173
173
}
174
174
175
- if (!in_array ("posix " , get_loaded_extensions ())) {
175
+ if (!in_array ("posix " , get_loaded_extensions ())) {
176
176
$ text .= "Extension posix check \033[31;40m [fail] \033[0m \n" ;
177
177
$ error = true ;
178
178
}
@@ -183,12 +183,12 @@ public function check()
183
183
"pcntl_signal_dispatch " ,
184
184
);
185
185
186
- if ($ disable_func_string = ini_get ("disable_functions " )) {
186
+ if ($ disable_func_string = ini_get ("disable_functions " )) {
187
187
$ disable_func_map = array_flip (explode (", " , $ disable_func_string ));
188
188
}
189
189
190
- foreach ($ check_func_map as $ func ) {
191
- if (isset ($ disable_func_map [$ func ])) {
190
+ foreach ($ check_func_map as $ func ) {
191
+ if (isset ($ disable_func_map [$ func ])) {
192
192
$ text .= "\033[31;40mFunction " . implode (', ' , $ check_func_map ) . "may be disabled. Please check disable_functions in php.ini \033[0m \n" ;
193
193
$ error = true ;
194
194
break ;
@@ -203,7 +203,7 @@ public function check()
203
203
204
204
public function initHooks ()
205
205
{
206
- $ this ->startWorkerHooks [] = function ($ beanbun ) {
206
+ $ this ->startWorkerHooks [] = function ($ beanbun ) {
207
207
$ beanbun ->id = $ beanbun ->worker ->id ;
208
208
$ beanbun ->log ("Beanbun worker {$ beanbun ->id } is starting ... " );
209
209
};
@@ -212,7 +212,7 @@ public function initHooks()
212
212
$ this ->startWorkerHooks [] = $ this ->startWorker ;
213
213
}
214
214
215
- $ this ->startWorkerHooks [] = function ($ beanbun ) {
215
+ $ this ->startWorkerHooks [] = function ($ beanbun ) {
216
216
$ beanbun ->queue ()->maxQueueSize = $ beanbun ->max ;
217
217
$ beanbun ->timer_id = Beanbun::timer ($ beanbun ->interval , [$ beanbun , 'crawler ' ]);
218
218
};
@@ -244,7 +244,7 @@ public function initHooks()
244
244
}
245
245
246
246
if ($ this ->daemonize ) {
247
- $ this ->afterDiscoverHooks [] = function ($ beanbun ) {
247
+ $ this ->afterDiscoverHooks [] = function ($ beanbun ) {
248
248
$ beanbun ->queue ()->queued ($ beanbun ->queue );
249
249
};
250
250
}
@@ -261,7 +261,7 @@ public function initHooks()
261
261
// 爬虫进程
262
262
public function onWorkerStart ($ worker )
263
263
{
264
- foreach ($ this ->startWorkerHooks as $ hook ) {
264
+ foreach ($ this ->startWorkerHooks as $ hook ) {
265
265
call_user_func ($ hook , $ this );
266
266
}
267
267
}
@@ -277,8 +277,7 @@ public function queue()
277
277
public function setQueue ($ callback = null , $ args = [
278
278
'host ' => '127.0.0.1 ' ,
279
279
'port ' => '2207 ' ,
280
- ])
281
- {
280
+ ]) {
282
281
if ($ callback === 'memory ' || $ callback === null ) {
283
282
$ this ->queueFactory = function ($ args ) {
284
283
return new \Beanbun \Queue \MemoryQueue ($ args );
@@ -290,7 +289,7 @@ public function setQueue($callback = null, $args = [
290
289
} else {
291
290
$ this ->queueFactory = $ callback ;
292
291
}
293
-
292
+
294
293
$ this ->queueArgs = $ args ;
295
294
}
296
295
@@ -322,10 +321,10 @@ public function log($msg)
322
321
public function setLog ($ callback = null )
323
322
{
324
323
$ this ->logFactory = $ callback === null
325
- ? function ($ msg , $ beanbun ) {
326
- echo date ('Y-m-d H:i:s ' ) . " {$ beanbun ->name } : $ msg \n" ;
327
- }
328
- : $ callback ;
324
+ ? function ($ msg , $ beanbun ) {
325
+ echo date ('Y-m-d H:i:s ' ) . " {$ beanbun ->name } : $ msg \n" ;
326
+ }
327
+ : $ callback ;
329
328
}
330
329
331
330
public function error ($ msg = null )
@@ -340,8 +339,8 @@ public function crawler()
340
339
array_shift ($ allHooks );
341
340
array_pop ($ allHooks );
342
341
343
- foreach ($ allHooks as $ hooks ) {
344
- foreach ($ this ->$ hooks as $ hook ) {
342
+ foreach ($ allHooks as $ hooks ) {
343
+ foreach ($ this ->$ hooks as $ hook ) {
345
344
call_user_func ($ hook , $ this );
346
345
}
347
346
}
@@ -364,7 +363,7 @@ public function crawler()
364
363
365
364
public function onWorkerStop ($ worker )
366
365
{
367
- foreach ($ this ->stopWorkerHooks as $ hook ) {
366
+ foreach ($ this ->stopWorkerHooks as $ hook ) {
368
367
call_user_func ($ hook , $ this );
369
368
}
370
369
}
@@ -407,7 +406,7 @@ public function defaultBeforeDownloadPage()
407
406
if (!is_array ($ queue )) {
408
407
$ this ->queue = $ queue = [
409
408
'url ' => $ queue ,
410
- 'options ' => []
409
+ 'options ' => [],
411
410
];
412
411
}
413
412
@@ -471,6 +470,6 @@ public function middleware($middleware, $action = 'handle')
471
470
$ middleware ->$ action ($ this );
472
471
} else {
473
472
call_user_func ($ middleware , $ this );
474
- }
473
+ }
475
474
}
476
475
}
0 commit comments