Stream.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\socket;
  9. use lithium\core\NetworkException;
  10. /**
  11. * A PHP stream-based socket adapter.
  12. *
  13. * This stream adapter provides the required method implementations of the abstract `Socket` class
  14. * for the `open()`, `close()`, `read()`, `write()`, `timeout()` `eof()` and `encoding()` methods.
  15. *
  16. * @link http://www.php.net/manual/en/book.stream.php PHP Manual: Streams
  17. * @see lithium\net\socket\Stream
  18. */
  19. class Stream extends \lithium\net\Socket {
  20. /**
  21. * Opens a socket and initializes the internal resource handle.
  22. *
  23. * @param array $options update the config settings
  24. * @return mixed Returns `false` if the socket configuration does not contain the
  25. * `'scheme'` or `'host'` settings, or if configuration fails, otherwise returns a
  26. * resource stream. Throws exception if there is a network error.
  27. */
  28. public function open(array $options = array()) {
  29. parent::open($options);
  30. $config = $this->_config;
  31. if (!$config['scheme'] || !$config['host']) {
  32. return false;
  33. }
  34. $scheme = ($config['scheme'] !== 'udp') ? 'tcp' : 'udp';
  35. $port = $config['port'] ?: 80;
  36. $host = "{$scheme}://{$config['host']}:{$port}";
  37. $flags = STREAM_CLIENT_CONNECT;
  38. if ($config['persistent']) {
  39. $flags = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
  40. }
  41. $this->_resource = stream_socket_client(
  42. $host, $errorCode, $errorMessage, $config['timeout'], $flags
  43. );
  44. if ($errorCode || $errorMessage) {
  45. throw new NetworkException($errorMessage);
  46. }
  47. $this->timeout($config['timeout']);
  48. if (!empty($config['encoding'])) {
  49. $this->encoding($config['encoding']);
  50. }
  51. return $this->_resource;
  52. }
  53. /**
  54. * Closes the stream
  55. *
  56. * @return boolean True on closed connection
  57. */
  58. public function close() {
  59. if (!is_resource($this->_resource)) {
  60. return true;
  61. }
  62. fclose($this->_resource);
  63. if (is_resource($this->_resource)) {
  64. $this->close();
  65. }
  66. return true;
  67. }
  68. /**
  69. * Determines if the socket resource is at EOF.
  70. *
  71. * @return boolean Returns `true` if resource pointer is at its EOF, `false` otherwise.
  72. */
  73. public function eof() {
  74. return is_resource($this->_resource) ? feof($this->_resource) : true;
  75. }
  76. /**
  77. * Reads data from the stream resource
  78. *
  79. * @param integer $length If specified, will read up to $length bytes from the stream.
  80. * If no value is specified, all remaining bytes in the buffer will be read.
  81. * @param integer $offset Seek to the specified byte offset before reading.
  82. * @return string Returns string read from stream resource on success, false otherwise.
  83. */
  84. public function read($length = null, $offset = null) {
  85. if (!is_resource($this->_resource)) {
  86. return false;
  87. }
  88. if (!$length) {
  89. return stream_get_contents($this->_resource);
  90. }
  91. return stream_get_contents($this->_resource, $length, $offset);
  92. }
  93. /**
  94. * writes data to the stream resource
  95. *
  96. * @param string $data The string to be written.
  97. * @return mixed False on error, number of bytes written otherwise.
  98. */
  99. public function write($data = null) {
  100. if (!is_resource($this->_resource)) {
  101. return false;
  102. }
  103. if (!is_object($data)) {
  104. $data = $this->_instance($this->_classes['request'], (array) $data + $this->_config);
  105. }
  106. return fwrite($this->_resource, (string) $data, strlen((string) $data));
  107. }
  108. /**
  109. * Set timeout period on a stream.
  110. *
  111. * @link http://www.php.net/manual/en/function.stream-set-timeout.php
  112. * PHP Manual: stream_set_timeout()
  113. * @param integer $time The timeout value in seconds.
  114. * @return void
  115. */
  116. public function timeout($time) {
  117. if (!is_resource($this->_resource)) {
  118. return false;
  119. }
  120. return stream_set_timeout($this->_resource, $time);
  121. }
  122. /**
  123. * Sets the character set for stream encoding
  124. *
  125. * Note: This function only exists in PHP 6. For PHP < 6, this method will return void.
  126. *
  127. * @link http://www.php.net/manual/en/function.stream-encoding.php stream_encoding()
  128. * @param string $charset
  129. * @return mixed Returns `null` if `stream_encoding()` function does not exist, boolean
  130. * result of `stream_encoding()` otherwise.
  131. */
  132. public function encoding($charset) {
  133. if (!function_exists('stream_encoding')) {
  134. return false;
  135. }
  136. return is_resource($this->_resource) ? stream_encoding($this->_resource, $charset) : false;
  137. }
  138. }
  139. ?>