1 | <?php |
||
24 | class ClientHolder |
||
25 | { |
||
26 | protected $clients = []; |
||
27 | |||
28 | /** |
||
29 | * add |
||
30 | * |
||
31 | * Add a new client or replace existing one. |
||
32 | * |
||
33 | * @access public |
||
34 | * @param ClientInterface $client |
||
35 | * @return ClientHolder $this |
||
36 | */ |
||
37 | public function add(ClientInterface $client) |
||
38 | { |
||
39 | $this->clients[$client->getClientType()][$client->getClientIdentifier()] = $client; |
||
40 | |||
41 | return $this; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * has |
||
46 | * |
||
47 | * Tell if a client is in the pool or not. |
||
48 | * |
||
49 | * @access public |
||
50 | * @param string $type |
||
51 | * @param string $name |
||
52 | * @return bool |
||
53 | */ |
||
54 | public function has($type, $name) |
||
58 | |||
59 | /** |
||
60 | * get |
||
61 | * |
||
62 | * Return a client by its name or null if no client exist for that name. |
||
63 | * |
||
64 | * @access public |
||
65 | * @param string $type |
||
66 | * @param string $name |
||
67 | * @return ClientInterface |
||
68 | */ |
||
69 | public function get($type, $name) |
||
73 | |||
74 | /** |
||
75 | * getAllFor |
||
76 | * |
||
77 | * Return all clients for a given type. |
||
78 | * |
||
79 | * @access public |
||
80 | * @param string $type |
||
81 | * @return array |
||
82 | */ |
||
83 | public function getAllFor($type) |
||
84 | { |
||
85 | if (!isset($this->clients[$type])) { |
||
86 | return []; |
||
87 | } |
||
88 | |||
89 | return $this->clients[$type]; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * clear |
||
94 | * |
||
95 | * Call shutdown and remove a client from the pool. If the client does not |
||
96 | * exist, nothing is done. |
||
97 | * |
||
98 | * @access public |
||
99 | * @param string $type |
||
100 | * @param string $name |
||
101 | * @return ClientHolder $this |
||
102 | */ |
||
103 | public function clear($type, $name) |
||
104 | { |
||
105 | if (isset($this->clients[$type][$name])) { |
||
106 | $this->clients[$type][$name]->shutdown(); |
||
107 | unset($this->clients[$type][$name]); |
||
108 | } |
||
109 | |||
110 | return $this; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * shutdown |
||
115 | * |
||
116 | * Call shutdown for all registered clients and unset the clients so they |
||
117 | * can be cleaned by GC. It would have been better by far to use a |
||
118 | * RecursiveArrayIterator to do this but it is not possible in PHP using |
||
119 | * built'in iterators hence the double foreach recursion. |
||
120 | * see http://fr2.php.net/manual/en/class.recursivearrayiterator.php#106519 |
||
121 | * |
||
122 | * @access public |
||
123 | * @return array exceptions caught during the shutdown |
||
124 | */ |
||
125 | public function shutdown() |
||
143 | } |
||
144 |