Repository navigation
Expand file tree
/
Copy pathServerFactory.php
More file actions
113 lines (95 loc) · 2.46 KB
/
Copy pathServerFactory.php
File metadata and controls
113 lines (95 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Server;
use Hyperf\Server\Entry\EventDispatcher;
use Hyperf\Server\Entry\Logger;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
class ServerFactory
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @var ServerInterface
*/
protected $server;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function configure(array $config)
{
$this->getServer()->init(new ServerConfig($config));
}
public function start()
{
return $this->getServer()->start();
}
public function getServer(): ServerInterface
{
if (! $this->server instanceof ServerInterface) {
$this->server = new Server(
$this->container,
$this->getLogger(),
$this->getEventDispatcher()
);
}
return $this->server;
}
public function setServer(Server $server): self
{
$this->server = $server;
return $this;
}
public function getEventDispatcher(): EventDispatcherInterface
{
if ($this->eventDispatcher instanceof EventDispatcherInterface) {
return $this->eventDispatcher;
}
return $this->getDefaultEventDispatcher();
}
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): self
{
$this->eventDispatcher = $eventDispatcher;
return $this;
}
public function getLogger(): LoggerInterface
{
if ($this->logger instanceof LoggerInterface) {
return $this->logger;
}
return $this->getDefaultLogger();
}
public function setLogger(LoggerInterface $logger): self
{
$this->logger = $logger;
return $this;
}
private function getDefaultEventDispatcher(): EventDispatcher
{
return new EventDispatcher();
}
private function getDefaultLogger(): Logger
{
return new Logger();
}
}