PublicStream.php 1.35 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\Core\StreamWrapper\PublicStream.
 */

namespace Drupal\Core\StreamWrapper;

10
use Drupal\Component\Utility\UrlHelper;
11
use Drupal\Core\Site\Settings;
12

13 14 15 16 17 18 19 20
/**
 * Defines a Drupal public (public://) stream wrapper class.
 *
 * Provides support for storing publicly accessible files with the Drupal file
 * interface.
 */
class PublicStream extends LocalStream {

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  /**
   * {@inheritdoc}
   */
  public static function getType() {
    return StreamWrapperInterface::LOCAL_NORMAL;
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return t('Public files');
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return t('Public local files served by the webserver.');
  }

42
  /**
43
   * {@inheritdoc}
44 45
   */
  public function getDirectoryPath() {
46
    return static::basePath();
47 48 49
  }

  /**
50
   * {@inheritdoc}
51
   */
52
  public function getExternalUrl() {
53
    $path = str_replace('\\', '/', $this->getTarget());
54
    return $GLOBALS['base_url'] . '/' . self::getDirectoryPath() . '/' . UrlHelper::encodePath($path);
55
  }
56 57 58 59 60 61 62 63

  /**
   * Returns the base path for public://.
   *
   * @return string
   *   The base path for public:// typically sites/default/files.
   */
  public static function basePath() {
64
    $base_path = Settings::get('file_public_path', conf_path() . '/files');
65 66 67
    return $base_path;
  }

68
}