3 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;
11 * Defines a Drupal public (public://) stream wrapper class.
13 * Provides support for storing publicly accessible files with the Drupal file
16 class PublicStream extends LocalStream {
21 public static function getType() {
22 return StreamWrapperInterface::LOCAL_NORMAL;
28 public function getName() {
29 return t('Public files');
35 public function getDescription() {
36 return t('Public local files served by the webserver.');
42 public function getDirectoryPath() {
43 return static::basePath();
49 public function getExternalUrl() {
50 $path = str_replace('\\', '/', $this->getTarget());
51 return static::baseUrl() . '/' . UrlHelper::encodePath($path);
55 * Finds and returns the base URL for public://.
57 * Defaults to the current site's base URL plus directory path.
59 * Note that this static method is used by \Drupal\system\Form\FileSystemForm
60 * so you should alter that form or substitute a different form if you change
61 * the class providing the stream_wrapper.public service.
64 * The external base URL for public://
66 public static function baseUrl() {
67 $settings_base_url = Settings::get('file_public_base_url', '');
68 if ($settings_base_url) {
69 return (string) $settings_base_url;
72 return $GLOBALS['base_url'] . '/' . static::basePath();
77 * Returns the base path for public://.
79 * If we have a setting for the public:// scheme's path, we use that.
80 * Otherwise we build a reasonable default based on the site.path service if
81 * it's available, or a default behavior based on the request.
83 * Note that this static method is used by \Drupal\system\Form\FileSystemForm
84 * so you should alter that form or substitute a different form if you change
85 * the class providing the stream_wrapper.public service.
87 * The site path is injectable from the site.path service:
89 * $base_path = PublicStream::basePath(\Drupal::service('site.path'));
92 * @param \SplString $site_path
93 * (optional) The site.path service parameter, which is typically the path
94 * to sites/ in a Drupal installation. This allows you to inject the site
95 * path using services from the caller. If omitted, this method will use the
96 * global service container or the kernel's default behavior to determine
100 * The base path for public:// typically sites/default/files.
102 public static function basePath(\SplString $site_path = NULL) {
103 if ($site_path === NULL) {
104 // Find the site path. Kernel service is not always available at this
105 // point, but is preferred, when available.
106 if (\Drupal::hasService('kernel')) {
107 $site_path = \Drupal::service('site.path');
110 // If there is no kernel available yet, we call the static
112 $site_path = DrupalKernel::findSitePath(Request::createFromGlobals());
115 return Settings::get('file_public_path', $site_path . '/files');