Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Validator.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\Exception\LocalizedException;
10 use \Magento\Theme\Api\Data\DesignConfigInterface;
11 use \Magento\Theme\Api\Data\DesignConfigDataInterface;
12 use \Magento\Framework\Mail\TemplateInterfaceFactory as TemplateFactory;
13 use \Magento\Framework\Filter\Template;
14 use \Magento\Framework\Filter\Template\Tokenizer\Parameter as ParameterTokenizer;
15 
19 class Validator
20 {
24  private $fields = [];
25 
29  private $templateFactory;
30 
37  public function __construct(TemplateFactory $templateFactory, $fields = [])
38  {
39  $this->templateFactory = $templateFactory;
40  $this->fields = $fields;
41  }
42 
51  public function validate(DesignConfigInterface $designConfig)
52  {
54  $designConfigData = $designConfig->getExtensionAttributes()->getDesignConfigData();
55  $elements = [];
56  foreach ($designConfigData as $designElement) {
57  if (!in_array($designElement->getFieldConfig()['field'], $this->fields)) {
58  continue;
59  }
60  /* Save mapping between field names and config paths */
61  $elements[$designElement->getFieldConfig()['field']] = [
62  'config_path' => $designElement->getPath(),
63  'value' => $designElement->getValue()
64  ];
65  }
66 
67  foreach ($elements as $name => $data) {
68  $templateId = $data['value'];
69  $text = $this->getTemplateText($templateId, $designConfig);
70  // Check if template body has a reference to the same config path
71  if (preg_match_all(Template::CONSTRUCTION_TEMPLATE_PATTERN, $text, $constructions, PREG_SET_ORDER)) {
72  foreach ($constructions as $construction) {
73  $configPath = isset($construction[2]) ? $construction[2] : '';
74  $params = $this->getParameters($configPath);
75  if (isset($params['config_path']) && $params['config_path'] == $data['config_path']) {
76  throw new LocalizedException(
77  __(
78  'The "%templateName" template contains an incorrect configuration, with a reference '
79  . 'to itself. Remove or change the reference, then try again.',
80  ["templateName" => $name]
81  )
82  );
83  };
84  }
85  }
86  }
87  }
88 
95  private function getScopeId(DesignConfigInterface $designConfig)
96  {
97  if ($designConfig->getScope() == 'stores') {
98  return $designConfig->getScopeId();
99  }
100  return false;
101  }
109  private function getTemplateText($templateId, DesignConfigInterface $designConfig)
110  {
111  // Load template object by configured template id
112  $template = $this->templateFactory->create();
113  $template->emulateDesign($this->getScopeId($designConfig));
114  if (is_numeric($templateId)) {
115  $template->load($templateId);
116  } else {
117  $template->setForcedArea($templateId);
118  $template->loadDefault($templateId);
119  }
120  $text = $template->getTemplateText();
121  $template->revertDesign();
122  return $text;
123  }
124 
131  private function getParameters($value)
132  {
133  $tokenizer = new ParameterTokenizer();
134  $tokenizer->setString($value);
135  $params = $tokenizer->tokenize();
136  return $params;
137  }
138 }
__()
Definition: __.php:13
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$templateId
Definition: queue.php:15
$value
Definition: gender.phtml:16
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
__construct(TemplateFactory $templateFactory, $fields=[])
Definition: Validator.php:37
$template
Definition: export.php:12
if(!isset($_GET['name'])) $name
Definition: log.php:14