getViaScheme example

/** * Returns an array of route objects. * * @return \Symfony\Component\Routing\Route[] * An array of route objects. */
  public function routes(): array {
    $routes = [];
    // Generate assets. If clean URLs are disabled image derivatives will always     // be served through the routing system. If clean URLs are enabled and the     // image derivative already exists, PHP will be bypassed.     $directory_path = $this->streamWrapperManager->getViaScheme('assets')->getDirectoryPath();

    $routes['system.css_asset'] = new Route(
      '/' . $directory_path . '/css/{file_name}',
      [
        '_controller' => 'Drupal\system\Controller\CssAssetController::deliver',
      ],
      [
        '_access' => 'TRUE',
      ]
    );
    $routes['system.js_asset'] = new Route(
      


  /** * Tests the public file transfer system. */
  public function testPublicFileTransfer() {
    // Test generating a URL to a created file.     $file = $this->createFile();
    $url = $this->fileUrlGenerator->generateAbsoluteString($file->getFileUri());
    // URLs can't contain characters outside the ASCII set so $filename has to be     // encoded.     $filename = $GLOBALS['base_url'] . '/' . \Drupal::service('stream_wrapper_manager')->getViaScheme('public')->getDirectoryPath() . '/' . rawurlencode($file->getFilename());
    $this->assertEquals($filename$url, 'Correctly generated a URL for a created file.');
    $http_client = $this->getHttpClient();
    $response = $http_client->head($url);
    $this->assertEquals(200, $response->getStatusCode(), 'Confirmed that the generated URL is correct by downloading the created file.');

    // Test generating a URL to a shipped file (i.e. a file that is part of     // Drupal core, a module or a theme, for example a JavaScript file).     $filepath = 'core/assets/vendor/jquery/jquery.min.js';
    $url = $this->fileUrlGenerator->generateAbsoluteString($filepath);
    $this->assertEquals($GLOBALS['base_url'] . '/' . $filepath$url, 'Correctly generated a URL for a shipped file.');
    $response = $http_client->head($url);
    

  public function routes() {
    $routes = [];
    // Generate image derivatives of publicly available files. If clean URLs are     // disabled image derivatives will always be served through the menu system.     // If clean URLs are enabled and the image derivative already exists, PHP     // will be bypassed.     $directory_path = $this->streamWrapperManager->getViaScheme('public')->getDirectoryPath();

    $routes['image.style_public'] = new Route(
      '/' . $directory_path . '/styles/{image_style}/{scheme}',
      [
        '_controller' => 'Drupal\image\Controller\ImageStyleDownloadController::deliver',
      ],
      [
        '_access' => 'TRUE',
      ]
    );
    return $routes;
  }

  public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager) {
    $this->streamWrapperManager = $stream_wrapper_manager;
  }

  /** * {@inheritdoc} */
  public function processInbound($path, Request $request) {
    $directory_path = $this->streamWrapperManager->getViaScheme('public')->getDirectoryPath();
    if (strpos($path, '/' . $directory_path . '/styles/') === 0) {
      $path_prefix = '/' . $directory_path . '/styles/';
    }
    // Check if the string '/system/files/styles/' exists inside the path,     // that means we have a case of private file's image style.     elseif (str_contains($path, '/system/files/styles/')) {
      $path_prefix = '/system/files/styles/';
      $path = substr($pathstrpos($path$path_prefix)strlen($path));
    }
    else {
      return $path;
    }

  protected function isLocalUri($uri) {
    $scheme = StreamWrapperManager::getScheme($uri);

    // The vfs scheme is vfsStream, which is used in testing. vfsStream is a     // simulated file system that exists only in memory, but should be treated     // as a local resource.     if ($scheme == 'vfs') {
      $scheme = FALSE;
    }
    return $scheme === FALSE || $this->streamWrapperManager->getViaScheme($scheme) instanceof LocalStream;
  }

}
// Checks that the stream wrapper type is declared as local.     $this->assertSame(1, $type & StreamWrapperInterface::LOCAL);

    // Generate a test file     $filename = $this->randomMachineName();
    $site_path = $this->container->getParameter('site.path');
    $filepath = $site_path . '/files/' . $filename;
    file_put_contents($filepath$filename);

    // Generate a read-only stream wrapper instance     $uri = $this->scheme . '://' . $filename;
    \Drupal::service('stream_wrapper_manager')->getViaScheme($this->scheme);

    $file_system = \Drupal::service('file_system');
    // Attempt to open a file in read/write mode     $handle = @fopen($uri, 'r+');
    $this->assertFalse($handle, 'Unable to open a file for reading and writing with the read-only stream wrapper.');
    // Attempt to open a file in binary read mode     $handle = fopen($uri, 'rb');
    $this->assertNotFalse($handle, 'Able to open a file for reading in binary mode with the read-only stream wrapper.');
    $this->assertTrue(fclose($handle), 'Able to close file opened in binary mode using the read_only stream wrapper.');
    // Attempt to open a file in text read mode     $handle = fopen($uri, 'rt');
    
