Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConvertToXml.php
Go to the documentation of this file.
1 <?php
7 
17 
22 {
26  protected $directory;
27 
31  protected $metadataProvider;
32 
36  protected $excelFactory;
37 
41  protected $options;
42 
46  protected $iteratorFactory;
47 
51  protected $fields;
52 
56  protected $filter;
57 
66  public function __construct(
71  SearchResultIteratorFactory $iteratorFactory
72  ) {
73  $this->filter = $filter;
74  $this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
75  $this->metadataProvider = $metadataProvider;
76  $this->excelFactory = $excelFactory;
77  $this->iteratorFactory = $iteratorFactory;
78  }
79 
85  protected function getOptions()
86  {
87  if (!$this->options) {
88  $this->options = $this->metadataProvider->getOptions();
89  }
90  return $this->options;
91  }
92 
99  protected function getFields()
100  {
101  if (!$this->fields) {
102  $component = $this->filter->getComponent();
103  $this->fields = $this->metadataProvider->getFields($component);
104  }
105  return $this->fields;
106  }
107 
115  public function getRowData(DocumentInterface $document)
116  {
117  return $this->metadataProvider->getRowData($document, $this->getFields(), $this->getOptions());
118  }
119 
126  public function getXmlFile()
127  {
128  $component = $this->filter->getComponent();
129 
130  $name = md5(microtime());
131  $file = 'export/'. $component->getName() . $name . '.xml';
132 
133  $this->filter->prepareComponent($component);
134  $this->filter->applySelectionOnTargetProvider();
135 
136  $component->getContext()->getDataProvider()->setLimit(0, 0);
137 
139  $searchResult = $component->getContext()->getDataProvider()->getSearchResult();
140 
142  $searchResultItems = $searchResult->getItems();
143 
144  $this->prepareItems($component->getName(), $searchResultItems);
145 
147  $searchResultIterator = $this->iteratorFactory->create(['items' => $searchResultItems]);
148 
150  $excel = $this->excelFactory->create(
151  [
152  'iterator' => $searchResultIterator,
153  'rowCallback'=> [$this, 'getRowData'],
154  ]
155  );
156 
157  $this->directory->create('export');
158  $stream = $this->directory->openFile($file, 'w+');
159  $stream->lock();
160 
161  $excel->setDataHeader($this->metadataProvider->getHeaders($component));
162  $excel->write($stream, $component->getName() . '.xml');
163 
164  $stream->unlock();
165  $stream->close();
166 
167  return [
168  'type' => 'filename',
169  'value' => $file,
170  'rm' => true // can delete file after use
171  ];
172  }
173 
179  protected function prepareItems($componentName, array $items = [])
180  {
181  foreach ($items as $document) {
182  $this->metadataProvider->convertDate($document, $componentName);
183  }
184  }
185 }
getRowData(DocumentInterface $document)
prepareItems($componentName, array $items=[])
__construct(Filesystem $filesystem, Filter $filter, MetadataProvider $metadataProvider, ExcelFactory $excelFactory, SearchResultIteratorFactory $iteratorFactory)
$filesystem
$items
if(!isset($_GET['name'])) $name
Definition: log.php:14