Context.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /**
  10. * A socket adapter that uses PHP stream contexts.
  11. */
  12. class Context extends \lithium\net\Socket {
  13. /**
  14. * Connection timeout value.
  15. *
  16. * @var integer
  17. */
  18. protected $_timeout = 30;
  19. /**
  20. * Content of the stream
  21. *
  22. * @var string
  23. */
  24. protected $_content = null;
  25. /**
  26. * Constructor
  27. *
  28. * @param array $config
  29. */
  30. public function __construct(array $config = array()) {
  31. $defaults = array('mode' => 'r', 'message' => null);
  32. parent::__construct($config + $defaults);
  33. $this->timeout($this->_config['timeout']);
  34. }
  35. /**
  36. * Opens the socket and sets its timeout value.
  37. *
  38. * @param array $options Update the config settings.
  39. * @return mixed Returns `false` if the socket configuration does not contain the
  40. * `'scheme'` or `'host'` settings, or if configuration fails, otherwise returns a
  41. * resource stream.
  42. */
  43. public function open(array $options = array()) {
  44. parent::open($options);
  45. $config = $this->_config;
  46. if (!$config['scheme'] || !$config['host']) {
  47. return false;
  48. }
  49. $url = "{$config['scheme']}://{$config['host']}:{$config['port']}";
  50. $context = array($config['scheme'] => array('timeout' => $this->_timeout));
  51. if (is_object($config['message'])) {
  52. $url = $config['message']->to('url');
  53. $context = $config['message']->to('context', array('timeout' => $this->_timeout));
  54. }
  55. $this->_resource = fopen($url, $config['mode'], false, stream_context_create($context));
  56. return $this->_resource;
  57. }
  58. /**
  59. * Closes the socket connection.
  60. *
  61. * @return boolean Success.
  62. */
  63. public function close() {
  64. if (!is_resource($this->_resource)) {
  65. return true;
  66. }
  67. fclose($this->_resource);
  68. if (is_resource($this->_resource)) {
  69. $this->close();
  70. }
  71. return true;
  72. }
  73. /**
  74. * End of file test for this socket connection. Does not apply to this implementation.
  75. *
  76. * @return boolean Success.
  77. */
  78. public function eof() {
  79. if (!is_resource($this->_resource)) {
  80. return true;
  81. }
  82. return feof($this->_resource);
  83. }
  84. /**
  85. * Reads from the socket. Does not apply to this implementation.
  86. *
  87. * @return void
  88. */
  89. public function read() {
  90. if (!is_resource($this->_resource)) {
  91. return false;
  92. }
  93. $meta = stream_get_meta_data($this->_resource);
  94. if (isset($meta['wrapper_data'])) {
  95. $headers = join("\r\n", $meta['wrapper_data']) . "\r\n\r\n";
  96. } else {
  97. $headers = null;
  98. }
  99. return $headers . stream_get_contents($this->_resource);
  100. }
  101. /**
  102. * Writes to the socket.
  103. *
  104. * @param string $data Data to write.
  105. * @return boolean Success
  106. */
  107. public function write($data = null) {
  108. if (!is_resource($this->_resource)) {
  109. return false;
  110. }
  111. if (!is_object($data)) {
  112. $data = $this->_instance($this->_classes['request'], (array) $data + $this->_config);
  113. }
  114. return stream_context_set_option(
  115. $this->_resource, $data->to('context', array('timeout' => $this->_timeout))
  116. );
  117. }
  118. /**
  119. * Sets the timeout on the socket *connection*.
  120. *
  121. * @param integer $time Seconds after the connection times out.
  122. * @return booelan `true` if timeout has been set, `false` otherwise.
  123. */
  124. public function timeout($time = null) {
  125. if ($time !== null) {
  126. $this->_timeout = $time;
  127. }
  128. return $this->_timeout;
  129. }
  130. /**
  131. * Sets the encoding of the socket connection. Does not apply to this implementation.
  132. *
  133. * @param string $charset The character set to use.
  134. * @return boolean `true` if encoding has been set, `false` otherwise.
  135. */
  136. public function encoding($charset = null) {
  137. return false;
  138. }
  139. }
  140. ?>