Skip to content

Commit a631629

Browse files
committed
支持 facade调用
1 parent 610bc91 commit a631629

6 files changed

Lines changed: 80 additions & 5 deletions

File tree

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ composer require xiaodi/think-site-pusher:dev-master
1313

1414
### 临时配置
1515
```php
16+
use EasyPush\Facade\Pusher;
17+
1618
$config = ['site' => 'xxx', 'token' => 'xxx'];
1719
Pusher::baidu($config)->urls($urls);
1820
```
1921

2022
## 使用
23+
#### Facade
2124
```php
22-
use EasyPush\Pusher;
25+
use EasyPush\Facade\Pusher;
2326

2427
$urls = [
2528
'https://www.xiaodim.com/index.html',
@@ -34,4 +37,22 @@ Pusher::baidu()->update($urls);
3437

3538
// 删除链接
3639
Pusher::baidu()->delete($urls);
40+
41+
```
42+
43+
#### 助手函数
44+
```php
45+
$urls = [
46+
'https://www.xiaodim.com/index.html',
47+
'https://www.xiaodim.com/2019/10/25/thinkphp6-rpc-tutorial/'
48+
]
49+
50+
// 推送链接
51+
app('pusher')->baidu()->push($url);
52+
53+
// 更新链接
54+
app('pusher')->baidu()->update($urls);
55+
56+
// 删除链接
57+
app('pusher')->baidu()->delete($urls);
3758
```

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
"autoload": {
1717
"psr-4": {
1818
"EasyPush\\": "src/"
19-
}
19+
},
20+
"files": [
21+
"src/helper.php"
22+
]
2023
},
2124
"extra": {
2225
"think": {
26+
"services": [
27+
"EasyPush\\PusherService"
28+
],
2329
"config": {
2430
"push": "src/Config/push.php"
2531
}

src/Facade/Pusher.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace EasyPush\Facade;
4+
5+
use think\Facade;
6+
use EasyPush\Pusher as Accessor;
7+
8+
class Pusher extends Facade
9+
{
10+
protected static function getFacadeClass()
11+
{
12+
return Accessor::class;
13+
}
14+
}

src/Pusher.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,22 @@ class Pusher
1414

1515
private $client;
1616

17-
public function __callStatic($name, $arguments)
17+
private $namespace = "\\EasyPush\\Handles\\";
18+
19+
private $action = ['push', 'update', 'delete'];
20+
21+
public function __call($name, $arguments)
22+
{
23+
if (!in_array($name, $this->action)) {
24+
return $this->setHandle($name, $arguments);
25+
}
26+
27+
return $this->exec($name, $arguments);
28+
}
29+
30+
private function setHandle($name, $arguments)
1831
{
19-
$className = "\\EasyPush\\Handles\\{$name}";
32+
$className = $this->namespace . $name;
2033

2134
if (false === class_exists($className)) {
2235
throw new ClassNotFound(
@@ -39,7 +52,7 @@ public function __callStatic($name, $arguments)
3952
return $instance;
4053
}
4154

42-
public function __call($method, $argc)
55+
private function exec($method, $argc)
4356
{
4457
$this->handle->setParams($method, $argc[0]);
4558

src/PusherService.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace EasyPush;
3+
4+
use think\Service;
5+
use EasyPush\Pusher;
6+
7+
class PusherService extends Service
8+
{
9+
public function register()
10+
{
11+
$this->app->bind('pusher', Pusher::class);
12+
}
13+
}

src/helper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use EasyPush\Pusher;
4+
5+
// 兼容 5.1 版本
6+
if (strpos(\think\App::VERSION, '5.1') !== false) {
7+
bind('pusher', Pusher::class);
8+
}

0 commit comments

Comments
 (0)