StreamWrapperManagerInterface.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. /**
  163. * Returns the part of a URI after the schema.
  164. *
  165. * @param string $uri
  166. * A stream, referenced as "scheme://target" or "data:target".
  167. *
  168. * @return string|bool
  169. * A string containing the target (path), or FALSE if none.
  170. * For example, the URI "public://sample/test.txt" would return
  171. * "sample/test.txt".
  172. *
  173. * @see \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getScheme()
  174. */
  175. public static function getTarget($uri);
  176. /**
  177. * Normalizes a URI by making it syntactically correct.
  178. *
  179. * A stream is referenced as "scheme://target".
  180. *
  181. * The following actions are taken:
  182. * - Remove trailing slashes from target
  183. * - Trim erroneous leading slashes from target. e.g. ":///" becomes "://".
  184. *
  185. * @param string $uri
  186. * String reference containing the URI to normalize.
  187. *
  188. * @return string
  189. * The normalized URI.
  190. */
  191. public function normalizeUri($uri);
  192. /**
  193. * Returns the scheme of a URI (e.g. a stream).
  194. *
  195. * @param string $uri
  196. * A stream, referenced as "scheme://target" or "data:target".
  197. *
  198. * @return string|bool
  199. * A string containing the name of the scheme, or FALSE if none. For
  200. * example, the URI "public://example.txt" would return "public".
  201. *
  202. * @see \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getTarget()
  203. */
  204. public static function getScheme($uri);
  205. /**
  206. * Checks that the scheme of a stream URI is valid.
  207. *
  208. * Confirms that there is a registered stream handler for the provided scheme
  209. * and that it is callable. This is useful if you want to confirm a valid
  210. * scheme without creating a new instance of the registered handler.
  211. *
  212. * @param string $scheme
  213. * A URI scheme, a stream is referenced as "scheme://target".
  214. *
  215. * @return bool
  216. * Returns TRUE if the string is the name of a validated stream, or FALSE if
  217. * the scheme does not have a registered handler.
  218. */
  219. public function isValidScheme($scheme);
  220. /**
  221. * Determines whether the URI has a valid scheme for file API operations.
  222. *
  223. * There must be a scheme and it must be a Drupal-provided scheme like
  224. * 'public', 'private', 'temporary', or an extension provided with
  225. * hook_stream_wrappers().
  226. *
  227. * @param string $uri
  228. * The URI to be tested.
  229. *
  230. * @return bool
  231. * TRUE if the URI is valid.
  232. */
  233. public function isValidUri($uri);
  234. }