-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Factory.php
142 lines (127 loc) · 4.2 KB
/
Factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* Magento validator config factory
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
namespace Magento\Framework\Validator;
use Magento\Framework\Cache\FrontendInterface;
class Factory
{
/** cache key */
const CACHE_KEY = __CLASS__;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $_objectManager;
/**
* Validator config files
*
* @var array|null
*/
protected $_configFiles = null;
/**
* @var bool
*/
private $isDefaultTranslatorInitialized = false;
/**
* @var \Magento\Framework\Module\Dir\Reader
*/
private $moduleReader;
/**
* @var FrontendInterface
*/
private $cache;
/**
* Initialize dependencies
*
* @param \Magento\Framework\ObjectManagerInterface $objectManager
* @param \Magento\Framework\Module\Dir\Reader $moduleReader
* @param FrontendInterface $cache
*/
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager,
\Magento\Framework\Module\Dir\Reader $moduleReader,
FrontendInterface $cache
) {
$this->_objectManager = $objectManager;
$this->moduleReader = $moduleReader;
$this->cache = $cache;
}
/**
* Init cached list of validation files
*/
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(serialize($this->_configFiles), self::CACHE_KEY);
} else {
$this->_configFiles = unserialize($this->_configFiles);
}
}
}
/**
* Create and set default translator to \Magento\Framework\Validator\AbstractValidator.
*
* @return void
*/
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);
};
/** @var \Magento\Framework\Translate\Adapter $translator */
$translator = $this->_objectManager->create('Magento\Framework\Translate\Adapter');
$translator->setOptions(['translator' => $translatorCallback]);
\Magento\Framework\Validator\AbstractValidator::setDefaultTranslator($translator);
$this->isDefaultTranslatorInitialized = true;
}
}
/**
* Get validator config object.
*
* Will instantiate \Magento\Framework\Validator\Config
*
* @return \Magento\Framework\Validator\Config
*/
public function getValidatorConfig()
{
$this->_initializeConfigList();
$this->_initializeDefaultTranslator();
return $this->_objectManager->create('Magento\Framework\Validator\Config', ['configFiles' => $this->_configFiles]);
}
/**
* Create validator builder instance based on entity and group.
*
* @param string $entityName
* @param string $groupName
* @param array|null $builderConfig
* @return \Magento\Framework\Validator\Builder
*/
public function createValidatorBuilder($entityName, $groupName, array $builderConfig = null)
{
$this->_initializeDefaultTranslator();
return $this->getValidatorConfig()->createValidatorBuilder($entityName, $groupName, $builderConfig);
}
/**
* Create validator based on entity and group.
*
* @param string $entityName
* @param string $groupName
* @param array|null $builderConfig
* @return \Magento\Framework\Validator
*/
public function createValidator($entityName, $groupName, array $builderConfig = null)
{
$this->_initializeDefaultTranslator();
return $this->getValidatorConfig()->createValidator($entityName, $groupName, $builderConfig);
}
}