Skip to content

Commit

Permalink
修改版本号与格式
Browse files Browse the repository at this point in the history
  • Loading branch information
kiddyuchina committed Jun 11, 2017
1 parent 3b01a2b commit 3b84c35
Show file tree
Hide file tree
Showing 7 changed files with 561 additions and 757 deletions.
8 changes: 4 additions & 4 deletions docs/chs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ $beanbun->afterDownloadPage = function($beanbun) {
``` php
// 输出下载页面后发现的所有链接,不加入队列
$beanbun->discoverUrl = function($beanbun) {
$urls = Helper::getUrlbyHtml($beanbun->page, $beanbun->url);
$urls = Helper::getUrlByHtml($beanbun->page, $beanbun->url);
print_r($urls);
};
```
Expand Down Expand Up @@ -727,7 +727,7 @@ $beanbun->beforDownloadPage = function($beanbun) {
``` php
// 取出页面中所有的 url 加入队列,均以 POST 来请求
$beanbun->discoverUrl = function ($beanbun) {
$urls = Helper::getUrlbyHtml($beanbun->page, $beanbun->url);
$urls = Helper::getUrlByHtml($beanbun->page, $beanbun->url);
foreach ($urls as $url) {
$beanbun->queue()->add($url, [
'method' => 'POST'
Expand Down Expand Up @@ -913,7 +913,7 @@ Helper 类中定义了一些辅助方法,帮助用户更方便的爬取网页

### 静态方法

#### getUrlbyHtml
#### getUrlByHtml
<p class="tip">
返回网页中的完整链接。接受两个参数,第一个参数为网页 html,第二个参数为网页的 url
</p>
Expand All @@ -931,7 +931,7 @@ $html =<<<STR
</ul>
STR;

$urls = Helper::getUrlbyHtml($html, $url);
$urls = Helper::getUrlByHtml($html, $url);
print_r($urls);

// Array
Expand Down
51 changes: 25 additions & 26 deletions src/Beanbun.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php
namespace Beanbun;

use Beanbun\Lib\Helper;
use Beanbun\Exception\BeanbunException;
use Beanbun\Lib\Helper;
use Exception;
use GuzzleHttp\Client;
use Workerman\Lib\Timer;
use Workerman\Worker;

class Beanbun
{
const VERSION = '1.0.3';
const VERSION = '1.0.4';

public $id = null;
public $name = null;
Expand Down Expand Up @@ -86,8 +86,8 @@ public function __construct($config = [])
global $argv;
$this->commands = $argv;
$this->name = isset($config['name'])
? $config['name']
: current(explode('.', $this->commands[0]));
? $config['name']
: current(explode('.', $this->commands[0]));
$this->logFile = isset($config['logFile']) ? $config['logFile'] : __DIR__ . '/' . $this->name . '_access.log';
$this->setQueue();
$this->setDownloader();
Expand Down Expand Up @@ -162,17 +162,17 @@ public function check()
$error = false;
$text = '';
$version_ok = $pcntl_loaded = $posix_loaded = true;
if(!version_compare(phpversion(), "5.3.3", ">=")) {
if (!version_compare(phpversion(), "5.3.3", ">=")) {
$text .= "PHP Version >= 5.3.3 \033[31;40m [fail] \033[0m\n";
$error = true;
}

if(!in_array("pcntl", get_loaded_extensions())) {
if (!in_array("pcntl", get_loaded_extensions())) {
$text .= "Extension posix check \033[31;40m [fail] \033[0m\n";
$error = true;
}

if(!in_array("posix", get_loaded_extensions())) {
if (!in_array("posix", get_loaded_extensions())) {
$text .= "Extension posix check \033[31;40m [fail] \033[0m\n";
$error = true;
}
Expand All @@ -183,12 +183,12 @@ public function check()
"pcntl_signal_dispatch",
);

if($disable_func_string = ini_get("disable_functions")) {
if ($disable_func_string = ini_get("disable_functions")) {
$disable_func_map = array_flip(explode(",", $disable_func_string));
}

foreach($check_func_map as $func) {
if(isset($disable_func_map[$func])) {
foreach ($check_func_map as $func) {
if (isset($disable_func_map[$func])) {
$text .= "\033[31;40mFunction " . implode(', ', $check_func_map) . "may be disabled. Please check disable_functions in php.ini\033[0m\n";
$error = true;
break;
Expand All @@ -203,7 +203,7 @@ public function check()

public function initHooks()
{
$this->startWorkerHooks[] = function($beanbun) {
$this->startWorkerHooks[] = function ($beanbun) {
$beanbun->id = $beanbun->worker->id;
$beanbun->log("Beanbun worker {$beanbun->id} is starting ...");
};
Expand All @@ -212,7 +212,7 @@ public function initHooks()
$this->startWorkerHooks[] = $this->startWorker;
}

$this->startWorkerHooks[] = function($beanbun) {
$this->startWorkerHooks[] = function ($beanbun) {
$beanbun->queue()->maxQueueSize = $beanbun->max;
$beanbun->timer_id = Beanbun::timer($beanbun->interval, [$beanbun, 'crawler']);
};
Expand Down Expand Up @@ -244,7 +244,7 @@ public function initHooks()
}

if ($this->daemonize) {
$this->afterDiscoverHooks[] = function($beanbun) {
$this->afterDiscoverHooks[] = function ($beanbun) {
$beanbun->queue()->queued($beanbun->queue);
};
}
Expand All @@ -261,7 +261,7 @@ public function initHooks()
// 爬虫进程
public function onWorkerStart($worker)
{
foreach($this->startWorkerHooks as $hook) {
foreach ($this->startWorkerHooks as $hook) {
call_user_func($hook, $this);
}
}
Expand All @@ -277,8 +277,7 @@ public function queue()
public function setQueue($callback = null, $args = [
'host' => '127.0.0.1',
'port' => '2207',
])
{
]) {
if ($callback === 'memory' || $callback === null) {
$this->queueFactory = function ($args) {
return new \Beanbun\Queue\MemoryQueue($args);
Expand All @@ -290,7 +289,7 @@ public function setQueue($callback = null, $args = [
} else {
$this->queueFactory = $callback;
}

$this->queueArgs = $args;
}

Expand Down Expand Up @@ -322,10 +321,10 @@ public function log($msg)
public function setLog($callback = null)
{
$this->logFactory = $callback === null
? function ($msg, $beanbun) {
echo date('Y-m-d H:i:s') . " {$beanbun->name} : $msg\n";
}
: $callback;
? function ($msg, $beanbun) {
echo date('Y-m-d H:i:s') . " {$beanbun->name} : $msg\n";
}
: $callback;
}

public function error($msg = null)
Expand All @@ -340,8 +339,8 @@ public function crawler()
array_shift($allHooks);
array_pop($allHooks);

foreach($allHooks as $hooks) {
foreach($this->$hooks as $hook) {
foreach ($allHooks as $hooks) {
foreach ($this->$hooks as $hook) {
call_user_func($hook, $this);
}
}
Expand All @@ -364,7 +363,7 @@ public function crawler()

public function onWorkerStop($worker)
{
foreach($this->stopWorkerHooks as $hook) {
foreach ($this->stopWorkerHooks as $hook) {
call_user_func($hook, $this);
}
}
Expand Down Expand Up @@ -407,7 +406,7 @@ public function defaultBeforeDownloadPage()
if (!is_array($queue)) {
$this->queue = $queue = [
'url' => $queue,
'options' => []
'options' => [],
];
}

Expand Down Expand Up @@ -471,6 +470,6 @@ public function middleware($middleware, $action = 'handle')
$middleware->$action($this);
} else {
call_user_func($middleware, $this);
}
}
}
}
Loading

0 comments on commit 3b84c35

Please sign in to comment.