123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\Socket;
  36. /**
  37. * Class \Hoa\Socket\Transport.
  38. *
  39. * Transports manipulation. Can be used to register new transports. A URI is of
  40. * kind `scheme://uri`. A callable is associated to a `scheme` and represents a
  41. * factory building valid `Hoa\Socket\Socket` instances (so with `tcp://` or
  42. * `udp://` “native” schemes).
  43. *
  44. * @copyright Copyright © 2007-2017 Hoa community
  45. * @license New BSD License
  46. */
  47. class Transport
  48. {
  49. /**
  50. * Additionnal transports (scheme to callable).
  51. *
  52. * @var array
  53. */
  54. protected static $_transports = [];
  55. /**
  56. * Get all enabled transports.
  57. *
  58. * @return array
  59. */
  60. public static function get()
  61. {
  62. return array_merge(
  63. stream_get_transports(),
  64. array_keys(static::$_transports)
  65. );
  66. }
  67. /**
  68. * Check if a transport exists.
  69. *
  70. * @param string $transport Transport to check.
  71. * @return bool
  72. */
  73. public static function exists($transport)
  74. {
  75. return in_array(strtolower($transport), static::get());
  76. }
  77. /**
  78. * Register a new transport.
  79. * Note: It is possible to override a standard transport.
  80. *
  81. * @param string $transport Transport name.
  82. * @param callable $factory Associated factory to build a valid
  83. * `Hoa\Socket\Socket` object.
  84. * @return void
  85. */
  86. public static function register($transport, callable $factory)
  87. {
  88. static::$_transports[$transport] = $factory;
  89. return;
  90. }
  91. /**
  92. * Get the factory associated to a specific transport.
  93. *
  94. * @param string $transport Transport.
  95. * @return callable
  96. */
  97. public static function getFactory($transport)
  98. {
  99. if (false === static::exists($transport) ||
  100. !isset(static::$_transports[$transport])) {
  101. return function ($socketUri) {
  102. return new Socket($socketUri);
  103. };
  104. }
  105. return static::$_transports[$transport];
  106. }
  107. }