Wrapper.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\Wrapper;
  36. use Hoa\Consistency;
  37. /**
  38. * Class \Hoa\Stream\Wrapper.
  39. *
  40. * Manipulate wrappers.
  41. *
  42. * @copyright Copyright © 2007-2017 Hoa community
  43. * @license New BSD License
  44. */
  45. class Wrapper
  46. {
  47. /**
  48. * Register a wrapper.
  49. *
  50. * @param string $protocol The wrapper name to be registered.
  51. * @param string $className Class name which implements the protocol.
  52. * @param int $flags Should be set to `STREAM_IS_URL` if
  53. * `$protocol` is a URL protocol. Default is 0,
  54. * local stream.
  55. * @return bool
  56. * @throws \Hoa\Stream\Wrapper\Exception
  57. */
  58. public static function register($protocol, $className, $flags = 0)
  59. {
  60. if (true === self::isRegistered($protocol)) {
  61. throw new Exception(
  62. 'The protocol %s is already registered.',
  63. 0,
  64. $protocol
  65. );
  66. }
  67. if (false === class_exists($className)) {
  68. throw new Exception(
  69. 'Cannot use the %s class for the implementation of ' .
  70. 'the %s protocol because it is not found.',
  71. 1,
  72. [$className, $protocol]
  73. );
  74. }
  75. return stream_wrapper_register($protocol, $className, $flags);
  76. }
  77. /**
  78. * Unregister a wrapper.
  79. *
  80. * @param string $protocol The wrapper name to be unregistered.
  81. * @return bool
  82. */
  83. public static function unregister($protocol)
  84. {
  85. // Silent errors if `$protocol` does not exist. This function already
  86. // returns `false` in this case, which is the strict expected
  87. // behaviour.
  88. return @stream_wrapper_unregister($protocol);
  89. }
  90. /**
  91. * Restore a previously unregistered build-in wrapper.
  92. *
  93. * @param string $protocol The wrapper name to be restored.
  94. * @return bool
  95. */
  96. public static function restore($protocol)
  97. {
  98. // Silent errors if `$protocol` does not exist. This function already
  99. // returns `false` in this case, which is the strict expected
  100. // behaviour.
  101. return @stream_wrapper_restore($protocol);
  102. }
  103. /**
  104. * Check if a protocol is registered or not.
  105. *
  106. * @param string $protocol Protocol name.
  107. * @return bool
  108. */
  109. public static function isRegistered($protocol)
  110. {
  111. return in_array($protocol, self::getRegistered());
  112. }
  113. /**
  114. * Get all registered wrapper.
  115. *
  116. * @return array
  117. */
  118. public static function getRegistered()
  119. {
  120. return stream_get_wrappers();
  121. }
  122. }
  123. /**
  124. * Flex entity.
  125. */
  126. Consistency::flexEntity('Hoa\Stream\Wrapper\Wrapper');