Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MetadataGenUtil.php
Go to the documentation of this file.
1 <?php
8 
9 use Mustache_Engine;
10 use Mustache_Loader_FilesystemLoader;
11 
13 {
14  const OUTPUT_DIR = '_output';
15  const INPUT_TXT_FILE = 'input.yml';
16 
22  private $mustacheEngine;
23 
29  private $operationName;
30 
36  private $operationDataType;
37 
43  private $operationUrl;
44 
51  private $inputString;
52 
58  private $filepath;
59 
68  public function __construct($operationName, $operationDataType, $operationUrl, $inputString)
69  {
70  $this->operationName = $operationName;
71  $this->operationDataType = $operationDataType;
72  $this->operationUrl = $operationUrl;
73  $this->inputString = $inputString;
74 
75  $this->filepath = self::OUTPUT_DIR . DIRECTORY_SEPARATOR . $this->operationDataType . "-meta.xml";
76  }
77 
84  public function generateMetadataFile()
85  {
86  // Load Mustache templates
87  $this->mustacheEngine = new Mustache_Engine(
88  ['loader' => new Mustache_Loader_FilesystemLoader("views"),
89  'partials_loader' => new Mustache_Loader_FilesystemLoader(
90  "views" . DIRECTORY_SEPARATOR . "partials"
91  )]
92  );
93 
94  // parse the string params into an array
95  parse_str($this->inputString, $results);
96  $data = $this->convertResultToEntry($results, $this->operationDataType);
97  $data = $this->appendParentParams($data);
98  $output = $this->mustacheEngine->render('operation', $data);
99  $this->cleanAndCreateOutputDir();
101  $this->filepath,
102  $output
103  );
104  }
105 
112  private function appendParentParams($data)
113  {
114  $result = $data;
115  $result['operationName'] = $this->operationName;
116  $result['operationDataType'] = $this->operationDataType;
117  $result['operationUrl'] = $this->operationUrl;
118 
119  return $result;
120  }
121 
130  private function convertResultToEntry($results, $defaultDataType)
131  {
132  $data = [];
133 
134  foreach ($results as $key => $result) {
135  $entry = [];
136  if (is_array($result)) {
137  $entry = array_merge($entry, ['objectName' => $key]);
138  $res = $this->convertResultToEntry($result, $defaultDataType);
139  if (!array_key_exists('objects', $res)) {
140  $entry = array_merge($entry, ['objects' => null]);
141  $entry = array_merge($entry, ['dataType' => $key]);
142  } else {
143  $entry = array_merge($entry, ['hasChildObj' => true]);
144  $entry = array_merge($entry, ['dataType' => $defaultDataType]);
145  }
146  $data['objects'][] = array_merge($entry, $res);
147  } else {
148  $data['fields'][] = ['fieldName' => $key];
149  }
150  }
151 
152  return $data;
153  }
154 
160  private function cleanAndCreateOutputDir()
161  {
162  if (!file_exists(self::OUTPUT_DIR)) {
163  mkdir(self::OUTPUT_DIR);
164  }
165 
166  if (file_exists($this->filepath)) {
167  unlink($this->filepath);
168  }
169  }
170 }
$results
Definition: popup.phtml:13
__construct($operationName, $operationDataType, $operationUrl, $inputString)
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
Definition: ioMock.php:25