StreamWrapperManagerInterface.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace Drupal\Core\StreamWrapper;
  3. /**
  4. * Provides a StreamWrapper manager.
  5. *
  6. * @see \Drupal\Core\StreamWrapper\StreamWrapperInterface
  7. */
  8. interface StreamWrapperManagerInterface {
  9. /**
  10. * Provides Drupal stream wrapper registry.
  11. *
  12. * A stream wrapper is an abstraction of a file system that allows Drupal to
  13. * use the same set of methods to access both local files and remote
  14. * resources.
  15. *
  16. * Provide a facility for managing and querying user-defined stream wrappers
  17. * in PHP. PHP's internal stream_get_wrappers() doesn't return the class
  18. * registered to handle a stream, which we need to be able to find the
  19. * handler
  20. * for class instantiation.
  21. *
  22. * If a module registers a scheme that is already registered with PHP, the
  23. * existing scheme will be unregistered and replaced with the specified
  24. * class.
  25. *
  26. * A stream is referenced as "scheme://target".
  27. *
  28. * The optional $filter parameter can be used to retrieve only the stream
  29. * wrappers that are appropriate for particular usage. For example, this
  30. * returns only stream wrappers that use local file storage:
  31. *
  32. * @code
  33. * $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
  34. * $local_stream_wrappers = $stream_wrapper_manager->getWrappers(StreamWrapperInterface::LOCAL);
  35. * @endcode
  36. *
  37. * The $filter parameter can only filter to types containing a particular
  38. * flag. In some cases, you may want to filter to types that do not contain a
  39. * particular flag. For example, you may want to retrieve all stream wrappers
  40. * that are not writable, or all stream wrappers that are not local. PHP's
  41. * array_diff_key() function can be used to help with this. For example, this
  42. * returns only stream wrappers that do not use local file storage:
  43. * @code
  44. * $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
  45. * $remote_stream_wrappers = array_diff_key(
  46. * $stream_wrapper_manager->getWrappers(StreamWrapperInterface::ALL),
  47. * $stream_wrapper_manager->getWrappers(StreamWrapperInterface::LOCAL)
  48. * );
  49. * @endcode
  50. *
  51. * @param int $filter
  52. * (Optional) Filters out all types except those with an on bit for each on
  53. * bit in $filter. For example, if $filter is
  54. * StreamWrapperInterface::WRITE_VISIBLE, which is equal to
  55. * (StreamWrapperInterface::READ | StreamWrapperInterface::WRITE |
  56. * StreamWrapperInterface::VISIBLE), then only stream wrappers with all
  57. * three of these bits set are returned. Defaults to
  58. * StreamWrapperInterface::ALL, which returns all registered stream
  59. * wrappers.
  60. *
  61. * @return array
  62. * An array keyed by scheme, with values containing an array of information
  63. * about the stream wrapper, as returned by hook_stream_wrappers(). If
  64. * $filter is omitted or set to StreamWrapperInterface::ALL, the entire
  65. * Drupal stream wrapper registry is returned. Otherwise only the stream
  66. * wrappers whose 'type' bitmask has an on bit for each bit specified in
  67. * $filter are returned.
  68. */
  69. public function getWrappers($filter = StreamWrapperInterface::ALL);
  70. /**
  71. * Returns registered stream wrapper names.
  72. *
  73. * @param int $filter
  74. * (Optional) Filters out all types except those with an on bit for each on
  75. * bit in $filter. For example, if $filter is
  76. * StreamWrapperInterface::WRITE_VISIBLE, which is equal to
  77. * (StreamWrapperInterface::READ | StreamWrapperInterface::WRITE |
  78. * StreamWrapperInterface::VISIBLE), then only stream wrappers with all
  79. * three of these bits set are returned. Defaults to
  80. * StreamWrapperInterface::ALL, which returns all registered stream
  81. * wrappers.
  82. *
  83. * @return array
  84. * Stream wrapper names, keyed by scheme.
  85. */
  86. public function getNames($filter = StreamWrapperInterface::ALL);
  87. /**
  88. * Returns registered stream wrapper descriptions.
  89. *
  90. * @param int $filter
  91. * (Optional) Filters out all types except those with an on bit for each on
  92. * bit in $filter. For example, if $filter is
  93. * StreamWrapperInterface::WRITE_VISIBLE, which is equal to
  94. * (StreamWrapperInterface::READ | StreamWrapperInterface::WRITE |
  95. * StreamWrapperInterface::VISIBLE), then only stream wrappers with all
  96. * three of these bits set are returned. Defaults to
  97. * StreamWrapperInterface::ALL, which returns all registered stream
  98. * wrappers.
  99. *
  100. * @return array
  101. * Stream wrapper descriptions, keyed by scheme.
  102. */
  103. public function getDescriptions($filter = StreamWrapperInterface::ALL);
  104. /**
  105. * Returns a reference to the stream wrapper class responsible for a scheme.
  106. *
  107. * This helper method returns a stream instance using a scheme. That is, the
  108. * passed string does not contain a "://". For example, "public" is a scheme
  109. * but "public://" is a URI (stream). This is because the later contains both
  110. * a scheme and target despite target being empty.
  111. *
  112. * Note: the instance URI will be initialized to "scheme://" so that you can
  113. * make the customary method calls as if you had retrieved an instance by URI.
  114. *
  115. * @param string $scheme
  116. * If the stream was "public://target", "public" would be the scheme.
  117. *
  118. * @return \Drupal\Core\StreamWrapper\StreamWrapperInterface|bool
  119. * Returns a new stream wrapper object appropriate for the given $scheme.
  120. * For example, for the public scheme a stream wrapper object
  121. * (Drupal\Core\StreamWrapper\PublicStream).
  122. * FALSE is returned if no registered handler could be found.
  123. */
  124. public function getViaScheme($scheme);
  125. /**
  126. * Returns a reference to the stream wrapper class responsible for a URI.
  127. *
  128. * The scheme determines the stream wrapper class that should be
  129. * used by consulting the stream wrapper registry.
  130. *
  131. * @param string $uri
  132. * A stream, referenced as "scheme://target".
  133. *
  134. * @return \Drupal\Core\StreamWrapper\StreamWrapperInterface|bool
  135. * Returns a new stream wrapper object appropriate for the given URI or
  136. * FALSE if no registered handler could be found. For example, a URI of
  137. * "private://example.txt" would return a new private stream wrapper object
  138. * (Drupal\Core\StreamWrapper\PrivateStream).
  139. */
  140. public function getViaUri($uri);
  141. /**
  142. * Returns the stream wrapper class name for a given scheme.
  143. *
  144. * @param string $scheme
  145. * Stream scheme.
  146. *
  147. * @return string|bool
  148. * Return string if a scheme has a registered handler, or FALSE.
  149. */
  150. public function getClass($scheme);
  151. /**
  152. * Registers stream wrapper with PHP.
  153. *
  154. * @param string $scheme
  155. * The scheme of the stream wrapper.
  156. * @param string $class
  157. * The class of the stream wrapper.
  158. * @param int $type
  159. * The type of the stream wrapper.
  160. */
  161. public function registerWrapper($scheme, $class, $type);
  162. }