Skip to content

Commit a9f1c5d

Browse files
authored
Merge pull request #2 from reallyli/add-pusher-component
添加 pusher 组件
2 parents 82fe42e + 7d00be6 commit a9f1c5d

File tree

8 files changed

+186
-30
lines changed

8 files changed

+186
-30
lines changed

config/unicomponent.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: reallyli
5+
* Date: 18/10/11
6+
* Time: 下午2:06.
7+
*/
8+
9+
return [
10+
'components' => [
11+
/*
12+
|--------------------------------------------------------------------------
13+
| Log Formatter
14+
|--------------------------------------------------------------------------
15+
|
16+
| Log formatting
17+
|
18+
*/
19+
'log_formatter' => [
20+
'provider' => \Reallyli\LaravelUnicomponent\Components\LogFormatter\LogFormatterService::class,
21+
'configs' => [],
22+
],
23+
/*
24+
|--------------------------------------------------------------------------
25+
| Pusher
26+
|--------------------------------------------------------------------------
27+
|
28+
| Internal message push
29+
|
30+
*/
31+
'pusher' => [
32+
'provider' => \Reallyli\LaravelUnicomponent\Components\Pusher\PusherService::class,
33+
'configs' => [
34+
'pusher_url' => env('UNICOMPONENT_PUSHER_URL'),
35+
],
36+
],
37+
],
38+
];

src/Components/Pusher/Pusher.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: uniqueway
5+
* Date: 2018/11/20
6+
* Time: 上午11:39.
7+
*/
8+
9+
namespace Reallyli\LaravelUnicomponent\Components\Pusher;
10+
11+
class Pusher
12+
{
13+
/**
14+
* trigger.
15+
*
16+
* @author reallyli <[email protected]>
17+
* @since 2018/11/20
18+
* @param mixed $channels
19+
* @param string $eventName
20+
* @param array $data
21+
* @param array $params
22+
* @return mixed
23+
*/
24+
public function trigger($channels, string $eventName, array $data, array $params = [])
25+
{
26+
$params['channel'] = is_string($channels) ? [$channels] : (array) $channels;
27+
$params['data'] = $data;
28+
$params['name'] = $eventName;
29+
30+
$this->sendRequest($this->getPusherUrl(), $params);
31+
}
32+
33+
/**
34+
* Send Request.
35+
*
36+
* @author reallyli <[email protected]>
37+
* @since 2018/11/20
38+
* @param string $url
39+
* @param array $data
40+
* @return mixed
41+
*/
42+
protected function sendRequest(string $url, array $data)
43+
{
44+
$ch = curl_init($url);
45+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
46+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
47+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
48+
curl_setopt(
49+
$ch,
50+
CURLOPT_HTTPHEADER,
51+
[
52+
'Content-Type: application/json',
53+
'Content-Length: '.strlen($data),
54+
]
55+
);
56+
57+
$response = curl_exec($ch);
58+
59+
if ($response === false) {
60+
logger()->error('[LaravelUnicomponent] Pusher Send Request Error, data:'.json_encode($data));
61+
62+
return false;
63+
}
64+
65+
return $response;
66+
}
67+
68+
/**
69+
* Get Pusher Url.
70+
*
71+
* @author reallyli <[email protected]>
72+
* @since 2018/11/20
73+
* @return string
74+
*/
75+
protected function getPusherUrl()
76+
{
77+
$pusherUrl = config('unicomponent.pusher.configs.pusher_url');
78+
79+
throw_unless(filter_var($pusherUrl, FILTER_VALIDATE_URL), '\Exception', 'pusher url illegal');
80+
81+
return $pusherUrl;
82+
}
83+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: uniqueway
5+
* Date: 2018/11/20
6+
* Time: 上午11:38.
7+
*/
8+
9+
namespace Reallyli\LaravelUnicomponent\Components\Pusher;
10+
11+
use Reallyli\LaravelUnicomponent\UnicomponentServiceInterface;
12+
13+
class PusherService implements UnicomponentServiceInterface
14+
{
15+
public function alias()
16+
{
17+
return 'pusher';
18+
}
19+
20+
public function provider()
21+
{
22+
return Pusher::class;
23+
}
24+
}

src/UnicomponentConfig.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/UnicomponentServiceManager.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ class UnicomponentServiceManager
1616
private $servicesComponents = [];
1717

1818
/**
19-
* @var array
19+
* @var mixed
2020
*/
2121
private $config;
2222

2323
/**
24-
* Method description:__construct.
24+
* Construct.
2525
*
2626
* @author reallyli <[email protected]>
2727
* @since 18/10/11
28-
* @param array $config
29-
* @return mixed
28+
* @param mixed $config
29+
* @return void
3030
* 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
3131
*/
32-
public function __construct(array $config = [])
32+
public function __construct($config)
3333
{
3434
$this->config = $config;
3535
$this->registerDefaultComponent();
@@ -65,8 +65,8 @@ public function registerDefaultComponent()
6565
return;
6666
}
6767

68-
foreach ($this->config['component_provider'] as $component) {
69-
$this->registerComponent(new $component);
68+
foreach ($this->config['components'] as $component) {
69+
$this->registerComponent(new $component['provider']);
7070
}
7171
}
7272

@@ -130,4 +130,17 @@ public function __call(string $method, array $parameters)
130130
{
131131
return $this->getServiceComponent($method);
132132
}
133+
134+
/**
135+
* Method description:__get.
136+
*
137+
* @author reallyli <[email protected]>
138+
* @since 2018/11/20
139+
* @param string $componentName
140+
* @return mixed
141+
*/
142+
public function __get(string $componentName)
143+
{
144+
return $this->getServiceComponent($componentName);
145+
}
133146
}

