Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Storage Class Reference
Inheritance diagram for Storage:
AbstractHelper

Public Member Functions

 __construct (\Magento\Framework\App\Helper\Context $context, \Magento\Framework\Filesystem $filesystem, \Magento\Backend\Model\Session $session, \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory)
 
 convertPathToId ($path)
 
 convertIdToPath ($value)
 
 getShortFilename ($filename, $maxLength=20)
 
 getStorageRoot ()
 
 getStorageType ()
 
 getRelativeUrl ()
 
 getCurrentPath ()
 
 getThumbnailDirectory ($path)
 
 getThumbnailPath ($imageName)
 
 getRequestParams ()
 
 getAllowedExtensionsByType ()
 
 getStorageTypeName ()
 
 getSession ()
 
- Public Member Functions inherited from AbstractHelper
 __construct (Context $context)
 
 isModuleOutputEnabled ($moduleName=null)
 

Data Fields

const PARAM_NODE = 'node'
 
const PARAM_CONTENT_TYPE = 'content_type'
 
const PARAM_THEME_ID = 'theme_id'
 
const PARAM_FILENAME = 'filename'
 
const NODE_ROOT = 'root'
 
const IMAGES = 'Images'
 
const FONTS = 'Fonts'
 

Protected Member Functions

 _getTheme ()
 
- Protected Member Functions inherited from AbstractHelper
 _getRequest ()
 
 _getModuleName ()
 
 _getUrl ($route, $params=[])
 

Protected Attributes

 $_currentPath
 
 $_storageRoot
 
 $filesystem
 
 $_session
 
 $_themeFactory
 
 $mediaDirectoryWrite
 
- Protected Attributes inherited from AbstractHelper
 $_moduleName
 
 $_request
 
 $_moduleManager
 
 $_logger
 
 $_urlBuilder
 
 $_httpHeader
 
 $_eventManager
 
 $_remoteAddress
 
 $urlEncoder
 
 $urlDecoder
 
 $scopeConfig
 
 $_cacheConfig
 

Detailed Description

@api

Since
100.0.2

Definition at line 18 of file Storage.php.

Constructor & Destructor Documentation

◆ __construct()

Parameters
\Magento\Framework\App\Helper\Context$context
\Magento\Framework\Filesystem$filesystem
\Magento\Backend\Model\Session$session
\Magento\Framework\View\Design\Theme\FlyweightFactory$themeFactory

Definition at line 97 of file Storage.php.

102  {
103  parent::__construct($context);
104  $this->filesystem = $filesystem;
105  $this->_session = $session;
106  $this->_themeFactory = $themeFactory;
107  $this->mediaDirectoryWrite = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
108  $this->mediaDirectoryWrite->create($this->mediaDirectoryWrite->getRelativePath($this->getStorageRoot()));
109  }

Member Function Documentation

◆ _getTheme()

_getTheme ( )
protected

Get theme module for custom static files

Returns
\Magento\Theme\Model\Theme
Exceptions

Definition at line 172 of file Storage.php.

173  {
174  $themeId = $this->_getRequest()->getParam(self::PARAM_THEME_ID);
175  $theme = $this->_themeFactory->create($themeId);
176  if (!$themeId || !$theme) {
177  throw new \InvalidArgumentException('Theme was not found.');
178  }
179  return $theme;
180  }
$theme

◆ convertIdToPath()

convertIdToPath (   $value)

Convert id to path

Parameters
string$value
Returns
string

Definition at line 129 of file Storage.php.

130  {
131  $path = $this->urlDecoder->decode($value);
132  if (!strstr($path, $this->getStorageRoot())) {
133  $path = $this->getStorageRoot() . $path;
134  }
135  return $path;
136  }
$value
Definition: gender.phtml:16

◆ convertPathToId()

convertPathToId (   $path)

Convert path to id

Parameters
string$path
Returns
string

Definition at line 117 of file Storage.php.

118  {
119  $path = str_replace($this->getStorageRoot(), '', $path);
120  return $this->urlEncoder->encode($path);
121  }

◆ getAllowedExtensionsByType()

getAllowedExtensionsByType ( )

Get allowed extensions by type

Returns
string[]
Exceptions

Definition at line 291 of file Storage.php.

292  {
294  ? ['ttf', 'otf', 'eot', 'svg', 'woff']
295  : ['jpg', 'jpeg', 'gif', 'png', 'xbm', 'wbmp'];
296  }

◆ getCurrentPath()

getCurrentPath ( )

Get current path

Returns
string

Definition at line 224 of file Storage.php.

225  {
226  if (!$this->_currentPath) {
227  $currentPath = $this->getStorageRoot();
228  $path = $this->_getRequest()->getParam(self::PARAM_NODE);
229  if ($path && $path !== self::NODE_ROOT) {
230  $path = $this->convertIdToPath($path);
231 
232  if ($this->mediaDirectoryWrite->isDirectory($path) && 0 === strpos($path, $currentPath)) {
233  $currentPath = $this->mediaDirectoryWrite->getRelativePath($path);
234  }
235  }
236  $this->_currentPath = $currentPath;
237  }
238  return $this->_currentPath;
239  }

◆ getRelativeUrl()

getRelativeUrl ( )

Relative url to static content

Returns
string

Definition at line 206 of file Storage.php.

