ExtensionStreamBase.php in System stream wrapper 8
File
src/StreamWrapper/ExtensionStreamBase.php
View source
<?php
namespace Drupal\system_stream_wrapper\StreamWrapper;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
abstract class ExtensionStreamBase extends LocalReadOnlyStream {
use StringTranslationTrait;
protected $request;
public static function getType() {
return StreamWrapperInterface::LOCAL | StreamWrapperInterface::READ;
}
protected function getOwnerName() {
$uri_parts = explode('://', $this->uri, 2);
$length = strpos($uri_parts[1], '/');
return $length === FALSE ? $uri_parts[1] : substr($uri_parts[1], 0, $length);
}
protected function getTarget($uri = NULL) {
if ($target = strstr(parent::getTarget($uri), '/')) {
return trim($target, '/');
}
return '';
}
public function getExternalUrl() {
$dir = $this
->getDirectoryPath();
if (empty($dir)) {
throw new \InvalidArgumentException("Extension directory for {$this->uri} does not exist.");
}
$path = rtrim(base_path() . $dir . '/' . $this
->getTarget(), '/');
return $this
->getRequest()
->getUriForPath($path);
}
public function dirname($uri = NULL) {
if (!isset($uri)) {
$uri = $this->uri;
}
else {
$this->uri = $uri;
}
if (isset($uri)) {
$this->uri = $uri;
}
list($scheme) = explode('://', $uri, 2);
$dirname = dirname($this
->getTarget($uri));
$dirname = $dirname !== '.' ? rtrim("/{$dirname}", '/') : '';
return "{$scheme}://{$this->getOwnerName()}{$dirname}";
}
protected function getRequest() {
if (!isset($this->request)) {
$this->request = \Drupal::service('request_stack')
->getCurrentRequest();
}
return $this->request;
}
}