You are here

trait FlysystemUrlTrait in Flysystem 8

Same name and namespace in other branches
  1. 3.x src/Plugin/FlysystemUrlTrait.php \Drupal\flysystem\Plugin\FlysystemUrlTrait
  2. 2.0.x src/Plugin/FlysystemUrlTrait.php \Drupal\flysystem\Plugin\FlysystemUrlTrait
  3. 3.0.x src/Plugin/FlysystemUrlTrait.php \Drupal\flysystem\Plugin\FlysystemUrlTrait

Helper trait for generating URLs from adapter plugins.

Hierarchy

3 files declare their use of FlysystemUrlTrait
FlysystemUrlTraitTest.php in tests/src/Unit/Plugin/FlysystemUrlTraitTest.php
Ftp.php in src/Flysystem/Ftp.php
Local.php in src/Flysystem/Local.php

File

src/Plugin/FlysystemUrlTrait.php, line 11

Namespace

Drupal\flysystem\Plugin
View source
trait FlysystemUrlTrait {

  /**
   * Returns a web accessible URL for the resource.
   *
   * This function should return a URL that can be embedded in a web page
   * and accessed from a browser. For example, the external URL of
   * "youtube://xIpLd0WQKCY" might be
   * "http://www.youtube.com/watch?v=xIpLd0WQKCY".
   *
   * @param string $uri
   *   The URI to provide a URL for.
   *
   * @return string
   *   Returns a string containing a web accessible URL for the resource.
   */
  public function getExternalUrl($uri) {
    $path = str_replace('\\', '/', $this
      ->getTarget($uri));
    $arguments = [
      'scheme' => $this
        ->getScheme($uri),
      'filepath' => $path,
    ];
    return Url::fromRoute('flysystem.serve', $arguments, [
      'absolute' => TRUE,
    ])
      ->toString();
  }

  /**
   * Returns the target file path of a URI.
   *
   * @param string $uri
   *   The URI.
   *
   * @return string
   *   The file path of the URI.
   */
  protected function getTarget($uri) {
    return Util::normalizePath(substr($uri, strpos($uri, '://') + 3));
  }

  /**
   * Returns the scheme from the internal URI.
   *
   * @param string $uri
   *   The URI.
   *
   * @return string
   *   The scheme.
   */
  protected function getScheme($uri) {
    return substr($uri, 0, strpos($uri, '://'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlysystemUrlTrait::getExternalUrl public function Returns a web accessible URL for the resource.
FlysystemUrlTrait::getScheme protected function Returns the scheme from the internal URI.
FlysystemUrlTrait::getTarget protected function Returns the target file path of a URI.