Factory.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Validator;
  7. use Magento\Framework\Cache\FrontendInterface;
  8. class Factory
  9. {
  10. /** cache key */
  11. const CACHE_KEY = __CLASS__;
  12. /**
  13. * @var \Magento\Framework\ObjectManagerInterface
  14. */
  15. protected $_objectManager;
  16. /**
  17. * Validator config files
  18. *
  19. * @var array|null
  20. */
  21. protected $_configFiles = null;
  22. /**
  23. * @var bool
  24. */
  25. private $isDefaultTranslatorInitialized = false;
  26. /**
  27. * @var \Magento\Framework\Module\Dir\Reader
  28. */
  29. private $moduleReader;
  30. /**
  31. * @var FrontendInterface
  32. */
  33. private $cache;
  34. /**
  35. * @var \Magento\Framework\Serialize\SerializerInterface
  36. */
  37. private $serializer;
  38. /**
  39. * @var \Magento\Framework\Config\FileIteratorFactory
  40. */
  41. private $fileIteratorFactory;
  42. /**
  43. * Initialize dependencies
  44. *
  45. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  46. * @param \Magento\Framework\Module\Dir\Reader $moduleReader
  47. * @param FrontendInterface $cache
  48. */
  49. public function __construct(
  50. \Magento\Framework\ObjectManagerInterface $objectManager,
  51. \Magento\Framework\Module\Dir\Reader $moduleReader,
  52. FrontendInterface $cache
  53. ) {
  54. $this->_objectManager = $objectManager;
  55. $this->moduleReader = $moduleReader;
  56. $this->cache = $cache;
  57. }
  58. /**
  59. * Init cached list of validation files
  60. *
  61. * @return void
  62. */
  63. protected function _initializeConfigList()
  64. {
  65. if (!$this->_configFiles) {
  66. $this->_configFiles = $this->cache->load(self::CACHE_KEY);
  67. if (!$this->_configFiles) {
  68. $this->_configFiles = $this->moduleReader->getConfigurationFiles('validation.xml');
  69. $this->cache->save(
  70. $this->getSerializer()->serialize($this->_configFiles->toArray()),
  71. self::CACHE_KEY
  72. );
  73. } else {
  74. $filesArray = $this->getSerializer()->unserialize($this->_configFiles);
  75. $this->_configFiles = $this->getFileIteratorFactory()->create(array_keys($filesArray));
  76. }
  77. }
  78. }
  79. /**
  80. * Create and set default translator to \Magento\Framework\Validator\AbstractValidator.
  81. *
  82. * @return void
  83. */
  84. protected function _initializeDefaultTranslator()
  85. {
  86. if (!$this->isDefaultTranslatorInitialized) {
  87. // Pass translations to \Magento\Framework\TranslateInterface from validators
  88. $translatorCallback = function () {
  89. $argc = func_get_args();
  90. return (string)new \Magento\Framework\Phrase(array_shift($argc), $argc);
  91. };
  92. /** @var \Magento\Framework\Translate\Adapter $translator */
  93. $translator = $this->_objectManager->create(\Magento\Framework\Translate\Adapter::class);
  94. $translator->setOptions(['translator' => $translatorCallback]);
  95. \Magento\Framework\Validator\AbstractValidator::setDefaultTranslator($translator);
  96. $this->isDefaultTranslatorInitialized = true;
  97. }
  98. }
  99. /**
  100. * Get validator config object.
  101. *
  102. * Will instantiate \Magento\Framework\Validator\Config
  103. *
  104. * @return \Magento\Framework\Validator\Config
  105. */
  106. public function getValidatorConfig()
  107. {
  108. $this->_initializeConfigList();
  109. $this->_initializeDefaultTranslator();
  110. return $this->_objectManager->create(
  111. \Magento\Framework\Validator\Config::class,
  112. ['configFiles' => $this->_configFiles]
  113. );
  114. }
  115. /**
  116. * Create validator builder instance based on entity and group.
  117. *
  118. * @param string $entityName
  119. * @param string $groupName
  120. * @param array|null $builderConfig
  121. * @return \Magento\Framework\Validator\Builder
  122. */
  123. public function createValidatorBuilder($entityName, $groupName, array $builderConfig = null)
  124. {
  125. $this->_initializeDefaultTranslator();
  126. return $this->getValidatorConfig()->createValidatorBuilder($entityName, $groupName, $builderConfig);
  127. }
  128. /**
  129. * Create validator based on entity and group.
  130. *
  131. * @param string $entityName
  132. * @param string $groupName
  133. * @param array|null $builderConfig
  134. * @return \Magento\Framework\Validator
  135. */
  136. public function createValidator($entityName, $groupName, array $builderConfig = null)
  137. {
  138. $this->_initializeDefaultTranslator();
  139. return $this->getValidatorConfig()->createValidator($entityName, $groupName, $builderConfig);
  140. }
  141. /**
  142. * Get serializer
  143. *
  144. * @return \Magento\Framework\Serialize\SerializerInterface
  145. * @deprecated 101.0.0
  146. */
  147. private function getSerializer()
  148. {
  149. if ($this->serializer === null) {
  150. $this->serializer = $this->_objectManager->get(
  151. \Magento\Framework\Serialize\SerializerInterface::class
  152. );
  153. }
  154. return $this->serializer;
  155. }
  156. /**
  157. * Get file iterator factory
  158. *
  159. * @return \Magento\Framework\Config\FileIteratorFactory
  160. * @deprecated 101.0.0
  161. */
  162. private function getFileIteratorFactory()
  163. {
  164. if ($this->fileIteratorFactory === null) {
  165. $this->fileIteratorFactory = $this->_objectManager->get(
  166. \Magento\Framework\Config\FileIteratorFactory::class
  167. );
  168. }
  169. return $this->fileIteratorFactory;
  170. }
  171. }