Version 1
[yaffs-website] / web / core / lib / Drupal / Core / StreamWrapper / PublicStream.php
1 <?php
2
3 namespace Drupal\Core\StreamWrapper;
4
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  * Defines a Drupal public (public://) stream wrapper class.
12  *
13  * Provides support for storing publicly accessible files with the Drupal file
14  * interface.
15  */
16 class PublicStream extends LocalStream {
17
18   /**
19    * {@inheritdoc}
20    */
21   public static function getType() {
22     return StreamWrapperInterface::LOCAL_NORMAL;
23   }
24
25   /**
26    * {@inheritdoc}
27    */
28   public function getName() {
29     return t('Public files');
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public function getDescription() {
36     return t('Public local files served by the webserver.');
37   }
38
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     return static::baseUrl() . '/' . UrlHelper::encodePath($path);
52   }
53
54   /**
55    * Finds and returns the base URL for public://.
56    *
57    * Defaults to the current site's base URL plus directory path.
58    *
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.
62    *
63    * @return string
64    *   The external base URL for public://
65    */
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;
70     }
71     else {
72       return $GLOBALS['base_url'] . '/' . static::basePath();
73     }
74   }
75
76   /**
77    * Returns the base path for public://.
78    *
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.
82    *
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.
86    *
87    * The site path is injectable from the site.path service:
88    * @code
89    * $base_path = PublicStream::basePath(\Drupal::service('site.path'));
90    * @endcode
91    *
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
97    *   the site path.
98    *
99    * @return string
100    *   The base path for public:// typically sites/default/files.
101    */
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');
108       }
109       else {
110         // If there is no kernel available yet, we call the static
111         // findSitePath().
112         $site_path = DrupalKernel::findSitePath(Request::createFromGlobals());
113       }
114     }
115     return Settings::get('file_public_path', $site_path . '/files');
116   }
117
118 }