1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Channel; |
4
|
|
|
|
5
|
|
|
use Dazzle\Channel\Encoder\Encoder; |
6
|
|
|
use Dazzle\Channel\Encoder\EncoderInterface; |
7
|
|
|
use Dazzle\Channel\Model\ModelFactoryInterface; |
8
|
|
|
use Dazzle\Channel\Router\Router; |
9
|
|
|
use Dazzle\Channel\Router\RouterComposite; |
10
|
|
|
use Dazzle\Loop\LoopInterface; |
11
|
|
|
use Dazzle\Util\Factory\Factory; |
12
|
|
|
use Dazzle\Util\Parser\Json\JsonParser; |
13
|
|
|
|
14
|
|
|
class ChannelFactory extends Factory implements ChannelFactoryInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param string $name |
18
|
|
|
* @param ModelFactoryInterface $modelFactory |
19
|
|
|
* @param LoopInterface|null $loop |
20
|
|
|
*/ |
21
|
2 |
|
public function __construct($name, ModelFactoryInterface $modelFactory, LoopInterface $loop = null) |
22
|
|
|
{ |
23
|
2 |
|
parent::__construct(); |
24
|
|
|
|
25
|
2 |
|
$factory = $this; |
26
|
|
|
$factory |
27
|
2 |
|
->bindParam('name', $name) |
28
|
2 |
|
->bindParam('encoder', new Encoder(new JsonParser)) |
29
|
|
|
->bindParam('router', function() { |
30
|
1 |
|
return new RouterComposite([ |
31
|
1 |
|
'input' => new Router(), |
32
|
1 |
|
'output' => new Router() |
33
|
|
|
]); |
34
|
2 |
|
}) |
35
|
2 |
|
->bindParam('loop', $loop) |
36
|
|
|
; |
37
|
|
|
$factory |
38
|
|
|
->define(Channel::class, function($model, $config = []) use($factory, $modelFactory) { |
39
|
|
|
return new Channel( |
40
|
|
|
isset($config['name']) ? $config['name'] : $factory->getParam('name'), |
41
|
|
|
$modelFactory->create($model, [ $config ]), |
42
|
|
|
$factory->getParam('router'), |
43
|
|
|
$factory->getParam('encoder'), |
44
|
|
|
isset($config['loop']) ? $config['loop'] : $factory->getParam('loop') |
45
|
|
|
); |
46
|
2 |
|
}) |
47
|
2 |
|
->define(ChannelComposite::class, function($channels = [], $config = []) use($factory) { |
48
|
|
|
return new ChannelComposite( |
49
|
|
|
isset($config['name']) ? $config['name'] : $factory->getParam('name'), |
50
|
|
|
$channels, |
51
|
|
|
$factory->getParam('router'), |
52
|
|
|
isset($config['loop']) ? $config['loop'] : $factory->getParam('loop') |
53
|
|
|
); |
54
|
2 |
|
}) |
55
|
|
|
; |
56
|
2 |
|
} |
57
|
|
|
} |
58
|
|
|
|