Skip to content

Commit 610bc91

Browse files
committed
update
1 parent 29eb9db commit 610bc91

7 files changed

Lines changed: 77 additions & 20 deletions

File tree

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,32 @@ ThinkPHP 扩展包 网站链接提交
66
composer require xiaodi/think-site-pusher:dev-master
77
```
88

9+
## 配置
10+
11+
### 默认配置
12+
项目根目录 `config/push.php`
13+
14+
### 临时配置
15+
```php
16+
$config = ['site' => 'xxx', 'token' => 'xxx'];
17+
Pusher::baidu($config)->urls($urls);
18+
```
19+
920
## 使用
1021
```php
11-
use xiaodi\EasyPush\Pusher;
22+
use EasyPush\Pusher;
1223

1324
$urls = [
1425
'https://www.xiaodim.com/index.html',
1526
'https://www.xiaodim.com/2019/10/25/thinkphp6-rpc-tutorial/'
1627
]
1728

1829
// 推送链接
19-
Pusher::baidu()->urls($urls);
30+
Pusher::baidu()->push($urls);
2031

2132
// 更新链接
2233
Pusher::baidu()->update($urls);
2334

2435
// 删除链接
25-
Pusher::baidu()->del($urls);
36+
Pusher::baidu()->delete($urls);
2637
```

composer.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "xiaodi/think-site-pusher",
33
"license": "MIT",
4+
"type": "think-extend",
45
"authors": [
56
{
67
"name": "xiaodi",
@@ -14,7 +15,17 @@
1415
},
1516
"autoload": {
1617
"psr-4": {
17-
"xiaodi\\EasyPush\\": "src/"
18+
"EasyPush\\": "src/"
19+
}
20+
},
21+
"extra": {
22+
"think": {
23+
"config": {
24+
"push": "src/Config/push.php"
25+
}
26+
},
27+
"think-config": {
28+
"push": "src/Config/push.php"
1829
}
1930
}
2031
}

src/Config/push.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
return [
4+
'baidu' => [
5+
// 在搜索资源平台验证的站点
6+
'site' => '',
7+
8+
// 在搜索资源平台申请的推送用的准入密钥
9+
'token' => ''
10+
]
11+
];

src/Exception/ClassNotFound.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace EasyPush\Exception;
4+
5+
class ClassNotFound extends Exception
6+
{ }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPush\Exception;
4+
5+
class InvalidArgumentException extends \InvalidArgumentException
6+
{
7+
}

src/Handles/Baidu.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace EasyPush\Handles;
44

5+
use EasyPush\Exception\InvalidArgumentException;
56
use EasyPush\Interfaces\PushInterface;
67

78
class Baidu implements PushInterface
89
{
910
const URI = 'http://data.zz.baidu.com';
1011

12+
private $actions = ['push' => '/urls', 'update' => 'update', 'delete' => '/del'];
13+
1114
private $query = [];
1215

1316
private $body = '';
@@ -22,15 +25,23 @@ public function __construct(array $config = [])
2225
if (!empty($config)) {
2326
$this->config = array_merge($this->config, $config);
2427
} else {
25-
$this->config = array_merge($this->config, config('push'));
28+
$this->config = array_merge($this->config, config('push.baidu'));
29+
}
30+
31+
if (!$this->config['site'] || !$this->config['token']) {
32+
throw new InvalidArgumentException('site token配置不能为空');
2633
}
2734

2835
$this->setQuery($this->config['site'], $this->config['token']);
2936
}
3037

3138
public function setParams($method, $url)
3239
{
33-
$this->path = "/{$method}";
40+
if (!array_key_exists($method, $this->actions)) {
41+
throw new InvalidArgumentException(sprintf('"%s" not allow', $method));
42+
}
43+
44+
$this->path = $this->actions[$method];
3445
$this->setBody($url);
3546
}
3647

src/Pusher.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use GuzzleHttp\Client;
66
use EasyPush\Exception\Exception;
7+
use EasyPush\Exception\ClassNotFound;
8+
use EasyPush\Interfaces\PushInterface;
79
use ReflectionClass;
810

911
class Pusher
@@ -14,21 +16,22 @@ class Pusher
1416

1517
public function __callStatic($name, $arguments)
1618
{
17-
$handle = "\\EasyPush\\Handles\\{$name}";
19+
$className = "\\EasyPush\\Handles\\{$name}";
1820

19-
if (false === class_exists($handle)) {
20-
throw new Exception("{$handle} Not Found!");
21+
if (false === class_exists($className)) {
22+
throw new ClassNotFound(
23+
sprintf('Class "%s" Not Found', $className)
24+
);
2125
}
2226

23-
$reflectionClass = new ReflectionClass($handle);
24-
$interfaces = $reflectionClass->getInterfaceNames();
25-
26-
if (empty($interfaces) || !in_array('EasyPush\\Interfaces\\PushInterface', $interfaces)) {
27-
throw new Exception("{$handle} 没有继承相关接口类");
27+
$reflectionClass = new ReflectionClass($className);
28+
$handle = $reflectionClass->newInstanceArgs($arguments);
29+
if (false === $handle instanceof PushInterface) {
30+
throw new Exception("{$className} is not instanceof PushInterface");
2831
}
2932

3033
$instance = new static();
31-
$instance->handle = new $handle($arguments);
34+
$instance->handle = $handle;
3235
$instance->client = new Client([
3336
'base_uri' => $instance->handle->getUri()
3437
]);
@@ -50,12 +53,9 @@ public function __call($method, $argc)
5053
'body' => $body
5154
]);
5255
} catch (\GuzzleHttp\Exception\RequestException $e) {
53-
54-
} catch (\Excpetion $e) {
55-
56+
return ['code' => $e->getResponse()->getStatusCode(), 'result' => null];
5657
}
5758

58-
59-
return $response->getBody()->getContents();
59+
return ['code' => 200, 'result' => $response->getBody()->getContents()];
6060
}
6161
}

0 commit comments

Comments
 (0)