Skip to content

Commit

Permalink
Merge pull request #4672 from magento-tango/PR-2019-08-22
Browse files Browse the repository at this point in the history
[tango] Bugfixes
  • Loading branch information
dhorytskyi authored Aug 25, 2019
2 parents 099c639 + ebbbad4 commit 20db8f6
Show file tree
Hide file tree
Showing 11 changed files with 373 additions and 255 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@ public function beforeSave($object)

if ($this->fileResidesOutsideCategoryDir($value)) {
// use relative path for image attribute so we know it's outside of category dir when we fetch it
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$value[0]['url'] = parse_url($value[0]['url'], PHP_URL_PATH);
$value[0]['name'] = $value[0]['url'];
}

if ($imageName = $this->getUploadedImageName($value)) {
$imageName = $this->checkUniqueImageName($imageName);
if (!$this->fileResidesOutsideCategoryDir($value)) {
$imageName = $this->checkUniqueImageName($imageName);
}
$object->setData($this->additionalData . $attributeName, $value);
$object->setData($attributeName, $imageName);
} elseif (!is_string($value)) {
Expand Down Expand Up @@ -182,7 +186,7 @@ private function fileResidesOutsideCategoryDir($value)
return false;
}

return strpos($fileUrl, $baseMediaDir) === 0;
return strpos($fileUrl, $baseMediaDir) !== false;
}

/**
Expand Down
44 changes: 41 additions & 3 deletions app/code/Magento/Catalog/Model/Category/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Framework\Filesystem\Directory\ReadInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class FileInfo
Expand Down Expand Up @@ -48,16 +50,26 @@ class FileInfo
*/
private $pubDirectory;

/**
* Store manager
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;

/**
* @param Filesystem $filesystem
* @param Mime $mime
* @param StoreManagerInterface $storeManager
*/
public function __construct(
Filesystem $filesystem,
Mime $mime
Mime $mime,
StoreManagerInterface $storeManager
) {
$this->filesystem = $filesystem;
$this->mime = $mime;
$this->storeManager = $storeManager;
}

