Ejemplo n.º 1
0
 public function exportModules($data)
 {
     $moduleTables = $this->getModuleTables();
     $configs = [];
     if (!empty($data['modules'])) {
         $store = $this->_storeManager->getStore($data['store_id']);
         foreach ($data['modules'] as $k => $v) {
             if (isset($moduleTables[$v])) {
                 $tables = $moduleTables[$v];
             }
             $systemFileDir = $this->_moduleDir->getDir($v, Dir::MODULE_ETC_DIR) . DIRECTORY_SEPARATOR . 'adminhtml' . DIRECTORY_SEPARATOR . 'system.xml';
             if (file_exists($systemFileDir)) {
                 $systemConfigs = $this->parser->load($systemFileDir)->xmlToArray();
                 if ($systemConfigs['config']['_value']['system']['section']) {
                     foreach ($systemConfigs['config']['_value']['system']['section'] as $_section) {
                         $groups = [];
                         if (isset($_section['_value']['group'])) {
                             $groups = $_section['_value']['group'];
                         } elseif (isset($_section['group'])) {
                             $groups = $_section['group'];
                         }
                         $_sectionId = '';
                         if (isset($_section['_attribute']['id'])) {
                             $_sectionId = $_section['_attribute']['id'];
                         } elseif (isset($systemConfigs['config']['_value']['system']['section']['_attribute']['id'])) {
                             $_sectionId = $systemConfigs['config']['_value']['system']['section']['_attribute']['id'];
                         }
                         if (empty($groups)) {
                             continue;
                         }
                         foreach ($groups as $_group) {
                             if (!isset($_group['_value']['field'])) {
                                 continue;
                             }
                             foreach ($_group['_value']['field'] as $_field) {
                                 if (isset($_sectionId) && isset($_group['_attribute']['id']) && isset($_field['_attribute']['id'])) {
                                     $key = $_sectionId . '/' . $_group['_attribute']['id'] . '/' . $_field['_attribute']['id'];
                                     $result = $this->_scopeConfig->getValue($key, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
                                     if ($result == '') {
                                         continue;
                                     }
                                     $configs[$v]['system_configs'][] = ['key' => $key, 'value' => $result];
                                 }
                             }
                         }
                     }
                 }
             }
             if (isset($moduleTables[$v]) && is_array($moduleTables[$v])) {
                 foreach ($moduleTables[$v] as $key => $tableName) {
                     $connection = $this->_resource->getConnection();
                     $select = 'SELECT * FROM ' . $this->_resource->getTableName($tableName);
                     $rows = $connection->fetchAll($select);
                     $configs[$v]['tables'][$tableName] = $rows;
                 }
             }
         }
     }
     return $configs;
 }
Ejemplo n.º 2
0
 /**
  * Load config from file
  *
  * @param string $filename
  * @throws \Exception
  *
  * @return void
  */
 public function loadConfig($filename)
 {
     if (!is_readable($filename)) {
         throw new \Exception("Profile configuration file `{$filename}` is not readable or does not exists.");
     }
     $this->config = $this->fileParser->load($filename)->xmlToArray();
 }
Ejemplo n.º 3
0
 /**
  * Loads the full module list information. Excludes modules specified in $exclude.
  *
  * @param array $exclude
  * @throws \Magento\Framework\Exception\LocalizedException
  * @return array
  */
 public function load(array $exclude = [])
 {
     $result = [];
     foreach ($this->getModuleConfigs() as list($file, $contents)) {
         try {
             $this->parser->loadXML($contents);
         } catch (\Magento\Framework\Exception\LocalizedException $e) {
             throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('Invalid Document: %1%2 Error: %3', [$file, PHP_EOL, $e->getMessage()]), $e);
         }
         $data = $this->converter->convert($this->parser->getDom());
         $name = key($data);
         if (!in_array($name, $exclude)) {
             $result[$name] = $data[$name];
         }
     }
     return $this->sortBySequence($result);
 }
Ejemplo n.º 4
0
 /**
  * Loads the full module list information
  *
  * @throws \Magento\Framework\Exception
  * @return array
  */
 public function load()
 {
     $result = [];
     $dir = $this->filesystem->getDirectoryRead(DirectoryList::MODULES);
     foreach ($dir->search('*/*/etc/module.xml') as $file) {
         $contents = $dir->readFile($file);
         try {
             $this->parser->loadXML($contents);
         } catch (\Magento\Framework\Exception $e) {
             throw new \Magento\Framework\Exception('Invalid Document: ' . $file . PHP_EOL . ' Error: ' . $e->getMessage(), $e->getCode(), $e);
         }
         $data = $this->converter->convert($this->parser->getDom());
         $name = key($data);
         $result[$name] = $data[$name];
     }
     return $this->sortBySequence($result);
 }
Ejemplo n.º 5
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);
 }
Ejemplo n.º 6
0
 /**
  * @expectedException \Magento\Framework\Exception\LocalizedException
  * @expectedExceptionMessage DOMDocument::loadXML(): Opening and ending tag mismatch
  */
 public function testLoadXmlInvalid()
 {
     $sampleInvalidXml = '<?xml version="1.0"?><config></onfig>';
     $this->parser->initErrorHandler();
     $this->parser->loadXML($sampleInvalidXml);
 }
Ejemplo n.º 7
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;
 }
Ejemplo n.º 8
0
 public function testGetXml()
 {
     $this->assertEquals(['data' => ['nodes' => ['text' => ' some text ', 'trim_spaces' => '', 'cdata' => '  Some data here <strong>html</strong> tags are <i>allowed</i>  ', 'zero' => '0', 'null' => null]]], $this->parser->load(__DIR__ . '/_files/data.xml')->xmlToArray());
 }