src/UnicomponentServiceProvider.php

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

1313
class UnicomponentServiceProvider extends ServiceProvider
1414
{
15+
/**
16+
* Bootstrap the application services.
17+
*/
18+
public function boot()
19+
{
20+
$this->publishes([
21+
__DIR__.'/../config/unicomponent.php' => config_path('unicomponent.php'),
22+
], 'config');
23+
}
24+
1525
/**
1626
* @return void
1727
*/
1828
public function register()
1929
{
20-
$this->app->singleton('unicomponent', function () {
21-
return new UnicomponentServiceManager(include_once(__DIR__.'/UnicomponentConfig.php'));
30+
$this->app->singleton('unicomponent', function ($app) {
31+
return new UnicomponentServiceManager($app['config']['unicomponent']);
2232
});
2333
}
2434

tests/UnicomponentTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Reallyli\LaravelUnicomponent\Tests;
1010

1111
use PHPUnit\Framework\TestCase;
12+
use Reallyli\LaravelUnicomponent\Components\Pusher\Pusher;
1213
use Reallyli\LaravelUnicomponent\UnicomponentServiceManager;
1314
use Reallyli\LaravelUnicomponent\Tests\Example\ExampleService;
1415

@@ -19,13 +20,12 @@ class UnicomponentTest extends TestCase
1920
*
2021
* @author reallyli <[email protected]>
2122
* @since 18/10/11
22-
* @param
2323
* @return mixed
2424
* 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
2525
*/
2626
public function testRegisterComponent()
2727
{
28-
$unicomponentServiceManager = new UnicomponentServiceManager();
28+
$unicomponentServiceManager = new UnicomponentServiceManager([]);
2929

3030
$unicomponentServiceManager->registerComponent(new ExampleService());
3131

@@ -37,7 +37,6 @@ public function testRegisterComponent()
3737
*
3838
* @author reallyli <[email protected]>
3939
* @since 18/10/11
40-
* @param
4140
* @return mixed
4241
* 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
4342
*/
@@ -54,16 +53,15 @@ public function testConfigurationComponents()
5453
*
5554
* @author reallyli <[email protected]>
5655
* @since 18/10/11
57-
* @param
5856
* @return mixed
5957
* 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
6058
*/
6159
public function testGetComponent()
6260
{
63-
$unicompoentConfig = include_once __DIR__.'/../src/UnicomponentConfig.php';
61+
$unicompoentConfig = include_once __DIR__.'/../config/unicomponent.php';
6462

6563
$unicomponentServiceManager = new UnicomponentServiceManager($unicompoentConfig);
6664

67-
$this->assertEquals(1, is_callable($unicomponentServiceManager->logFormatter()));
65+
$this->assertInstanceOf(Pusher::class, $unicomponentServiceManager->pusher);
6866
}
6967
}

tests/unicomponentConfig.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
*/
88

99
return [
10-
'component_provider' => [
11-
\Reallyli\LaravelUnicomponent\Tests\Example\ExampleService::class,
10+
'components' => [
11+
'example' => [
12+
'provider' => \Reallyli\LaravelUnicomponent\Tests\Example\ExampleService::class,
13+
'configs' => [],
14+
],
1215
],
1316
];

0 commit comments

Comments
 (0)