Socket.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\net;
  9. /**
  10. * Abstract class for connecting to sockets with various adapters.
  11. *
  12. * Currently, Curl, Stream and Context adapters are available.
  13. */
  14. abstract class Socket extends \lithium\core\Object {
  15. /**
  16. * The resource for the current connection.
  17. *
  18. * @var resource
  19. */
  20. protected $_resource = null;
  21. /**
  22. * The classes for the socket.
  23. *
  24. * @var array
  25. */
  26. protected $_classes = array(
  27. 'request' => 'lithium\net\Message',
  28. 'response' => 'lithium\net\Message'
  29. );
  30. /**
  31. * Auto config.
  32. *
  33. * @var array
  34. */
  35. protected $_autoConfig = array('classes' => 'merge');
  36. /**
  37. * Constructor.
  38. *
  39. * @param array $config Available configuration options are:
  40. * - `'persistent'`: Use a persistent connection (defaults to `false`).
  41. * - `'protocol'`: Transfer protocol to use (defaults to `'tcp'`).
  42. * - `'host'`: Host name or address (defaults to `'localhost'`).
  43. * - `'login'`: Username for a login (defaults to `'root'`).
  44. * - `'password'`: Password for a login (defaults to `''`).
  45. * - `'port'`: Host port (defaults to `80`).
  46. * - `'timeout'`: Seconds after opening the socket times out (defaults to `30`).
  47. */
  48. public function __construct(array $config = array()) {
  49. $defaults = array(
  50. 'persistent' => false,
  51. 'scheme' => 'tcp',
  52. 'host' => 'localhost',
  53. 'port' => 80,
  54. 'timeout' => 30
  55. );
  56. parent::__construct($config + $defaults);
  57. }
  58. /**
  59. * Opens the socket and sets `Socket::$_resource`.
  60. *
  61. * @param array $options Update the config settings.
  62. * @return mixed The open resource on success, `false` otherwise.
  63. */
  64. public function open(array $options = array()) {
  65. parent::__construct($options + $this->_config);
  66. return false;
  67. }
  68. /**
  69. * Closes the socket and unsets `Socket::$_resource`.
  70. *
  71. * @return boolean `true` on success, `false` otherwise.
  72. */
  73. abstract public function close();
  74. /**
  75. * Test for the end-of-file on the socket.
  76. *
  77. * @return boolean `true` if end has been reached, `false` otherwise.
  78. */
  79. abstract public function eof();
  80. /**
  81. * Reads from the socket.
  82. *
  83. * @return object `lithium\net\Message`
  84. */
  85. abstract public function read();
  86. /**
  87. * Writes data to the socket.
  88. *
  89. * @param mixed $data
  90. * @return boolean `true` if data has been successfully written, `false` otherwise.
  91. */
  92. abstract public function write($data);
  93. /**
  94. * Sets the timeout on the socket *connection*.
  95. *
  96. * @param integer $time Seconds after the connection times out.
  97. * @return Boolean `true` if timeout has been set, `false` otherwise.
  98. */
  99. abstract public function timeout($time);
  100. /**
  101. * Sets the encoding of the socket connection.
  102. *
  103. * @param string $charset The character set to use.
  104. * @return boolean `true` if encoding has been set, `false` otherwise.
  105. */
  106. abstract public function encoding($charset);
  107. /**
  108. * Sets the options to be used in subsequent requests.
  109. *
  110. * @param array $flags If $values is an array, $flags will be used as the
  111. * keys to an associative array of curl options. If $values is not set,
  112. * then $flags will be used as the associative array.
  113. * @param array $value If set, this array becomes the values for the
  114. * associative array of curl options.
  115. * @return void
  116. */
  117. public function set($flags, $value = null) {}
  118. /**
  119. * Aggregates read and write methods into a coherent request response
  120. *
  121. * @param mixed $message a request object based on `\lithium\net\Message`
  122. * @param array $options
  123. * - '`response`': a fully-namespaced string for the response object
  124. * @return object a response object based on `\lithium\net\Message`
  125. */
  126. public function send($message = null, array $options = array()) {
  127. $defaults = array('response' => $this->_classes['response']);
  128. $options += $defaults;
  129. if ($this->write($message)) {
  130. $config = array('message' => $this->read()) + $this->_config;
  131. return $this->_instance($options['response'], $config);
  132. }
  133. }
  134. /**
  135. * Destructor.
  136. *
  137. * @return void
  138. */
  139. public function __destruct() {
  140. $this->close();
  141. }
  142. /**
  143. * Returns the resource.
  144. *
  145. * @return resource
  146. */
  147. public function resource() {
  148. return $this->_resource;
  149. }
  150. }
  151. ?>