/**
Expand Down Expand Up @@ -152,7 +164,8 @@ public function isExist($fileName)
*/
private function getFilePath($fileName)
{
$filePath = ltrim($fileName, '/');
$filePath = $this->removeStorePath($fileName);
$filePath = ltrim($filePath, '/');

$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath($filePath);
$isFileNameBeginsWithMediaDirectoryPath = $this->isBeginsWithMediaDirectoryPath($fileName);
Expand All @@ -177,14 +190,39 @@ private function getFilePath($fileName)
*/
public function isBeginsWithMediaDirectoryPath($fileName)
{
$filePath = ltrim($fileName, '/');
$filePath = $this->removeStorePath($fileName);
$filePath = ltrim($filePath, '/');

$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath($filePath);
$isFileNameBeginsWithMediaDirectoryPath = strpos($filePath, (string) $mediaDirectoryRelativeSubpath) === 0;

return $isFileNameBeginsWithMediaDirectoryPath;
}

/**
* Clean store path in case if it's exists
*
* @param string $path
* @return string
*/
private function removeStorePath(string $path): string
{
$result = $path;
try {
$storeUrl = $this->storeManager->getStore()->getBaseUrl();
} catch (NoSuchEntityException $e) {
return $result;
}
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$path = parse_url($path, PHP_URL_PATH);
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$storePath = parse_url($storeUrl, PHP_URL_PATH);
$storePath = rtrim($storePath, '/');

$result = preg_replace('/^' . preg_quote($storePath, '/') . '/', '', $path);
return $result;
}

/**
* Get media directory subpath relative to base directory path
*
Expand Down
60 changes: 30 additions & 30 deletions app/code/Magento/Catalog/Model/Indexer/Product/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,56 @@
*/
namespace Magento\Catalog\Model\Indexer\Product;

use Magento\Catalog\Model\Category as CategoryModel;
use Magento\Catalog\Model\Indexer\Product\Price\Action\Full as FullAction;
use Magento\Catalog\Model\Indexer\Product\Price\Action\Row as RowAction;
use Magento\Catalog\Model\Indexer\Product\Price\Action\Rows as RowsAction;
use Magento\Catalog\Model\Product as ProductModel;
use Magento\Framework\Indexer\ActionInterface as IndexerActionInterface;
use Magento\Framework\Indexer\CacheContext;
use Magento\Framework\Mview\ActionInterface as MviewActionInterface;

class Price implements \Magento\Framework\Indexer\ActionInterface, \Magento\Framework\Mview\ActionInterface
/**
* Price indexer
*/
class Price implements IndexerActionInterface, MviewActionInterface
{
/**
* @var \Magento\Catalog\Model\Indexer\Product\Price\Action\Row
* @var RowAction
*/
protected $_productPriceIndexerRow;

/**
* @var \Magento\Catalog\Model\Indexer\Product\Price\Action\Rows
* @var RowsAction
*/
protected $_productPriceIndexerRows;

/**
* @var \Magento\Catalog\Model\Indexer\Product\Price\Action\Full
* @var FullAction
*/
protected $_productPriceIndexerFull;

/**
* @var \Magento\Framework\Indexer\CacheContext
* @var CacheContext
*/
private $cacheContext;

/**
* @param Price\Action\Row $productPriceIndexerRow
* @param Price\Action\Rows $productPriceIndexerRows
* @param Price\Action\Full $productPriceIndexerFull
* @param RowAction $productPriceIndexerRow
* @param RowsAction $productPriceIndexerRows
* @param FullAction $productPriceIndexerFull
* @param CacheContext $cacheContext
*/
public function __construct(
\Magento\Catalog\Model\Indexer\Product\Price\Action\Row $productPriceIndexerRow,
\Magento\Catalog\Model\Indexer\Product\Price\Action\Rows $productPriceIndexerRows,
\Magento\Catalog\Model\Indexer\Product\Price\Action\Full $productPriceIndexerFull
RowAction $productPriceIndexerRow,
RowsAction $productPriceIndexerRows,
FullAction $productPriceIndexerFull,
CacheContext $cacheContext
) {
$this->_productPriceIndexerRow = $productPriceIndexerRow;
$this->_productPriceIndexerRows = $productPriceIndexerRows;
$this->_productPriceIndexerFull = $productPriceIndexerFull;
$this->cacheContext = $cacheContext;
}

/**
Expand All @@ -53,7 +66,7 @@ public function __construct(
public function execute($ids)
{
$this->_productPriceIndexerRows->execute($ids);
$this->getCacheContext()->registerEntities(\Magento\Catalog\Model\Product::CACHE_TAG, $ids);
$this->cacheContext->registerEntities(ProductModel::CACHE_TAG, $ids);
}

/**
Expand All @@ -64,10 +77,10 @@ public function execute($ids)
public function executeFull()
{
$this->_productPriceIndexerFull->execute();
$this->getCacheContext()->registerTags(
$this->cacheContext->registerTags(
[
\Magento\Catalog\Model\Category::CACHE_TAG,
\Magento\Catalog\Model\Product::CACHE_TAG
CategoryModel::CACHE_TAG,
ProductModel::CACHE_TAG
]
);
}
Expand All @@ -81,6 +94,7 @@ public function executeFull()
public function executeList(array $ids)
{
$this->_productPriceIndexerRows->execute($ids);
$this->cacheContext->registerEntities(ProductModel::CACHE_TAG, $ids);
}

/**
Expand All @@ -92,20 +106,6 @@ public function executeList(array $ids)
public function executeRow($id)
{
$this->_productPriceIndexerRow->execute($id);
}

/**
* Get cache context
*
* @return \Magento\Framework\Indexer\CacheContext
* @deprecated 100.0.11
*/
protected function getCacheContext()
{
if (!($this->cacheContext instanceof CacheContext)) {
return \Magento\Framework\App\ObjectManager::getInstance()->get(CacheContext::class);
} else {
return $this->cacheContext;
}
$this->cacheContext->registerEntities(ProductModel::CACHE_TAG, [$id]);
}
}
Loading

0 comments on commit 20db8f6

Please sign in to comment.