PublicStream.php 3.4 KB

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