Chris@0: wrappers[$filter])) { Chris@0: return $this->wrappers[$filter]; Chris@0: } Chris@0: elseif (isset($this->wrappers[StreamWrapperInterface::ALL])) { Chris@0: $this->wrappers[$filter] = []; Chris@0: foreach ($this->wrappers[StreamWrapperInterface::ALL] as $scheme => $info) { Chris@0: // Bit-wise filter. Chris@0: if (($info['type'] & $filter) == $filter) { Chris@0: $this->wrappers[$filter][$scheme] = $info; Chris@0: } Chris@0: } Chris@0: return $this->wrappers[$filter]; Chris@0: } Chris@0: else { Chris@0: return []; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getNames($filter = StreamWrapperInterface::ALL) { Chris@0: $names = []; Chris@0: foreach (array_keys($this->getWrappers($filter)) as $scheme) { Chris@0: $names[$scheme] = $this->getViaScheme($scheme)->getName(); Chris@0: } Chris@0: Chris@0: return $names; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getDescriptions($filter = StreamWrapperInterface::ALL) { Chris@0: $descriptions = []; Chris@0: foreach (array_keys($this->getWrappers($filter)) as $scheme) { Chris@0: $descriptions[$scheme] = $this->getViaScheme($scheme)->getDescription(); Chris@0: } Chris@0: Chris@0: return $descriptions; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getViaScheme($scheme) { Chris@0: return $this->getWrapper($scheme, $scheme . '://'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getViaUri($uri) { Chris@0: $scheme = file_uri_scheme($uri); Chris@0: return $this->getWrapper($scheme, $uri); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getClass($scheme) { Chris@0: if (isset($this->info[$scheme])) { Chris@0: return $this->info[$scheme]['class']; Chris@0: } Chris@0: Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a stream wrapper instance. Chris@0: * Chris@0: * @param string $scheme Chris@0: * The scheme of the desired stream wrapper. Chris@0: * @param string $uri Chris@0: * The URI of the stream. Chris@0: * Chris@0: * @return \Drupal\Core\StreamWrapper\StreamWrapperInterface|bool Chris@0: * A stream wrapper object, or false if the scheme is not available. Chris@0: */ Chris@0: protected function getWrapper($scheme, $uri) { Chris@0: if (isset($this->info[$scheme]['service_id'])) { Chris@0: $instance = $this->container->get($this->info[$scheme]['service_id']); Chris@0: $instance->setUri($uri); Chris@0: return $instance; Chris@0: } Chris@0: Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a stream wrapper. Chris@0: * Chris@0: * Internal use only. Chris@0: * Chris@0: * @param string $service_id Chris@0: * The service id. Chris@0: * @param string $class Chris@0: * The stream wrapper class. Chris@0: * @param string $scheme Chris@0: * The scheme for which the wrapper should be registered. Chris@0: */ Chris@0: public function addStreamWrapper($service_id, $class, $scheme) { Chris@0: $this->info[$scheme] = [ Chris@0: 'class' => $class, Chris@0: 'type' => $class::getType(), Chris@0: 'service_id' => $service_id, Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Registers the tagged stream wrappers. Chris@0: * Chris@0: * Internal use only. Chris@0: */ Chris@0: public function register() { Chris@0: foreach ($this->info as $scheme => $info) { Chris@0: $this->registerWrapper($scheme, $info['class'], $info['type']); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Unregisters the tagged stream wrappers. Chris@0: * Chris@0: * Internal use only. Chris@0: */ Chris@0: public function unregister() { Chris@0: // Normally, there are definitely wrappers set for the ALL filter. However, Chris@0: // in some cases involving many container rebuilds (e.g. WebTestBase), Chris@0: // $this->wrappers may be empty although wrappers are still registered Chris@0: // globally. Thus an isset() check is needed before iterating. Chris@0: if (isset($this->wrappers[StreamWrapperInterface::ALL])) { Chris@0: foreach (array_keys($this->wrappers[StreamWrapperInterface::ALL]) as $scheme) { Chris@0: stream_wrapper_unregister($scheme); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function registerWrapper($scheme, $class, $type) { Chris@0: if (in_array($scheme, stream_get_wrappers(), TRUE)) { Chris@0: stream_wrapper_unregister($scheme); Chris@0: } Chris@0: Chris@0: if (($type & StreamWrapperInterface::LOCAL) == StreamWrapperInterface::LOCAL) { Chris@0: stream_wrapper_register($scheme, $class); Chris@0: } Chris@0: else { Chris@0: stream_wrapper_register($scheme, $class, STREAM_IS_URL); Chris@0: } Chris@0: Chris@0: // Pre-populate the static cache with the filters most typically used. Chris@0: $info = ['type' => $type, 'class' => $class]; Chris@0: $this->wrappers[StreamWrapperInterface::ALL][$scheme] = $info; Chris@0: Chris@0: if (($type & StreamWrapperInterface::WRITE_VISIBLE) == StreamWrapperInterface::WRITE_VISIBLE) { Chris@0: $this->wrappers[StreamWrapperInterface::WRITE_VISIBLE][$scheme] = $info; Chris@0: } Chris@0: } Chris@0: Chris@0: }