Beispiel #1
0
 /**
  * Convert XML document into array.
  *
  * @param string $xmlRequestBody XML document
  * @return array Data converted from XML document to array. Root node is excluded from response.
  * @throws \InvalidArgumentException In case of invalid argument type.
  * @throws \Magento\Framework\Webapi\Exception If decoding error occurs.
  */
 public function deserialize($xmlRequestBody)
 {
     if (!is_string($xmlRequestBody)) {
         throw new \InvalidArgumentException(sprintf('"%s" data type is invalid. String is expected.', gettype($xmlRequestBody)));
     }
     /** Disable external entity loading to prevent possible vulnerability */
     $previousLoaderState = libxml_disable_entity_loader(true);
     set_error_handler([$this, 'handleErrors']);
     $this->_xmlParser->loadXML($xmlRequestBody);
     restore_error_handler();
     libxml_disable_entity_loader($previousLoaderState);
     /** Process errors during XML parsing. */
     if ($this->_errorMessage !== null) {
         if ($this->_appState->getMode() !== State::MODE_DEVELOPER) {
             $exceptionMessage = new Phrase('Decoding error.');
         } else {
             $exceptionMessage = 'Decoding Error: ' . $this->_errorMessage;
         }
         throw new \Magento\Framework\Webapi\Exception($exceptionMessage);
     }
     $data = $this->_xmlParser->xmlToArray();
     /** Data will always have exactly one element so it is safe to call reset here. */
     return reset($data);
 }
Beispiel #2
0
 /**
  * run action
  *
  * @return \Magento\Framework\View\Result\Page
  */
 public function execute()
 {
     $pageResult = $this->resultPageFactory->create();
     $pageResult->getConfig()->getTitle()->set(__('Ultimate Module Creator'));
     $id = $this->getRequest()->getParam('id');
     $module = $this->moduleFactory->create();
     if ($id) {
         try {
             $moduleName = $this->decoder->decode($id);
             $path = $this->settings->getXmlRootPath();
             $moduleName = basename($moduleName);
             $xmlFile = $moduleName . '.xml';
             $rootDir = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
             $file = $rootDir->getRelativePath(Settings::VAR_DIR_NAME . '/' . $xmlFile);
             if ($rootDir->isFile($file) && $rootDir->isReadable($file)) {
                 $this->xmlParser->load($path . '/' . $xmlFile);
                 $data = $this->xmlParser->xmlToArray();
                 $data = $data[$module->getEntityCode()];
                 $entityIdsByCode = [];
                 if (isset($data['entities']['entity'][0])) {
                     $entities = $data['entities']['entity'];
                 } else {
                     $entities = [$data['entities']['entity']];
                 }
                 if (isset($data[$this->settings->getEntityCode()])) {
                     $settings = $data[$this->settings->getEntityCode()];
                     unset($data[$this->settings->getEntityCode()]);
                 } else {
                     $settings = [];
                 }
                 foreach ($entities as $key => $entity) {
                     if (isset($entity['attributes']['attribute'])) {
                         if (isset($entity['attributes']['attribute'][0])) {
                             $entities[$key]['attributes'] = $entity['attributes']['attribute'];
                         } else {
                             $entities[$key]['attributes'] = [$entity['attributes']['attribute']];
                         }
                     }
                     $entityIdsByCode[$entity['name_singular']] = $key;
                 }
                 unset($data['entities']);
                 $relationsByCode = [];
                 if (isset($data['relations'])) {
                     if (isset($data['relations']['relation'][0])) {
                         foreach ($data['relations']['relation'] as $relationArray) {
                             $relationsByCode = array_merge($relationsByCode, $relationArray);
                         }
                     } else {
                         $relationsByCode = $data['relations']['relation'];
                     }
                 }
                 $relations = [];
                 foreach ($relationsByCode as $code => $value) {
                     $parts = explode('_', $code);
                     if (count($parts) != 2) {
                         continue;
                     }
                     if (isset($entityIdsByCode[$parts[0]]) && isset($entityIdsByCode[$parts[1]])) {
                         $entity0 = $entityIdsByCode[$parts[0]];
                         $entity1 = $entityIdsByCode[$parts[1]];
                         $relations[$entity0][$entity1] = $value;
                     }
                 }
                 unset($data['relations']);
                 $data = [$module->getEntityCode() => $data, 'entity' => $entities, $this->settings->getEntityCode() => $settings, 'relation' => $relations];
             } else {
                 $data = [];
             }
         } catch (\Exception $e) {
             $this->messageManager->addError($e->getMessage());
             $pageRedirect = $this->pageRedirectFactory->create();
             $pageRedirect->setPath('umc/*/index');
             return $pageRedirect;
         }
     } else {
         $data = [];
     }
     $module->initFromData($data);
     $this->coreRegistry->register('current_module', $module);
     if ($id) {
         $title = __('Edit Module "%1"', $module->getNamespace() . '_' . $module->getModuleName());
     } else {
         $title = __('Create module');
     }
     $pageResult->getConfig()->getTitle()->prepend($title);
     $this->_setActiveMenu('Umc_Base::umc')->_addBreadcrumb(__('Ultimate Module Creator'), __('Ultimate Module Creator'))->_addBreadcrumb($title, $title);
     return $pageResult;
 }