Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Media.php
Go to the documentation of this file.
1 <?php
8 
10 
15 {
19  protected $_date;
20 
24  protected $filesystem;
25 
33  public function __construct(
34  \Magento\Framework\App\Helper\Context $context,
35  \Magento\Framework\Stdlib\DateTime\DateTime $date,
36  \Magento\Framework\Filesystem $filesystem
37  ) {
38  parent::__construct($context);
39  $this->_date = $date;
40  $this->filesystem = $filesystem;
41  }
42 
58  {
59  $path = ltrim($path, '\\/');
60  $fullPath = $mediaDirectory . '/' . $path;
61 
62  $dir = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
63  $relativePath = $dir->getRelativePath($fullPath);
64  if (!$dir->isFile($relativePath)) {
65  throw new \Magento\Framework\Exception\LocalizedException(
66  __('The "%1" file doesn\'t exist. Verify the file and try again.', $fullPath)
67  );
68  }
69  if (!$dir->isReadable($relativePath)) {
70  throw new \Magento\Framework\Exception\LocalizedException(__('File %1 is not readable', $fullPath));
71  }
72 
73  $path = str_replace(['/', '\\'], '/', $path);
74  $directory = dirname($path);
75  if ($directory == '.') {
76  $directory = null;
77  }
78 
79  return [
80  'filename' => basename($path),
81  'content' => $dir->readFile($relativePath),
82  'update_time' => $this->_date->date(),
83  'directory' => $directory
84  ];
85  }
86 }
__()
Definition: __.php:13
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Framework\Stdlib\DateTime\DateTime $date, \Magento\Framework\Filesystem $filesystem)
Definition: Media.php:33
collectFileInfo($mediaDirectory, $path)
Definition: Media.php:57
$mediaDirectory
$relativePath
Definition: get.php:35