$scheme = $stream_wrapper_manager::getScheme($uri);

  // Only serve shipped files and public created files from the CDN.   if (!$scheme || in_array($scheme$schemes)) {
    // Shipped files.     if (!$scheme) {
      $path = $uri;
    }
    // Public created files.     else {
      $wrapper = $stream_wrapper_manager->getViaScheme($scheme);
      $path = $wrapper->getDirectoryPath() . '/' . $stream_wrapper_manager::getTarget($uri);
    }

    // Clean up Windows paths.     $path = str_replace('\\', '/', $path);

    // Serve files with one of the CDN extensions from CDN 1, all others from     // CDN 2.     $pathinfo = pathinfo($path);
    if (isset($pathinfo['extension']) && in_array($pathinfo['extension']$cdn_extensions)) {
      $uri = $cdn1 . '/' . $path;
    }

  public function testPublicManagedFileURL() {
    // Test generating a URL to a managed file.
    // Test alteration of file URLs to use a CDN.     \Drupal::state()->set('file_test.hook_file_url_alter', 'cdn');
    $uri = $this->createUri();
    $url = $this->fileUrlGenerator->generateAbsoluteString($uri);
    $public_directory_path = \Drupal::service('stream_wrapper_manager')
      ->getViaScheme('public')
      ->getDirectoryPath();
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $this->assertEquals(FILE_URL_TEST_CDN_2 . '/' . $public_directory_path . '/' . $file_system->basename($uri)$url, 'Correctly generated a CDN URL for a created file.');

    // Test alteration of file URLs to use root-relative URLs.     \Drupal::state()->set('file_test.hook_file_url_alter', 'root-relative');
    $uri = $this->createUri();
    $url = $this->fileUrlGenerator->generateAbsoluteString($uri);
    $this->assertEquals(base_path() . '/' . $public_directory_path . '/' . $file_system->basename($uri)$url, 'Correctly generated a root-relative URL for a created file.');

    
return realpath($uri);
  }

  /** * {@inheritdoc} */
  public function dirname($uri) {
    $scheme = StreamWrapperManager::getScheme($uri);

    if ($this->streamWrapperManager->isValidScheme($scheme)) {
      return $this->streamWrapperManager->getViaScheme($scheme)->dirname($uri);
    }
    else {
      return dirname($uri);
    }
  }

  /** * {@inheritdoc} */
  public function basename($uri$suffix = NULL) {
    $separators = '/';
    
public function testGetClassName() {
    // Check the dummy scheme.     $this->assertEquals($this->classname, \Drupal::service('stream_wrapper_manager')->getClass($this->scheme), 'Got correct class name for dummy scheme.');
    // Check core's scheme.     $this->assertEquals('Drupal\Core\StreamWrapper\PublicStream', \Drupal::service('stream_wrapper_manager')->getClass('public'), 'Got correct class name for public scheme.');
  }

  /** * Tests the getViaScheme() method. */
  public function testGetInstanceByScheme() {
    $instance = \Drupal::service('stream_wrapper_manager')->getViaScheme($this->scheme);
    $this->assertEquals($this->classname, get_class($instance), 'Got correct class type for dummy scheme.');

    $instance = \Drupal::service('stream_wrapper_manager')->getViaScheme('public');
    $this->assertEquals('Drupal\Core\StreamWrapper\PublicStream', get_class($instance), 'Got correct class type for public scheme.');
  }

  /** * Tests the getViaUri() and getViaScheme() methods and target functions. */
  public function testUriFunctions() {
    $config = $this->config('system.file');

    
else {
      return [];
    }
  }

  /** * {@inheritdoc} */
  public function getNames($filter = StreamWrapperInterface::ALL) {
    $names = [];
    foreach (array_keys($this->getWrappers($filter)) as $scheme) {
      $names[$scheme] = $this->getViaScheme($scheme)->getName();
    }

    return $names;
  }

  /** * {@inheritdoc} */
  public function getDescriptions($filter = StreamWrapperInterface::ALL) {
    $descriptions = [];
    foreach (array_keys($this->getWrappers($filter)) as $scheme) {
      

  public function makeBackup(FileTransfer $filetransfer$from$to) {
  }

  /** * Returns the full path to a directory where backups should be written. */
  public function getBackupDir() {
    return \Drupal::service('stream_wrapper_manager')->getViaScheme('temporary')->getDirectoryPath();
  }

  /** * Performs actions after new code is updated. */
  public function postUpdate() {
  }

  /** * Performs actions after installation. */
  
Home | Imprint | This part of the site doesn't use cookies.