207  {
208  $pathPieces = ['..', $this->getStorageType()];
209  $node = $this->_getRequest()->getParam(self::PARAM_NODE);
210  if ($node !== self::NODE_ROOT) {
211  $node = $this->urlDecoder->decode($node);
212  $nodes = explode('/', trim($node, '/'));
213  $pathPieces = array_merge($pathPieces, $nodes);
214  }
215  $pathPieces[] = $this->urlDecoder->decode($this->_getRequest()->getParam(self::PARAM_FILENAME));
216  return implode('/', $pathPieces);
217  }

◆ getRequestParams()

getRequestParams ( )

Request params for selected theme

Returns
array

Definition at line 273 of file Storage.php.

274  {
275  $themeId = $this->_getRequest()->getParam(self::PARAM_THEME_ID);
276  $contentType = $this->_getRequest()->getParam(self::PARAM_CONTENT_TYPE);
277  $node = $this->_getRequest()->getParam(self::PARAM_NODE);
278  return [
279  self::PARAM_THEME_ID => $themeId,
280  self::PARAM_CONTENT_TYPE => $contentType,
281  self::PARAM_NODE => $node
282  ];
283  }

◆ getSession()

getSession ( )

Get session model

Returns
\Magento\Backend\Model\Session

Definition at line 316 of file Storage.php.

317  {
318  return $this->_session;
319  }

◆ getShortFilename()

getShortFilename (   $filename,
  $maxLength = 20 
)

Get short file name

Parameters
string$filename
int$maxLength
Returns
string

Definition at line 145 of file Storage.php.

146  {
147  return strlen($filename) <= $maxLength ? $filename : substr($filename, 0, $maxLength) . '...';
148  }

◆ getStorageRoot()

getStorageRoot ( )

Get storage root directory

Returns
string

Definition at line 155 of file Storage.php.

156  {
157  if (null === $this->_storageRoot) {
158  $this->_storageRoot = implode(
159  '/',
160  [$this->_getTheme()->getCustomization()->getCustomizationPath(), $this->getStorageType()]
161  );
162  }
163  return $this->_storageRoot;
164  }

◆ getStorageType()

getStorageType ( )

Get storage type

Returns
string
Exceptions

Definition at line 188 of file Storage.php.

189  {
190  $allowedTypes = [
193  ];
194  $type = (string)$this->_getRequest()->getParam(self::PARAM_CONTENT_TYPE);
195  if (!in_array($type, $allowedTypes)) {
196  throw new \Magento\Framework\Exception\LocalizedException(__('Invalid type'));
197  }
198  return $type;
199  }
__()
Definition: __.php:13
$type
Definition: item.phtml:13

◆ getStorageTypeName()

getStorageTypeName ( )

Get storage type name for display.

Returns
string
Exceptions

Definition at line 304 of file Storage.php.

◆ getThumbnailDirectory()

getThumbnailDirectory (   $path)

Get thumbnail directory for path

Parameters
string$path
Returns
string

Definition at line 247 of file Storage.php.

248  {
249  return pathinfo($path, PATHINFO_DIRNAME) . '/' . \Magento\Theme\Model\Wysiwyg\Storage::THUMBNAIL_DIRECTORY;
250  }

◆ getThumbnailPath()

getThumbnailPath (   $imageName)

Get thumbnail path in current directory by image name

Parameters
string$imageName
Returns
string
Exceptions

Definition at line 259 of file Storage.php.

260  {
261  $imagePath = $this->getCurrentPath() . '/' . $imageName;
262  if (!$this->mediaDirectoryWrite->isExist($imagePath) || 0 !== strpos($imagePath, $this->getStorageRoot())) {
263  throw new \InvalidArgumentException('The image not found.');
264  }
265  return $this->getThumbnailDirectory($imagePath) . '/' . pathinfo($imageName, PATHINFO_BASENAME);
266  }

Field Documentation

◆ $_currentPath

$_currentPath
protected

Definition at line 60 of file Storage.php.

◆ $_session

$_session
protected

Definition at line 79 of file Storage.php.

◆ $_storageRoot

$_storageRoot
protected

Definition at line 67 of file Storage.php.

◆ $_themeFactory

$_themeFactory
protected

Definition at line 84 of file Storage.php.

◆ $filesystem

$filesystem
protected

Definition at line 74 of file Storage.php.

◆ $mediaDirectoryWrite

$mediaDirectoryWrite
protected

Definition at line 89 of file Storage.php.

◆ FONTS

const FONTS = 'Fonts'

Display name for fonts storage type

Definition at line 53 of file Storage.php.

◆ IMAGES

const IMAGES = 'Images'

Display name for images storage type

Definition at line 48 of file Storage.php.

◆ NODE_ROOT

const NODE_ROOT = 'root'

Root node value identification number

Definition at line 43 of file Storage.php.

◆ PARAM_CONTENT_TYPE

const PARAM_CONTENT_TYPE = 'content_type'

Parameter name of content type

Definition at line 28 of file Storage.php.

◆ PARAM_FILENAME

const PARAM_FILENAME = 'filename'

Parameter name of filename

Definition at line 38 of file Storage.php.

◆ PARAM_NODE

const PARAM_NODE = 'node'

Parameter name of node

Definition at line 23 of file Storage.php.

◆ PARAM_THEME_ID

const PARAM_THEME_ID = 'theme_id'

Parameter name of theme identification number

Definition at line 33 of file Storage.php.


The documentation for this class was generated from the following file: