Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
File.php
Go to the documentation of this file.
1 <?php
8 
17 class File implements MergeableInterface
18 {
22  protected $filePath;
23 
27  protected $module;
28 
32  protected $contentType;
33 
37  protected $context;
38 
42  protected $source;
43 
47  private $resolvedFile;
48 
52  private $minification;
53 
57  private $sourceContentType;
58 
67  public function __construct(
70  $filePath,
71  $module,
73  Minification $minification
74  ) {
75  $this->source = $source;
76  $this->context = $context;
77  $this->filePath = $filePath;
78  $this->module = $module;
79  $this->contentType = $contentType;
80  $this->minification = $minification;
81  }
82 
86  public function getUrl()
87  {
88  return $this->context->getBaseUrl() . $this->getPath();
89  }
90 
94  public function getSourceUrl()
95  {
96  return $this->context->getBaseUrl() . $this->getRelativeSourceFilePath();
97  }
98 
102  public function getContentType()
103  {
104  return $this->contentType;
105  }
106 
110  public function getPath()
111  {
112  $result = '';
113  $result = $this->join($result, $this->context->getPath());
114  $result = $this->join($result, $this->module);
115  $result = $this->join($result, $this->filePath);
116  $result = $this->minification->addMinifiedSign($result);
117  return $result;
118  }
119 
123  public function getRelativeSourceFilePath()
124  {
126  $sourcePath = $this->source->findSource($this);
127  if ($sourcePath) {
128  $origExt = pathinfo($path, PATHINFO_EXTENSION);
129  $ext = pathinfo($sourcePath, PATHINFO_EXTENSION);
130  $path = str_replace('.' . $origExt, '.' . $ext, $this->filePath);
131  }
132  $result = '';
133  $result = $this->join($result, $this->context->getPath());
134  $result = $this->join($result, $this->module);
135  $result = $this->join($result, $path);
136  return $result;
137  }
138 
146  private function join($path, $item)
147  {
148  return trim($path . ($item ? '/' . $item : ''), '/');
149  }
150 
155  public function getSourceFile()
156  {
157  if (null === $this->resolvedFile) {
158  $this->resolvedFile = $this->source->getFile($this);
159  if (false === $this->resolvedFile) {
160  throw new File\NotFoundException("Unable to resolve the source file for '{$this->getPath()}'");
161  }
162  }
163  return $this->resolvedFile;
164  }
165 
172  public function getSourceContentType()
173  {
174  if ($this->sourceContentType === null) {
175  $this->sourceContentType = $this->source->getSourceContentType($this);
176  }
177  return $this->sourceContentType;
178  }
179 
183  public function getContent()
184  {
185  $content = $this->source->getContent($this);
186  if (false === $content) {
187  throw new File\NotFoundException("Unable to get content for '{$this->getPath()}'");
188  }
189  return $content;
190  }
191 
195  public function getFilePath()
196  {
197  return $this->filePath;
198  }
199 
204  public function getContext()
205  {
206  return $this->context;
207  }
208 
212  public function getModule()
213  {
214  return $this->module;
215  }
216 }
__construct(Source $source, ContextInterface $context, $filePath, $module, $contentType, Minification $minification)
Definition: File.php:67