Hoa central
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
Hoa\Socket\Socket Class Reference

Public Member Functions

 __construct ($uri)
 
 setURI ($uri)
 
 getAddress ()
 
 getAddressType ()
 
 hasPort ()
 
 getPort ()
 
 hasTransport ()
 
 getTransport ()
 
 __toString ()
 

Public Attributes

const ADDRESS_IPV6 = 0
 
const ADDRESS_IPV4 = 1
 
const ADDRESS_DOMAIN = 2
 
const ADDRESS_PATH = 3
 

Protected Member Functions

 setPort ($port)
 
 setTransport ($transport)
 

Protected Attributes

 $_address = null
 
 $_addressType = 0
 
 $_port = -1
 
 $_transport = null
 

Detailed Description

Class .

Socket analyzer.

Definition at line 49 of file Socket.php.

Constructor & Destructor Documentation

Hoa\Socket\Socket::__construct (   $uri)

Constructor.

Parameters
string$uriURI.
Returns
void

Definition at line 115 of file Socket.php.

116  {
117  $this->setURI($uri);
118 
119  return;
120  }

Here is the call graph for this function:

Member Function Documentation

Hoa\Socket\Socket::__toString ( )

Get a string that represents the socket address.

Returns
string

Definition at line 313 of file Socket.php.

314  {
315  $out = null;
316 
317  if (true === $this->hasTransport()) {
318  $out .= $this->getTransport() . '://';
319  }
320 
321  if (true === $this->hasPort()) {
322  if (self::ADDRESS_IPV6 === $this->getAddressType()) {
323  $out .= '[' . $this->getAddress() . ']';
324  } else {
325  $out .= $this->getAddress();
326  }
327 
328  return $out . ':' . $this->getPort();
329  }
330 
331  return $out . $this->getAddress();
332  }

Here is the call graph for this function:

Hoa\Socket\Socket::getAddress ( )

Get the address.

Returns
string

Definition at line 253 of file Socket.php.

254  {
255  return $this->_address;
256  }

Here is the caller graph for this function:

Hoa\Socket\Socket::getAddressType ( )

Get the address type.

Returns
int

Definition at line 263 of file Socket.php.

264  {
265  return $this->_addressType;
266  }

Here is the caller graph for this function:

Hoa\Socket\Socket::getPort ( )

Get the port.

Returns
int

Definition at line 283 of file Socket.php.

284  {
285  return $this->_port;
286  }

Here is the caller graph for this function:

Hoa\Socket\Socket::getTransport ( )

Get the transport.

Returns
string

Definition at line 303 of file Socket.php.

304  {
305  return $this->_transport;
306  }

Here is the caller graph for this function:

Hoa\Socket\Socket::hasPort ( )

Check if a port was declared.

Returns
string

Definition at line 273 of file Socket.php.

