PublicStream.php 3.4 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\StreamWrapper;

5
use Drupal\Component\Utility\UrlHelper;
6
use Drupal\Core\DrupalKernel;
7
use Drupal\Core\Site\Settings;
8
use Symfony\Component\HttpFoundation\Request;
9

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

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  /**
   * {@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.');
  }

39
  /**
40
   * {@inheritdoc}
41 42
   */
  public function getDirectoryPath() {
43
    return static::basePath();
44 45 46
  }

  /**
47
   * {@inheritdoc}
48
   */
49
  public function getExternalUrl() {
50
    $path = str_replace('\\', '/', $this->getTarget());
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    return static::baseUrl() . '/' . UrlHelper::encodePath($path);
  }

  /**
   * Finds and returns the base URL for public://.
   *
   * Defaults to the current site's base URL plus directory path.
   *
   * Note that this static method is used by \Drupal\system\Form\FileSystemForm
   * so you should alter that form or substitute a different form if you change
   * the class providing the stream_wrapper.public service.
   *
   * @return string
   *   The external base URL for public://
   */
  public static function baseUrl() {
    $settings_base_url = Settings::get('file_public_base_url', '');
    if ($settings_base_url) {
69
      return (string) $settings_base_url;
70 71 72 73
    }
    else {
      return $GLOBALS['base_url'] . '/' . static::basePath();
    }
74
  }
75 76 77 78

  /**
   * Returns the base path for public://.
   *
79 80 81 82
   * If we have a setting for the public:// scheme's path, we use that.
   * Otherwise we build a reasonable default based on the site.path service if
   * it's available, or a default behavior based on the request.
   *
83 84 85 86
   * Note that this static method is used by \Drupal\system\Form\FileSystemForm
   * so you should alter that form or substitute a different form if you change
   * the class providing the stream_wrapper.public service.
   *
87 88 89 90 91 92 93 94 95 96 97 98
   * The site path is injectable from the site.path service:
   * @code
   * $base_path = PublicStream::basePath(\Drupal::service('site.path'));
   * @endcode
   *
   * @param \SplString $site_path
   *   (optional) The site.path service parameter, which is typically the path
   *   to sites/ in a Drupal installation. This allows you to inject the site
   *   path using services from the caller. If omitted, this method will use the
   *   global service container or the kernel's default behavior to determine
   *   the site path.
   *
99 100 101
   * @return string
   *   The base path for public:// typically sites/default/files.
   */
102 103 104 105 106 107 108 109 110 111 112 113 114 115
  public static function basePath(\SplString $site_path = NULL) {
    if ($site_path === NULL) {
      // Find the site path. Kernel service is not always available at this
      // point, but is preferred, when available.
      if (\Drupal::hasService('kernel')) {
        $site_path = \Drupal::service('site.path');
      }
      else {
        // If there is no kernel available yet, we call the static
        // findSitePath().
        $site_path = DrupalKernel::findSitePath(Request::createFromGlobals());
      }
    }
    return Settings::get('file_public_path', $site_path . '/files');
116 117
  }

118
}