1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Q; |
3
|
|
|
|
4
|
|
|
use Aws\Sqs\SqsClient; |
5
|
|
|
use Pheanstalk\PheanstalkInterface; |
6
|
|
|
use Wandu\DI\ContainerInterface; |
7
|
|
|
use Wandu\DI\ServiceProviderInterface; |
8
|
|
|
use Wandu\Q\Adapter\BeanstalkdAdapter; |
9
|
|
|
use Wandu\Q\Adapter\NullAdapter; |
10
|
|
|
use Wandu\Q\Adapter\SqsAdapter; |
11
|
|
|
use Wandu\Q\Contracts\AdapterInterface; |
12
|
|
|
use Wandu\Q\Contracts\SerializerInterface; |
13
|
|
|
use Wandu\Q\Serializer\JsonSerializer; |
14
|
|
|
use Wandu\Q\Serializer\PhpSerializer; |
15
|
|
|
use function Wandu\Foundation\config; |
16
|
|
|
|
17
|
|
|
class QueueServiceProvider implements ServiceProviderInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
public function register(ContainerInterface $app) |
23
|
|
|
{ |
24
|
|
|
$app->closure(SerializerInterface::class, function () { |
25
|
|
|
switch (config('q.serializer')) { |
26
|
|
|
case 'json': |
27
|
|
|
return new JsonSerializer(); |
28
|
|
|
} |
29
|
|
|
return new PhpSerializer(); |
30
|
|
|
}); |
31
|
|
|
$app->closure(AdapterInterface::class, function (ContainerInterface $app) { |
32
|
|
|
switch (config('q.type')) { |
33
|
|
|
case 'beanstalkd': |
34
|
|
|
$app->assert(PheanstalkInterface::class, 'pda/pheanstalk'); |
35
|
|
|
return new BeanstalkdAdapter($app->get(PheanstalkInterface::class)); |
36
|
|
|
case 'sqs': |
37
|
|
|
$app->assert(SqsClient::class, 'aws/aws-sdk-php'); |
38
|
|
|
return new SqsAdapter($app->get(SqsClient::class), config('q.sqs.url')); |
39
|
|
|
} |
40
|
|
|
return new NullAdapter(); |
41
|
|
|
}); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function boot(ContainerInterface $app) |
48
|
|
|
{ |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|