getDirectoryPath example


  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;
    }
/** * 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(
      

  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.');

    // Test alteration of file URLs to use a protocol-relative URLs.

  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 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. */
  

  protected function getLocalPath($uri = NULL) {
    if (!isset($uri)) {
      $uri = $this->uri;
    }
    $path = $this->getDirectoryPath() . '/' . $this->getTarget($uri);

    // In PHPUnit tests, the base path for local streams may be a virtual     // filesystem stream wrapper URI, in which case this local stream acts like     // a proxy. realpath() is not supported by vfsStream, because a virtual     // file system does not have a real filepath.     if (str_starts_with($path, 'vfs://')) {
      return $path;
    }

    $realpath = realpath($path);
    if (!$realpath) {
      


  /** * 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);
    

      catch (ServiceNotFoundException $e) {
      }
    }

    // If not using clean URLs, the image derivative callback is only available     // with the script path. If the file does not exist, use Url::fromUri() to     // ensure that it is included. Once the file exists it's fine to fall back     // to the actual file path, this avoids bootstrapping PHP once the files are     // built.     if ($clean_urls === FALSE && $stream_wrapper_manager::getScheme($uri) == 'public' && !file_exists($uri)) {
      $directory_path = $stream_wrapper_manager->getViaUri($uri)->getDirectoryPath();
      return Url::fromUri('base:' . $directory_path . '/' . $stream_wrapper_manager::getTarget($uri)['absolute' => TRUE, 'query' => $token_query])->toString();
    }

    /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
    $file_url_generator = \Drupal::service('file_url_generator');
    $file_url = $file_url_generator->generateAbsoluteString($uri);
    // Append the query string with the token, if necessary.     if ($token_query) {
      $file_url .= (str_contains($file_url, '?') ? '&' : '?') . UrlHelper::buildQuery($token_query);
    }

    
$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;
    }
    
$instance = $stream_wrapper_manager->getViaUri('public://foo');
    $this->assertEquals('Drupal\Core\StreamWrapper\PublicStream', get_class($instance), 'Got correct class type for public URI.');

    // Test file_uri_target().     $this->assertEquals('foo/bar.txt', $stream_wrapper_manager::getTarget('public://foo/bar.txt'), 'Got a valid stream target from public://foo/bar.txt.');
    $this->assertEquals('image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', $stream_wrapper_manager::getTarget('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='));
    $this->assertFalse($stream_wrapper_manager::getTarget('foo/bar.txt'), 'foo/bar.txt is not a valid stream.');
    $this->assertSame($stream_wrapper_manager::getTarget('public://'), '');
    $this->assertSame($stream_wrapper_manager::getTarget('data:'), '');

    // Test Drupal\Core\StreamWrapper\LocalStream::getDirectoryPath().     $this->assertEquals(PublicStream::basePath()$stream_wrapper_manager->getViaScheme('public')->getDirectoryPath(), 'Expected default directory path was returned.');
    $file_system = \Drupal::service('file_system');
    assert($file_system instanceof FileSystemInterface);
    $this->assertEquals($file_system->getTempDirectory()$stream_wrapper_manager->getViaScheme('temporary')->getDirectoryPath(), 'Expected temporary directory path was returned.');

    // Test FileUrlGeneratorInterface::generateString()     // TemporaryStream::getExternalUrl() uses Url::fromRoute(), which needs     // route information to work.     $file_url_generator = $this->container->get('file_url_generator');
    assert($file_url_generator instanceof FileUrlGeneratorInterface);
    $this->assertStringContainsString('system/temporary?file=test.txt', $file_url_generator->generateString('temporary://test.txt'), 'Temporary external URL correctly built.');
    $this->assertStringContainsString(Settings::get('file_public_path') . '/test.txt', $file_url_generator->generateString('public://test.txt'), 'Public external URL correctly built.');
    


  /** * {@inheritdoc} */
  public function tempnam($directory$prefix) {
    $scheme = StreamWrapperManager::getScheme($directory);

    if ($this->streamWrapperManager->isValidScheme($scheme)) {
      $wrapper = $this->streamWrapperManager->getViaScheme($scheme);

      if ($filename = tempnam($wrapper->getDirectoryPath()$prefix)) {
        return $scheme . '://' . static::basename($filename);
      }
      else {
        return FALSE;
      }
    }
    else {
      // Handle as a normal tempnam() call.       return tempnam($directory$prefix);
    }
  }

  
Home | Imprint | This part of the site doesn't use cookies.