annotate core/lib/Drupal/Core/StreamWrapper/PublicStream.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\StreamWrapper;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\UrlHelper;
Chris@0 6 use Drupal\Core\DrupalKernel;
Chris@0 7 use Drupal\Core\Site\Settings;
Chris@0 8 use Symfony\Component\HttpFoundation\Request;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Defines a Drupal public (public://) stream wrapper class.
Chris@0 12 *
Chris@0 13 * Provides support for storing publicly accessible files with the Drupal file
Chris@0 14 * interface.
Chris@0 15 */
Chris@0 16 class PublicStream extends LocalStream {
Chris@0 17
Chris@0 18 /**
Chris@0 19 * {@inheritdoc}
Chris@0 20 */
Chris@0 21 public static function getType() {
Chris@0 22 return StreamWrapperInterface::LOCAL_NORMAL;
Chris@0 23 }
Chris@0 24
Chris@0 25 /**
Chris@0 26 * {@inheritdoc}
Chris@0 27 */
Chris@0 28 public function getName() {
Chris@0 29 return t('Public files');
Chris@0 30 }
Chris@0 31
Chris@0 32 /**
Chris@0 33 * {@inheritdoc}
Chris@0 34 */
Chris@0 35 public function getDescription() {
Chris@0 36 return t('Public local files served by the webserver.');
Chris@0 37 }
Chris@0 38
Chris@0 39 /**
Chris@0 40 * {@inheritdoc}
Chris@0 41 */
Chris@0 42 public function getDirectoryPath() {
Chris@0 43 return static::basePath();
Chris@0 44 }
Chris@0 45
Chris@0 46 /**
Chris@0 47 * {@inheritdoc}
Chris@0 48 */
Chris@0 49 public function getExternalUrl() {
Chris@0 50 $path = str_replace('\\', '/', $this->getTarget());
Chris@0 51 return static::baseUrl() . '/' . UrlHelper::encodePath($path);
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * Finds and returns the base URL for public://.
Chris@0 56 *
Chris@0 57 * Defaults to the current site's base URL plus directory path.
Chris@0 58 *
Chris@0 59 * Note that this static method is used by \Drupal\system\Form\FileSystemForm
Chris@0 60 * so you should alter that form or substitute a different form if you change
Chris@0 61 * the class providing the stream_wrapper.public service.
Chris@0 62 *
Chris@0 63 * @return string
Chris@0 64 * The external base URL for public://
Chris@0 65 */
Chris@0 66 public static function baseUrl() {
Chris@0 67 $settings_base_url = Settings::get('file_public_base_url', '');
Chris@0 68 if ($settings_base_url) {
Chris@0 69 return (string) $settings_base_url;
Chris@0 70 }
Chris@0 71 else {
Chris@0 72 return $GLOBALS['base_url'] . '/' . static::basePath();
Chris@0 73 }
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * Returns the base path for public://.
Chris@0 78 *
Chris@0 79 * If we have a setting for the public:// scheme's path, we use that.
Chris@0 80 * Otherwise we build a reasonable default based on the site.path service if
Chris@0 81 * it's available, or a default behavior based on the request.
Chris@0 82 *
Chris@0 83 * Note that this static method is used by \Drupal\system\Form\FileSystemForm
Chris@0 84 * so you should alter that form or substitute a different form if you change
Chris@0 85 * the class providing the stream_wrapper.public service.
Chris@0 86 *
Chris@0 87 * The site path is injectable from the site.path service:
Chris@0 88 * @code
Chris@0 89 * $base_path = PublicStream::basePath(\Drupal::service('site.path'));
Chris@0 90 * @endcode
Chris@0 91 *
Chris@0 92 * @param \SplString $site_path
Chris@0 93 * (optional) The site.path service parameter, which is typically the path
Chris@0 94 * to sites/ in a Drupal installation. This allows you to inject the site
Chris@0 95 * path using services from the caller. If omitted, this method will use the
Chris@0 96 * global service container or the kernel's default behavior to determine
Chris@0 97 * the site path.
Chris@0 98 *
Chris@0 99 * @return string
Chris@0 100 * The base path for public:// typically sites/default/files.
Chris@0 101 */
Chris@0 102 public static function basePath(\SplString $site_path = NULL) {
Chris@0 103 if ($site_path === NULL) {
Chris@0 104 // Find the site path. Kernel service is not always available at this
Chris@0 105 // point, but is preferred, when available.
Chris@0 106 if (\Drupal::hasService('kernel')) {
Chris@0 107 $site_path = \Drupal::service('site.path');
Chris@0 108 }
Chris@0 109 else {
Chris@0 110 // If there is no kernel available yet, we call the static
Chris@0 111 // findSitePath().
Chris@0 112 $site_path = DrupalKernel::findSitePath(Request::createFromGlobals());
Chris@0 113 }
Chris@0 114 }
Chris@0 115 return Settings::get('file_public_path', $site_path . '/files');
Chris@0 116 }
Chris@0 117
Chris@0 118 }