Skip to content

Commit cfbbb62

Browse files
ENGCOM-4545: 21842: don't cache absolute file paths in validator factory #21856
- Merge Pull Request #21856 from david-fuehr/magento2:issue-21842-dont-cache-absolute-file-paths - Merged commits: 1. 1106542 2. eb989e2 3. 7d0d01e 4. e266d9a 5. 83b34b8 6. f3d4d96
2 parents 7beb40f + f3d4d96 commit cfbbb62

File tree

2 files changed

+31
-148
lines changed

2 files changed

+31
-148
lines changed

lib/internal/Magento/Framework/Validator/Factory.php

Lines changed: 30 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,33 @@
66

77
namespace Magento\Framework\Validator;
88

9+
use Magento\Framework\Module\Dir\Reader;
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Framework\Phrase;
12+
use Magento\Framework\Validator;
913
use Magento\Framework\Cache\FrontendInterface;
1014

15+
/**
16+
* Factory for \Magento\Framework\Validator and \Magento\Framework\Validator\Builder.
17+
*/
1118
class Factory
1219
{
13-
/** cache key */
20+
/**
21+
* cache key
22+
*
23+
* @deprecated
24+
*/
1425
const CACHE_KEY = __CLASS__;
1526

1627
/**
17-
* @var \Magento\Framework\ObjectManagerInterface
28+
* @var ObjectManagerInterface
1829
*/
1930
protected $_objectManager;
2031

2132
/**
2233
* Validator config files
2334
*
24-
* @var array|null
35+
* @var iterable|null
2536
*/
2637
protected $_configFiles = null;
2738

@@ -31,40 +42,24 @@ class Factory
3142
private $isDefaultTranslatorInitialized = false;
3243

3344
/**
34-
* @var \Magento\Framework\Module\Dir\Reader
45+
* @var Reader
3546
*/
3647
private $moduleReader;
3748

38-
/**
39-
* @var FrontendInterface
40-
*/
41-
private $cache;
42-
43-
/**
44-
* @var \Magento\Framework\Serialize\SerializerInterface
45-
*/
46-
private $serializer;
47-
48-
/**
49-
* @var \Magento\Framework\Config\FileIteratorFactory
50-
*/
51-
private $fileIteratorFactory;
52-
5349
/**
5450
* Initialize dependencies
5551
*
56-
* @param \Magento\Framework\ObjectManagerInterface $objectManager
57-
* @param \Magento\Framework\Module\Dir\Reader $moduleReader
58-
* @param FrontendInterface $cache
52+
* @param ObjectManagerInterface $objectManager
53+
* @param Reader $moduleReader
54+
* @param FrontendInterface $cache @deprecated
5955
*/
6056
public function __construct(
61-
\Magento\Framework\ObjectManagerInterface $objectManager,
62-
\Magento\Framework\Module\Dir\Reader $moduleReader,
57+
ObjectManagerInterface $objectManager,
58+
Reader $moduleReader,
6359
FrontendInterface $cache
6460
) {
6561
$this->_objectManager = $objectManager;
6662
$this->moduleReader = $moduleReader;
67-
$this->cache = $cache;
6863
}
6964

7065
/**
@@ -75,32 +70,23 @@ public function __construct(
7570
protected function _initializeConfigList()
7671
{
7772
if (!$this->_configFiles) {
78-
$this->_configFiles = $this->cache->load(self::CACHE_KEY);
79-
if (!$this->_configFiles) {
80-
$this->_configFiles = $this->moduleReader->getConfigurationFiles('validation.xml');
81-
$this->cache->save(
82-
$this->getSerializer()->serialize($this->_configFiles->toArray()),
83-
self::CACHE_KEY
84-
);
85-
} else {
86-
$filesArray = $this->getSerializer()->unserialize($this->_configFiles);
87-
$this->_configFiles = $this->getFileIteratorFactory()->create(array_keys($filesArray));
88-
}
73+
$this->_configFiles = $this->moduleReader->getConfigurationFiles('validation.xml');
8974
}
9075
}
9176

9277
/**
9378
* Create and set default translator to \Magento\Framework\Validator\AbstractValidator.
9479
*
9580
* @return void
81+
* @throws \Zend_Translate_Exception
9682
*/
9783
protected function _initializeDefaultTranslator()
9884
{
9985
if (!$this->isDefaultTranslatorInitialized) {
10086
// Pass translations to \Magento\Framework\TranslateInterface from validators
10187
$translatorCallback = function () {
10288
$argc = func_get_args();
103-
return (string)new \Magento\Framework\Phrase(array_shift($argc), $argc);
89+
return (string)new Phrase(array_shift($argc), $argc);
10490
};
10591
/** @var \Magento\Framework\Translate\Adapter $translator */
10692
$translator = $this->_objectManager->create(\Magento\Framework\Translate\Adapter::class);
@@ -115,14 +101,15 @@ protected function _initializeDefaultTranslator()
115101
*
116102
* Will instantiate \Magento\Framework\Validator\Config
117103
*
118-
* @return \Magento\Framework\Validator\Config
104+
* @return Config
105+
* @throws \Zend_Translate_Exception
119106
*/
120107
public function getValidatorConfig()
121108
{
122109
$this->_initializeConfigList();
123110
$this->_initializeDefaultTranslator();
124111
return $this->_objectManager->create(
125-
\Magento\Framework\Validator\Config::class,
112+
Config::class,
126113
['configFiles' => $this->_configFiles]
127114
);
128115
}
@@ -133,7 +120,8 @@ public function getValidatorConfig()
133120
* @param string $entityName
134121
* @param string $groupName
135122
* @param array|null $builderConfig
136-
* @return \Magento\Framework\Validator\Builder
123+
* @return Builder
124+
* @throws \Zend_Translate_Exception
137125
*/
138126
public function createValidatorBuilder($entityName, $groupName, array $builderConfig = null)
139127
{
@@ -147,43 +135,12 @@ public function createValidatorBuilder($entityName, $groupName, array $builderCo
147135
* @param string $entityName
148136
* @param string $groupName
149137
* @param array|null $builderConfig
150-
* @return \Magento\Framework\Validator
138+
* @return Validator
139+
* @throws \Zend_Translate_Exception
151140
*/
152141
public function createValidator($entityName, $groupName, array $builderConfig = null)
153142
{
154143
$this->_initializeDefaultTranslator();
155144
return $this->getValidatorConfig()->createValidator($entityName, $groupName, $builderConfig);
156145
}
157-
158-
/**
159-
* Get serializer
160-
*
161-
* @return \Magento\Framework\Serialize\SerializerInterface
162-
* @deprecated 100.2.0
163-
*/
164-
private function getSerializer()
165-
{
166-
if ($this->serializer === null) {
167-
$this->serializer = $this->_objectManager->get(
168-
\Magento\Framework\Serialize\SerializerInterface::class
169-
);
170-
}
171-
return $this->serializer;
172-
}
173-
174-
/**
175-
* Get file iterator factory
176-
*
177-
* @return \Magento\Framework\Config\FileIteratorFactory
178-
* @deprecated 100.2.0
179-
*/
180-
private function getFileIteratorFactory()
181-
{
182-
if ($this->fileIteratorFactory === null) {
183-
$this->fileIteratorFactory = $this->_objectManager->get(
184-
\Magento\Framework\Config\FileIteratorFactory::class
185-
);
186-
}
187-
return $this->fileIteratorFactory;
188-
}
189146
}

lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,6 @@ class FactoryTest extends \PHPUnit\Framework\TestCase
2525
*/
2626
private $validatorConfigMock;
2727

28-
/**
29-
* @var \Magento\Framework\Cache\FrontendInterface|\PHPUnit_Framework_MockObject_MockObject
30-
*/
31-
private $cacheMock;
32-
33-
/**
34-
* @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
35-
*/
36-
private $serializerMock;
37-
38-
/**
39-
* @var \Magento\Framework\Config\FileIteratorFactory|\PHPUnit_Framework_MockObject_MockObject
40-
*/
41-
private $fileIteratorFactoryMock;
42-
4328
/**
4429
* @var \Magento\Framework\Config\FileIterator|\PHPUnit_Framework_MockObject_MockObject
4530
*/
@@ -55,11 +40,6 @@ class FactoryTest extends \PHPUnit\Framework\TestCase
5540
*/
5641
private $factory;
5742

58-
/**
59-
* @var string
60-
*/
61-
private $jsonString = '["\/tmp\/moduleOne\/etc\/validation.xml"]';
62-
6343
/**
6444
* @var array
6545
*/
@@ -99,23 +79,9 @@ protected function setUp()
9979
\Magento\Framework\Validator\Factory::class,
10080
[
10181
'objectManager' => $this->objectManagerMock,
102-
'moduleReader' => $this->readerMock,
103-
'cache' => $this->cacheMock
82+
'moduleReader' => $this->readerMock
10483
]
10584
);
106-
107-
$this->serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
108-
$this->fileIteratorFactoryMock = $this->createMock(\Magento\Framework\Config\FileIteratorFactory::class);
109-
$objectManager->setBackwardCompatibleProperty(
110-
$this->factory,
111-
'serializer',
112-
$this->serializerMock
113-
);
114-
$objectManager->setBackwardCompatibleProperty(
115-
$this->factory,
116-
'fileIteratorFactory',
117-
$this->fileIteratorFactoryMock
118-
);
11985
}
12086

12187
/**
@@ -147,46 +113,6 @@ public function testGetValidatorConfig()
147113
);
148114
}
149115

150-
public function testGetValidatorConfigCacheNotExist()
151-
{
152-
$this->cacheMock->expects($this->once())
153-
->method('load')
154-
->willReturn(false);
155-
$this->readerMock->expects($this->once())
156-
->method('getConfigurationFiles')
157-
->willReturn($this->fileIteratorMock);
158-
$this->fileIteratorMock->method('toArray')
159-
->willReturn($this->data);
160-
$this->cacheMock->expects($this->once())
161-
->method('save')
162-
->with($this->jsonString);
163-
$this->serializerMock->expects($this->once())
164-
->method('serialize')
165-
->with($this->data)
166-
->willReturn($this->jsonString);
167-
$this->factory->getValidatorConfig();
168-
$this->factory->getValidatorConfig();
169-
}
170-
171-
public function testGetValidatorConfigCacheExist()
172-
{
173-
$this->cacheMock->expects($this->once())
174-
->method('load')
175-
->willReturn($this->jsonString);
176-
$this->readerMock->expects($this->never())
177-
->method('getConfigurationFiles');
178-
$this->cacheMock->expects($this->never())
179-
->method('save');
180-
$this->serializerMock->expects($this->once())
181-
->method('unserialize')
182-
->with($this->jsonString)
183-
->willReturn($this->data);
184-
$this->fileIteratorFactoryMock->method('create')
185-
->willReturn($this->fileIteratorMock);
186-
$this->factory->getValidatorConfig();
187-
$this->factory->getValidatorConfig();
188-
}
189-
190116
public function testCreateValidatorBuilder()
191117
{
192118
$this->readerMock->method('getConfigurationFiles')

0 commit comments

Comments
 (0)