Context.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Stream;
  36. /**
  37. * Class \Hoa\Stream\Context.
  38. *
  39. * Make a multiton of stream contexts.
  40. *
  41. * @copyright Copyright © 2007-2017 Hoa community
  42. * @license New BSD License
  43. */
  44. class Context
  45. {
  46. /**
  47. * Context ID.
  48. *
  49. * @var string
  50. */
  51. protected $_id = null;
  52. /**
  53. * Multiton.
  54. *
  55. * @var array
  56. */
  57. protected static $_instances = [];
  58. /**
  59. * Construct a context.
  60. *
  61. */
  62. protected function __construct($id)
  63. {
  64. $this->_id = $id;
  65. $this->_context = stream_context_create();
  66. return;
  67. }
  68. /**
  69. * Multiton.
  70. *
  71. * @param string $id ID.
  72. * @return \Hoa\Stream\Context
  73. * @throws \Hoa\Stream\Exception
  74. */
  75. public static function getInstance($id)
  76. {
  77. if (empty($id)) {
  78. throw new Exception('Context ID must not be null.', 0);
  79. }
  80. if (false === static::contextExists($id)) {
  81. static::$_instances[$id] = new static($id);
  82. }
  83. return static::$_instances[$id];
  84. }
  85. /**
  86. * Get context ID.
  87. *
  88. * @return string
  89. */
  90. public function getId()
  91. {
  92. return $this->_id;
  93. }
  94. /**
  95. * Check if a context exists.
  96. *
  97. * @param string $id ID.
  98. * @return bool
  99. */
  100. public static function contextExists($id)
  101. {
  102. return array_key_exists($id, static::$_instances);
  103. }
  104. /**
  105. * Set options.
  106. * Please, see http://php.net/context.
  107. *
  108. * @param array $options Options.
  109. * @return bool
  110. */
  111. public function setOptions(array $options)
  112. {
  113. return stream_context_set_option($this->getContext(), $options);
  114. }
  115. /**
  116. * Set parameters.
  117. * Please, see http://php.net/context.params.
  118. *
  119. * @param array $parameters Parameters.
  120. * @return bool
  121. */
  122. public function setParameters(array $parameters)
  123. {
  124. return stream_context_set_params($this->getContext(), $parameters);
  125. }
  126. /**
  127. * Get options.
  128. *
  129. * @return array
  130. */
  131. public function getOptions()
  132. {
  133. return stream_context_get_options($this->getContext());
  134. }
  135. /**
  136. * Get parameters.
  137. * .
  138. * @return array
  139. */
  140. public function getParameters()
  141. {
  142. return stream_context_get_params($this->getContext());
  143. }
  144. /**
  145. * Get context as a resource.
  146. *
  147. * @return resource
  148. */
  149. public function getContext()
  150. {
  151. return $this->_context;
  152. }
  153. }