diff --git a/core/lib/Drupal/Core/StreamWrapper/PublicStream.php b/core/lib/Drupal/Core/StreamWrapper/PublicStream.php index c18e880..ea7d1e6 100644 --- a/core/lib/Drupal/Core/StreamWrapper/PublicStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/PublicStream.php @@ -59,17 +59,37 @@ public function getExternalUrl() { /** * Returns the base path for public://. * + * 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. + * + * 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. + * * @return string * The base path for public:// typically sites/default/files. */ - public static function basePath() { - // 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 { - $site_path = DrupalKernel::findSitePath(Request::createFromGlobals()); + 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'); }