1 | <?php |
||
26 | class Node |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * Current version of PhuninNode |
||
31 | */ |
||
32 | const VERSION = '0.3.0-DEV'; |
||
33 | |||
34 | /** |
||
35 | * @var LoopInterface |
||
36 | */ |
||
37 | private $loop; |
||
38 | |||
39 | /** |
||
40 | * @var Socket |
||
41 | */ |
||
42 | private $socket; |
||
43 | |||
44 | /** |
||
45 | * @var Configuration |
||
46 | */ |
||
47 | private $configuration; |
||
48 | |||
49 | /** |
||
50 | * @var LoggerInterface |
||
51 | */ |
||
52 | private $logger; |
||
53 | |||
54 | /** |
||
55 | * Collection of Plugins in use (PluginInterface) |
||
56 | * |
||
57 | * @var \SplObjectStorage |
||
58 | */ |
||
59 | private $plugins; |
||
60 | |||
61 | /** |
||
62 | * Collection of connected clients (ConnectionContext) |
||
63 | * |
||
64 | * @var \SplObjectStorage |
||
65 | */ |
||
66 | private $connections; |
||
67 | |||
68 | private $commands; |
||
69 | |||
70 | /** |
||
71 | * @var array |
||
72 | */ |
||
73 | private $defaultConfiguration = [ |
||
74 | 'hostname' => 'HOSTNAME', |
||
75 | 'verbose' => false, |
||
76 | ]; |
||
77 | |||
78 | /** |
||
79 | * @param LoopInterface $loop |
||
80 | * @param Socket $socket The socket to bind on |
||
81 | * @param Configuration $configuration Node configuration |
||
82 | * @param LoggerInterface $logger Logger |
||
83 | */ |
||
84 | 46 | public function __construct( |
|
85 | LoopInterface $loop, |
||
86 | Socket $socket, |
||
87 | CommandsCollection $commands, |
||
88 | Configuration $configuration = null, |
||
89 | LoggerInterface $logger = null |
||
90 | ) { |
||
91 | |||
92 | 46 | if (false === strpos(PHP_VERSION, 'hiphop')) { |
|
93 | 46 | gc_enable(); |
|
94 | } |
||
95 | |||
96 | 46 | $this->loop = $loop; |
|
97 | 46 | $this->socket = $socket; |
|
98 | |||
99 | 46 | if ($configuration === null) { |
|
100 | 46 | $configuration = new Configuration(); |
|
101 | } |
||
102 | 46 | $configuration->applyDefaults($this->defaultConfiguration); |
|
103 | 46 | $this->configuration = $configuration; |
|
104 | |||
105 | 46 | $this->commands = $commands; |
|
106 | 46 | $this->commands->setNode($this); |
|
107 | |||
108 | 46 | if ($logger === null) { |
|
109 | 46 | $logger = new NullLogger(); |
|
110 | } |
||
111 | 46 | $this->logger = $logger; |
|
112 | |||
113 | 46 | $this->plugins = new \SplObjectStorage; |
|
114 | 46 | $this->connections = new \SplObjectStorage; |
|
115 | |||
116 | 46 | $this->socket->on('connection', [$this, 'onConnection']); |
|
117 | 46 | } |
|
118 | |||
119 | /** |
||
120 | * Shutdown Node and the underlying socket |
||
121 | */ |
||
122 | 1 | public function shutdown() |
|
123 | { |
||
124 | 1 | $this->socket->shutdown(); |
|
125 | 1 | } |
|
126 | |||
127 | /** |
||
128 | * @param Connection $conn |
||
129 | */ |
||
130 | 3 | public function onConnection(Connection $conn) |
|
131 | { |
||
132 | 3 | $this->connections->attach(new ConnectionContext($conn, $this)); |
|
133 | 3 | } |
|
134 | |||
135 | /** |
||
136 | * Detach connection from connection collection |
||
137 | * |
||
138 | * @param ConnectionContext $connection |
||
139 | */ |
||
140 | 2 | public function onClose(ConnectionContext $connection) |
|
141 | { |
||
142 | 2 | $this->connections->detach($connection); |
|
143 | 2 | } |
|
144 | |||
145 | /** |
||
146 | * Attach a plugin |
||
147 | * |
||
148 | * @param PluginInterface $plugin |
||
149 | */ |
||
150 | 3 | public function addPlugin(PluginInterface $plugin) |
|
151 | { |
||
152 | 3 | $plugin->setNode($this); |
|
153 | |||
154 | 3 | $this->plugins->attach($plugin); |
|
155 | 3 | } |
|
156 | |||
157 | /** |
||
158 | * Returns the plugins collection |
||
159 | * |
||
160 | * @return \SplObjectStorage |
||
161 | */ |
||
162 | 3 | public function getPlugins(): \SplObjectStorage |
|
166 | |||
167 | /** |
||
168 | * Return the connection collection |
||
169 | * |
||
170 | * @return \SplObjectStorage |
||
171 | */ |
||
172 | 4 | public function getConnections(): \SplObjectStorage |
|
176 | |||
177 | /** |
||
178 | * return the react |
||
179 | * |
||
180 | * @return LoopInterface |
||
181 | */ |
||
182 | 4 | public function getLoop(): LoopInterface |
|
186 | |||
187 | /** |
||
188 | * @return Configuration |
||
189 | */ |
||
190 | 5 | public function getConfiguration(): Configuration |
|
194 | |||
195 | /** |
||
196 | * @return CommandsCollection |
||
197 | */ |
||
198 | 1 | public function getCommands() |
|
199 | { |
||
200 | 1 | return $this->commands; |
|
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return LoggerInterface |
||
205 | */ |
||
206 | 5 | public function getLogger(): LoggerInterface |
|
210 | |||
211 | /** |
||
212 | * Get a plugin by slug or return false when none can be found |
||
213 | * |
||
214 | * @param string $slug |
||
215 | * @return bool|PluginInterface |
||
216 | */ |
||
217 | 1 | public function getPlugin($slug) |
|
229 | } |
||
230 |