-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
21842: don't cache absolute file paths in validator factory #21856
Changes from 6 commits
1106542
eb989e2
7d0d01e
e266d9a
83b34b8
f3d4d96
b77d8d0
875fa4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,22 +6,33 @@ | |
|
||
namespace Magento\Framework\Validator; | ||
|
||
use Magento\Framework\Module\Dir\Reader; | ||
use Magento\Framework\ObjectManagerInterface; | ||
use Magento\Framework\Phrase; | ||
use Magento\Framework\Validator; | ||
use Magento\Framework\Cache\FrontendInterface; | ||
|
||
/** | ||
* Factory for \Magento\Framework\Validator and \Magento\Framework\Validator\Builder. | ||
*/ | ||
class Factory | ||
{ | ||
/** cache key */ | ||
/** | ||
* cache key | ||
* | ||
* @deprecated | ||
*/ | ||
const CACHE_KEY = __CLASS__; | ||
|
||
/** | ||
* @var \Magento\Framework\ObjectManagerInterface | ||
* @var ObjectManagerInterface | ||
*/ | ||
protected $_objectManager; | ||
|
||
/** | ||
* Validator config files | ||
* | ||
* @var array|null | ||
* @var iterable|null | ||
*/ | ||
protected $_configFiles = null; | ||
|
||
|
@@ -31,40 +42,24 @@ class Factory | |
private $isDefaultTranslatorInitialized = false; | ||
|
||
/** | ||
* @var \Magento\Framework\Module\Dir\Reader | ||
* @var Reader | ||
*/ | ||
private $moduleReader; | ||
|
||
/** | ||
* @var FrontendInterface | ||
*/ | ||
private $cache; | ||
|
||
/** | ||
* @var \Magento\Framework\Serialize\SerializerInterface | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @var \Magento\Framework\Config\FileIteratorFactory | ||
*/ | ||
private $fileIteratorFactory; | ||
|
||
/** | ||
* Initialize dependencies | ||
* | ||
* @param \Magento\Framework\ObjectManagerInterface $objectManager | ||
* @param \Magento\Framework\Module\Dir\Reader $moduleReader | ||
* @param FrontendInterface $cache | ||
* @param ObjectManagerInterface $objectManager | ||
* @param Reader $moduleReader | ||
* @param FrontendInterface $cache @deprecated | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\ObjectManagerInterface $objectManager, | ||
\Magento\Framework\Module\Dir\Reader $moduleReader, | ||
ObjectManagerInterface $objectManager, | ||
Reader $moduleReader, | ||
FrontendInterface $cache | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, leave FrontendInterface as constructor parameter and mark it as deprecated, otherwise it's backward incompatible change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your feedback. I addressed this in my latest commit. |
||
) { | ||
$this->_objectManager = $objectManager; | ||
$this->moduleReader = $moduleReader; | ||
$this->cache = $cache; | ||
} | ||
|
||
/** | ||
|
@@ -75,32 +70,23 @@ public function __construct( | |
protected function _initializeConfigList() | ||
{ | ||
if (!$this->_configFiles) { | ||
$this->_configFiles = $this->cache->load(self::CACHE_KEY); | ||
if (!$this->_configFiles) { | ||
$this->_configFiles = $this->moduleReader->getConfigurationFiles('validation.xml'); | ||
$this->cache->save( | ||
$this->getSerializer()->serialize($this->_configFiles->toArray()), | ||
self::CACHE_KEY | ||
); | ||
} else { | ||
$filesArray = $this->getSerializer()->unserialize($this->_configFiles); | ||
$this->_configFiles = $this->getFileIteratorFactory()->create(array_keys($filesArray)); | ||
} | ||
$this->_configFiles = $this->moduleReader->getConfigurationFiles('validation.xml'); | ||
} | ||
} | ||
|
||
/** | ||
* Create and set default translator to \Magento\Framework\Validator\AbstractValidator. | ||
* | ||
* @return void | ||
* @throws \Zend_Translate_Exception | ||
*/ | ||
protected function _initializeDefaultTranslator() | ||
{ | ||
if (!$this->isDefaultTranslatorInitialized) { | ||
// Pass translations to \Magento\Framework\TranslateInterface from validators | ||
$translatorCallback = function () { | ||
$argc = func_get_args(); | ||
return (string)new \Magento\Framework\Phrase(array_shift($argc), $argc); | ||
return (string)new Phrase(array_shift($argc), $argc); | ||
}; | ||
/** @var \Magento\Framework\Translate\Adapter $translator */ | ||
$translator = $this->_objectManager->create(\Magento\Framework\Translate\Adapter::class); | ||
|
@@ -115,14 +101,15 @@ protected function _initializeDefaultTranslator() | |
* | ||
* Will instantiate \Magento\Framework\Validator\Config | ||
* | ||
* @return \Magento\Framework\Validator\Config | ||
* @return Config | ||
* @throws \Zend_Translate_Exception | ||
*/ | ||
public function getValidatorConfig() | ||
{ | ||
$this->_initializeConfigList(); | ||
$this->_initializeDefaultTranslator(); | ||
return $this->_objectManager->create( | ||
\Magento\Framework\Validator\Config::class, | ||
Config::class, | ||
['configFiles' => $this->_configFiles] | ||
); | ||
} | ||
|
@@ -133,7 +120,8 @@ public function getValidatorConfig() | |
* @param string $entityName | ||
* @param string $groupName | ||
* @param array|null $builderConfig | ||
* @return \Magento\Framework\Validator\Builder | ||
* @return Builder | ||
* @throws \Zend_Translate_Exception | ||
*/ | ||
public function createValidatorBuilder($entityName, $groupName, array $builderConfig = null) | ||
{ | ||
|
@@ -147,43 +135,12 @@ public function createValidatorBuilder($entityName, $groupName, array $builderCo | |
* @param string $entityName | ||
* @param string $groupName | ||
* @param array|null $builderConfig | ||
* @return \Magento\Framework\Validator | ||
* @return Validator | ||
* @throws \Zend_Translate_Exception | ||
*/ | ||
public function createValidator($entityName, $groupName, array $builderConfig = null) | ||
{ | ||
$this->_initializeDefaultTranslator(); | ||
return $this->getValidatorConfig()->createValidator($entityName, $groupName, $builderConfig); | ||
} | ||
|
||
/** | ||
* Get serializer | ||
* | ||
* @return \Magento\Framework\Serialize\SerializerInterface | ||
* @deprecated 100.2.0 | ||
*/ | ||
private function getSerializer() | ||
{ | ||
if ($this->serializer === null) { | ||
$this->serializer = $this->_objectManager->get( | ||
\Magento\Framework\Serialize\SerializerInterface::class | ||
); | ||
} | ||
return $this->serializer; | ||
} | ||
|
||
/** | ||
* Get file iterator factory | ||
* | ||
* @return \Magento\Framework\Config\FileIteratorFactory | ||
* @deprecated 100.2.0 | ||
*/ | ||
private function getFileIteratorFactory() | ||
{ | ||
if ($this->fileIteratorFactory === null) { | ||
$this->fileIteratorFactory = $this->_objectManager->get( | ||
\Magento\Framework\Config\FileIteratorFactory::class | ||
); | ||
} | ||
return $this->fileIteratorFactory; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, we are not allowed to remove the constants according to our Backward Compatible Development Guide. Just mark it as deprecated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your feedback. I addressed this in my latest commit.