274  {
275  return -1 != $this->getPort();
276  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Socket\Socket::hasTransport ( )

Check if a transport was declared.

Returns
bool

Definition at line 293 of file Socket.php.

294  {
295  return null !== $this->getTransport();
296  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Socket\Socket::setPort (   $port)
protected

Set the port.

Parameters
int$portPort.
Returns
int
Exceptions

Definition at line 207 of file Socket.php.

208  {
209  if ($port < 0) {
210  throw new Exception(
211  'Port must be greater or equal than zero, given %d.',
212  2,
213  $port
214  );
215  }
216 
217  $old = $this->_port;
218  $this->_port = $port;
219 
220  return $old;
221  }

Here is the caller graph for this function:

Hoa\Socket\Socket::setTransport (   $transport)
protected

Set the transport.

Parameters
string$transportTransport (TCP, UDP etc.).
Returns
string
Exceptions

Definition at line 230 of file Socket.php.

231  {
232  $transport = strtolower($transport);
233 
234  if (false === Transport::exists($transport)) {
235  throw new Exception(
236  'Transport %s is not enabled on this machin.',
237  3,
238  $transport
239  );
240  }
241 
242  $old = $this->_transport;
243  $this->_transport = $transport;
244 
245  return $old;
246  }
static exists($transport)
Definition: Transport.php:71

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Socket\Socket::setURI (   $uri)

Set URI.

Parameters
string$uriURI.
Returns
string
Exceptions

Definition at line 129 of file Socket.php.

130  {
131  $m = preg_match(
132  '#(?<scheme>[^:]+)://' .
133  '(?:\[(?<ipv6_>[^\]]+)\]:(?<ipv6_port>\d+)$|' .
134  '(?<ipv4>(\*|\d+(?:\.\d+){3}))(?::(?<ipv4_port>\d+))?$|' .
135  '(?<domain>[^:]+)(?::(?<domain_port>\d+))?$|' .
136  '(?<ipv6>.+)$)#',
137  $uri,
138  $matches);
139 
140  if (0 === $m) {
141  throw new Exception(
142  'URI %s is not recognized (it is not an IPv6, IPv4 nor ' .
143  'domain name).',
144  0,
145  $uri
146  );
147  }
148 
149  $this->setTransport($matches['scheme']);
150 
151  if (isset($matches['ipv6_']) && !empty($matches['ipv6_'])) {
152  $this->_address = $matches['ipv6_'];
153  $this->_addressType = self::ADDRESS_IPV6;
154  $this->setPort($matches['ipv6_port']);
155  } elseif (isset($matches['ipv6']) && !empty($matches['ipv6'])) {
156  $this->_address = $matches['ipv6'];
157  $this->_addressType = self::ADDRESS_IPV6;
158  } elseif (isset($matches['ipv4']) && !empty($matches['ipv4'])) {
159  $this->_address = $matches['ipv4'];
160  $this->_addressType = self::ADDRESS_IPV4;
161 
162  if ('*' === $this->_address) {
163  $this->_address = '0.0.0.0';
164  }
165 
166  if (isset($matches['ipv4_port'])) {
167  $this->setPort($matches['ipv4_port']);
168  }
169  } elseif (isset($matches['domain'])) {
170  $this->_address = $matches['domain'];
171 
172  if (false !== strpos($this->_address, '/')) {
173  $this->_addressType = self::ADDRESS_PATH;
174  } else {
175  $this->_addressType = self::ADDRESS_DOMAIN;
176  }
177 
178  if (isset($matches['domain_port'])) {
179  $this->setPort($matches['domain_port']);
180  }
181  }
182 
183  if (self::ADDRESS_IPV6 == $this->_addressType &&
184  (
185  !defined('STREAM_PF_INET6') ||
186  (function_exists('socket_create') && !defined('AF_INET6'))
187  )
188  ) {
189  throw new Exception(
190  'IPv6 support has been disabled from PHP, we cannot use ' .
191  'the %s URI.',
192  1,
193  $uri
194  );
195  }
196 
197  return;
198  }
setPort($port)
Definition: Socket.php:207
setTransport($transport)
Definition: Socket.php:230

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

Hoa\Socket\Socket::$_address = null
protected

Definition at line 84 of file Socket.php.

Hoa\Socket\Socket::$_addressType = 0
protected

Definition at line 91 of file Socket.php.

Hoa\Socket\Socket::$_port = -1
protected

Definition at line 98 of file Socket.php.

Hoa\Socket\Socket::$_transport = null
protected

Definition at line 105 of file Socket.php.

const Hoa\Socket\Socket::ADDRESS_DOMAIN = 2

Address type: domain.

int

Definition at line 70 of file Socket.php.

const Hoa\Socket\Socket::ADDRESS_IPV4 = 1

Address type: IPv4.

int

Definition at line 63 of file Socket.php.

const Hoa\Socket\Socket::ADDRESS_IPV6 = 0

Address type: IPv6.

int

Definition at line 56 of file Socket.php.

const Hoa\Socket\Socket::ADDRESS_PATH = 3

Address type: path.

int

Definition at line 77 of file Socket.php.


The documentation for this class was generated from the following file: