Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DeployStaticFile.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Deploy\Service;
7 
16 
21 {
25  private $filesystem;
26 
30  private $assetRepo;
31 
35  private $assetPublisher;
36 
40  private $fileNameResolver;
41 
45  private $minification;
46 
52  private $tmpDir;
53 
63  public function __construct(
64  Filesystem $filesystem,
65  Repository $assetRepo,
66  Publisher $assetPublisher,
67  FileNameResolver $fileNameResolver,
68  Minification $minification
69  ) {
70  $this->filesystem = $filesystem;
71  $this->assetRepo = $assetRepo;
72  $this->assetPublisher = $assetPublisher;
73  $this->fileNameResolver = $fileNameResolver;
74  $this->pubStaticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
75  $this->minification = $minification;
76  $this->tmpDir = $filesystem->getDirectoryWrite(DirectoryList::TMP_MATERIALIZATION_DIR);
77  }
78 
84  public function deployFile($fileName, array $params = [])
85  {
86  $params['publish'] = true;
87  $asset = $this->assetRepo->createAsset($this->resolveFile($fileName), $params);
88 
89  $this->assetPublisher->publish($asset);
90 
91  return $asset->getPath();
92  }
93 
98  public function deleteFile($path)
99  {
100  if ($this->pubStaticDir->isExist($path)) {
101  $absolutePath = $this->pubStaticDir->getAbsolutePath($path);
102  if (is_link($absolutePath)) {
103  $this->pubStaticDir->getDriver()->deleteFile($absolutePath);
104  } else {
105  if ($this->pubStaticDir->getDriver()->isFile($absolutePath)) {
106  $this->pubStaticDir->getDriver()->deleteFile($absolutePath);
107  } else {
108  $this->pubStaticDir->getDriver()->deleteDirectory($absolutePath);
109  }
110  }
111  }
112  }
113 
121  public function readFile($fileName, $filePath)
122  {
123  $fileName = $this->minification->addMinifiedSign($fileName);
124  $relativePath = $filePath . DIRECTORY_SEPARATOR . $this->resolveFile($fileName);
125  if ($this->pubStaticDir->isFile($relativePath)) {
126  return $this->pubStaticDir->readFile($relativePath);
127  } else {
128  return false;
129  }
130  }
131 
137  public function openFile($fileName, $filePath)
138  {
139  $relativePath = $filePath . DIRECTORY_SEPARATOR . $this->resolveFile($fileName);
140  return $this->pubStaticDir->openFile($relativePath, 'w+');
141  }
142 
151  public function writeFile($fileName, $filePath, $content)
152  {
153  $relativePath = $filePath . DIRECTORY_SEPARATOR . $this->resolveFile($fileName);
154  return $this->pubStaticDir->writeFile($relativePath, $content);
155  }
156 
165  public function copyFile($fileName, $sourcePath, $targetPath)
166  {
167  $fileName = $this->minification->addMinifiedSign($fileName);
168  return $this->pubStaticDir->copyFile(
169  $sourcePath . DIRECTORY_SEPARATOR . $this->resolveFile($fileName),
170  $targetPath . DIRECTORY_SEPARATOR . $this->resolveFile($fileName)
171  );
172  }
173 
181  public function readTmpFile($fileName, $filePath)
182  {
183  $relativePath = $filePath . DIRECTORY_SEPARATOR . $fileName;
184  return $this->tmpDir->isFile($relativePath) ? $this->tmpDir->readFile($relativePath) : false;
185  }
186 
195  public function writeTmpFile($fileName, $filePath, $content)
196  {
197  $relativePath = $filePath . DIRECTORY_SEPARATOR . $this->resolveFile($fileName);
198  return $this->tmpDir->writeFile($relativePath, $content);
199  }
200 
207  private function resolveFile($fileName)
208  {
209  $compiledFile = str_replace(
211  '/',
212  $this->fileNameResolver->resolve($fileName)
213  );
214 
215  return $compiledFile;
216  }
217 }
writeTmpFile($fileName, $filePath, $content)
copyFile($fileName, $sourcePath, $targetPath)
$fileName
Definition: translate.phtml:15
__construct(Filesystem $filesystem, Repository $assetRepo, Publisher $assetPublisher, FileNameResolver $fileNameResolver, Minification $minification)
$relativePath
Definition: get.php:35
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$filesystem
deployFile($fileName, array $params=[])
writeFile($fileName, $filePath, $content)