Skip to content

Commit

Permalink
feat: Add getViewFilePath
Browse files Browse the repository at this point in the history
  • Loading branch information
kpitn committed Jun 28, 2024
1 parent 0ad87db commit a4ba1b4
Showing 1 changed file with 53 additions and 10 deletions.
63 changes: 53 additions & 10 deletions ViewModel/ImageDisplay.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace Web200\ImageResize\ViewModel;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\State;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Framework\View\Asset\Repository;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Web200\ImageResize\Model\Display;

Expand All @@ -18,22 +23,20 @@
*/
class ImageDisplay implements ArgumentInterface
{
/**
* Display
*
* @var Display $display
*/
protected $display;

/**
* ImageDisplay constructor.
*
* @param Display $display
* @param Display $display
* @param Repository $assetRepository
* @param Filesystem $filesystem
* @param State $state
*/
public function __construct(
Display $display
protected Display $display,
protected Repository $assetRepository,
protected Filesystem $filesystem,
protected State $state
) {
$this->display = $display;
}

/**
Expand All @@ -45,4 +48,44 @@ public function getDisplay(): Display
{
return $this->display;
}

/**
* @param $imagePath
*
* @return string
* @throws LocalizedException
*/
public function getViewFilePath($imagePath): string
{
if ($this->isDeveloperMode()) {
return $this->getViewFileUrl($imagePath);
}
$asset = $this->assetRepository->createAsset($imagePath);
$directoryRead = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
return $directoryRead->getAbsolutePath($asset->getPath());
}

/**
* @return bool
*/
public function isDeveloperMode(): bool
{
return $this->state->getMode() == State::MODE_DEVELOPER;
}

/**
* Retrieve url of a view file
*
* @param string $fileId
*
* @return string
*/
public function getViewFileUrl(string $fileId)
{
try {
return $this->assetRepository->getUrl($fileId);
} catch (LocalizedException $e) {
return '';
}
}
}

0 comments on commit a4ba1b4

Please sign in to comment.