/** * Returns CSV file * * @return array * @throws LocalizedException */ public function getCsvFile() { $component = $this->filter->getComponent(); $name = md5(microtime()); $file = 'export/' . $component->getName() . $name . '.csv'; $this->filter->prepareComponent($component); $this->filter->applySelectionOnTargetProvider(); $dataProvider = $component->getContext()->getDataProvider(); $fields = $this->metadataProvider->getFields($component); $options = $this->metadataProvider->getOptions(); $this->directory->create('export'); $stream = $this->directory->openFile($file, 'w+'); $stream->lock(); $stream->writeCsv($this->metadataProvider->getHeaders($component)); $i = 1; $searchCriteria = $dataProvider->getSearchCriteria()->setCurrentPage($i)->setPageSize($this->pageSize); $totalCount = (int) $dataProvider->getSearchResult()->getTotalCount(); while ($totalCount > 0) { $items = $dataProvider->getSearchResult()->getItems(); foreach ($items as $item) { $this->metadataProvider->convertDate($item, $component->getName()); $stream->writeCsv($this->metadataProvider->getRowData($item, $fields, $options)); } $searchCriteria->setCurrentPage(++$i); $totalCount = $totalCount - $this->pageSize; } $stream->unlock(); $stream->close(); return ['type' => 'filename', 'value' => $file, 'rm' => true]; }
/** * Minify template file * * @param string $file * @return void */ public function minify($file) { $file = $this->rootDirectory->getRelativePath($file); $content = preg_replace('#(?<!]]>)\\s+</#', '</', preg_replace('#((?:<\\?php\\s+(?!echo|print|if|elseif|else)[^\\?]*)\\?>)\\s+#', '$1 ', preg_replace('#(?<!' . implode('|', $this->inlineHtmlTags) . ')\\> \\<#', '><', preg_replace('#(?ix)(?>[^\\S ]\\s*|\\s{2,})(?=(?:(?:[^<]++|<(?!/?(?:textarea|pre|script)\\b))*+)' . '(?:<(?>textarea|pre|script)\\b|\\z))#', ' ', preg_replace('#(?<!:|\\\\|\'|")//(?!\\s*\\<\\!\\[)(?!\\s*]]\\>)[^\\n\\r]*#', '', preg_replace('#(?<!:)//[^\\n\\r]*(\\s\\?\\>)#', '$1', preg_replace('#//[^\\n\\r]*(\\<\\?php)[^\\n\\r]*(\\s\\?\\>)[^\\n\\r]*#', '', $this->rootDirectory->readFile($file)))))))); if (!$this->htmlDirectory->isExist()) { $this->htmlDirectory->create(); } $this->htmlDirectory->writeFile($file, rtrim($content)); }
/** * Get file handler by process ID * * @param string $processId * @return File */ public function getFile($processId) { if (!isset($this->_fileHandlers[$processId])) { $this->_varDirectory->create('locks'); $fileName = 'locks/index_process_' . $processId . '.lock'; $stream = $this->_varDirectory->openFile($fileName, 'w+'); $stream->write(date('r')); $this->_fileHandlers[$processId] = $this->_fileFactory->create(array('streamHandler' => $stream)); } return $this->_fileHandlers[$processId]; }
/** * Returns XML file * * @return array * @throws LocalizedException */ public function getXmlFile() { $component = $this->filter->getComponent(); $name = md5(microtime()); $file = 'export/' . $component->getName() . $name . '.xml'; $this->filter->prepareComponent($component); $this->filter->applySelectionOnTargetProvider(); $component->getContext()->getDataProvider()->setLimit(0, 0); /** @var SearchResultInterface $searchResult */ $searchResult = $component->getContext()->getDataProvider()->getSearchResult(); /** @var DocumentInterface[] $searchResultItems */ $searchResultItems = $searchResult->getItems(); $this->prepareItems($component->getName(), $searchResultItems); /** @var SearchResultIterator $searchResultIterator */ $searchResultIterator = $this->iteratorFactory->create(['items' => $searchResultItems]); /** @var Excel $excel */ $excel = $this->excelFactory->create(['iterator' => $searchResultIterator, 'rowCallback' => [$this, 'getRowData']]); $this->directory->create('export'); $stream = $this->directory->openFile($file, 'w+'); $stream->lock(); $excel->setDataHeader($this->metadataProvider->getHeaders($component)); $excel->write($stream, $component->getName() . '.xml'); $stream->unlock(); $stream->close(); return ['type' => 'filename', 'value' => $file, 'rm' => true]; }
/** * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory * @param \Magento\Backup\Helper\Data $backupData * @param \Magento\Framework\Filesystem $filesystem * @param \Magento\Backup\Model\Backup $backup */ public function __construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Magento\Backup\Helper\Data $backupData, \Magento\Framework\Filesystem $filesystem, \Magento\Backup\Model\Backup $backup) { $this->_backupData = $backupData; parent::__construct($entityFactory); $this->_filesystem = $filesystem; $this->_backup = $backup; $this->_varDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); $this->_hideBackupsForApache(); // set collection specific params $extensions = $this->_backupData->getExtensions(); foreach ($extensions as $value) { $extensions[] = '(' . preg_quote($value, '/') . ')'; } $extensions = implode('|', $extensions); $this->_varDirectory->create($this->_path); $path = rtrim($this->_varDirectory->getAbsolutePath($this->_path), '/') . '/'; $this->setOrder('time', self::SORT_ORDER_DESC)->addTargetDir($path)->setFilesFilter('/^[a-z0-9\\-\\_]+\\.' . $extensions . '$/')->setCollectRecursively(false); }
/** * Directory structure initializing * * @return void */ protected function _initFilesystem() { $this->_mediaDirectory->create($this->_path); $this->_mediaDirectory->create($this->_quotePath); $this->_mediaDirectory->create($this->_orderPath); // Directory listing and hotlink secure $path = $this->_path . '/.htaccess'; if (!$this->_mediaDirectory->isFile($path)) { $this->_mediaDirectory->writeFile($path, "Order deny,allow\nDeny from all"); } }
/** * Returns an object for upload a media files * * @param string $type * @param array $parameters * @return \Magento\CatalogImportExport\Model\Import\Uploader * @throws \Magento\Framework\Exception\LocalizedException */ public function getUploader($type, $parameters) { $dirConfig = DirectoryList::getDefaultConfig(); $dirAddon = $dirConfig[DirectoryList::MEDIA][DirectoryList::PATH]; $DS = DIRECTORY_SEPARATOR; if (!empty($parameters[\Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR])) { $tmpPath = $parameters[\Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR]; } else { $tmpPath = $dirAddon . $DS . $this->mediaDirectory->getRelativePath('import'); } if (!$this->fileUploader->setTmpDir($tmpPath)) { throw new \Magento\Framework\Exception\LocalizedException(__('File directory \'%1\' is not readable.', $tmpPath)); } $destinationDir = "downloadable/files/" . $type; $destinationPath = $dirAddon . $DS . $this->mediaDirectory->getRelativePath($destinationDir); $this->mediaDirectory->create($destinationDir); if (!$this->fileUploader->setDestDir($destinationPath)) { throw new \Magento\Framework\Exception\LocalizedException(__('File directory \'%1\' is not writable.', $destinationPath)); } return $this->fileUploader; }
/** * Returns CSV file * * @return array * @throws LocalizedException */ public function getCsvFile() { $component = $this->filter->getComponent(); $name = md5(microtime()); $file = 'export/' . $component->getName() . $name . '.csv'; $this->filter->prepareComponent($component); $this->filter->applySelectionOnTargetProvider(); $searchResult = $component->getContext()->getDataProvider()->getSearchResult(); $fields = $this->metadataProvider->getFields($component); $options = $this->metadataProvider->getOptions(); $this->directory->create('export'); $stream = $this->directory->openFile($file, 'w+'); $stream->lock(); $stream->writeCsv($this->metadataProvider->getHeaders($component)); foreach ($searchResult->getItems() as $document) { $stream->writeCsv($this->metadataProvider->getRowData($document, $fields, $options)); } $stream->unlock(); $stream->close(); return ['type' => 'filename', 'value' => $file, 'rm' => true]; }
/** * Construct * * @param \Magento\Framework\Logger $logger * @param \Magento\Catalog\Model\Resource\ProductFactory $productFactory * @param \Magento\Framework\Event\ManagerInterface $eventManager * @param \Magento\Core\Helper\File\Storage\Database $fileStorageDb * @param \Magento\Core\Helper\Data $coreData * @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig * @param \Magento\Framework\App\Filesystem $filesystem * @param \Magento\Catalog\Model\Resource\Product\Attribute\Backend\Media $resourceProductAttribute */ public function __construct(\Magento\Framework\Logger $logger, \Magento\Catalog\Model\Resource\ProductFactory $productFactory, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Core\Helper\File\Storage\Database $fileStorageDb, \Magento\Core\Helper\Data $coreData, \Magento\Catalog\Model\Product\Media\Config $mediaConfig, \Magento\Framework\App\Filesystem $filesystem, \Magento\Catalog\Model\Resource\Product\Attribute\Backend\Media $resourceProductAttribute) { $this->_productFactory = $productFactory; $this->_eventManager = $eventManager; $this->_fileStorageDb = $fileStorageDb; $this->_coreData = $coreData; $this->_resourceModel = $resourceProductAttribute; $this->_mediaConfig = $mediaConfig; $this->_mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::MEDIA_DIR); $this->_mediaDirectory->create($this->_mediaConfig->getBaseMediaPath()); $this->_mediaDirectory->create($this->_mediaConfig->getBaseTmpMediaPath()); parent::__construct($logger); }
/** * @return string Path to config file * @throws \Exception */ public function makeConfig() { if (!$this->directory->isExist($this->basePath)) { //$this->directory->delete($this->basePath); $this->directory->create($this->basePath); $this->directory->changePermissions($this->basePath, 0777); } $jsonData = []; $sphinxData = ['time' => date('d.m.Y H:i:s'), 'host' => $this->host, 'port' => $this->port, 'fallback_port' => $this->port - 1, 'logdir' => $this->directory->getAbsolutePath($this->basePath), 'sphinxdir' => $this->directory->getAbsolutePath($this->basePath), 'indexes' => '', 'localdir' => dirname(dirname(__FILE__)), 'custom' => $this->config->getAdditionalSearchdConfig()]; $sphinxTemplate = $this->config->getSphinxConfigurationTemplate(); $indexTemplate = $this->config->getSphinxIndexConfigurationTemplate(); /** @var \Mirasvit\Search\Model\Index $index */ foreach ($this->indexCollectionFactory->create() as $index) { foreach (array_keys($this->storeManager->getStores()) as $storeId) { $indexName = $index->getIndexInstance()->getIndexer()->getIndexName($storeId); $data = ['name' => $indexName, 'min_word_len' => 1, 'path' => $this->directory->getAbsolutePath($this->basePath) . '/' . $indexName, 'custom' => $this->config->getAdditionalIndexConfig()]; $jsonAttributes = []; $attributes = []; foreach (array_keys($index->getIndexInstance()->getAttributes(true)) as $attribute) { $attributes[] = " rt_field = {$attribute}"; $jsonAttributes[] = $attribute; if (count($attributes) > 250) { break; } } $attributes[] = " rt_field = options"; $jsonAttributes[] = "options"; $data['attributes'] = implode(PHP_EOL, $attributes); $sphinxData['indexes'] .= $this->helper->filterTemplate($indexTemplate, $data); $jsonData[$indexName] = $jsonAttributes; } } $config = $this->helper->filterTemplate($sphinxTemplate, $sphinxData); if ($this->directory->isWritable($this->basePath)) { $this->directory->writeFile($this->configFilePath, $config); $this->directory->writeFile($this->configFilePath . '.attr', json_encode($jsonData)); } else { if ($this->directory->isExist($this->configFilePath)) { throw new \Exception(__('File %1 does not writable', $this->configFilePath)); } else { throw new \Exception(__('Directory %1 does not writable', $this->basePath)); } } return $this->directory->getAbsolutePath($this->configFilePath); }
/** * Retrieve a file container array by grid data as MS Excel 2003 XML Document * * Return array with keys type and value * * @param string $sheetName * @return array */ public function getExcelFile($sheetName = '') { $collection = $this->_getRowCollection(); $convert = new \Magento\Framework\Convert\Excel($collection->getIterator(), array($this, 'getRowRecord')); $name = md5(microtime()); $file = $this->_path . '/' . $name . '.xml'; $this->_directory->create($this->_path); $stream = $this->_directory->openFile($file, 'w+'); $stream->lock(); $convert->setDataHeader($this->_getExportHeaders()); if ($this->getCountTotals()) { $convert->setDataFooter($this->_getExportTotals()); } $convert->write($stream, $sheetName); $stream->unlock(); $stream->close(); return array('type' => 'filename', 'value' => $file, 'rm' => true); }
/** * Log debug data to file. * Log file dir: var/log/import_export/%Y/%m/%d/%time%_%operation_type%_%entity_type%.log * * @param mixed $debugData * @return $this */ public function addLogComment($debugData) { if (is_array($debugData)) { $this->_logTrace = array_merge($this->_logTrace, $debugData); } else { $this->_logTrace[] = $debugData; } if (!$this->_debugMode) { return $this; } if (!$this->_logInstance) { $dirName = date('Y/m/d/'); $fileName = join('_', array(str_replace(':', '-', $this->getRunAt()), $this->getScheduledOperationId(), $this->getOperationType(), $this->getEntity())); $path = 'import_export/' . $dirName; $this->_varDirectory->create($path); $fileName = $path . $fileName . '.log'; $this->_logInstance = $this->_adapterFactory->create(array('fileName' => $this->_varDirectory->getAbsolutePath($fileName)))->setFilterDataKeys($this->_debugReplacePrivateDataKeys); } $this->_logInstance->log($debugData); return $this; }
/** * Minify template file * * @param string $file * @return void */ public function minify($file) { $dir = dirname($file); $fileName = basename($file); $content = preg_replace( '#(?<!]]>)\s+</#', '</', preg_replace( '#((?:<\?php\s+(?!echo|print|if|elseif|else)[^\?]*)\?>)\s+#', '$1 ', preg_replace( '#(?<!' . implode('|', $this->inlineHtmlTags) . ')\> \<#', '><', preg_replace( '#(?ix)(?>[^\S ]\s*|\s{2,})(?=(?:(?:[^<]++|<(?!/?(?:textarea|pre|script)\b))*+)' . '(?:<(?>textarea|pre|script)\b|\z))#', ' ', preg_replace( '#(?<!:|\\\\|\'|")//(?!\s*\<\!\[)(?!\s*]]\>)[^\n\r]*#', '', preg_replace( '#(?<!:)//[^\n\r]*(\s\?\>)#', '$1', preg_replace( '#(?<!:)//[^\n\r]*(\<\?php)[^\n\r]*(\s\?\>)[^\n\r]*#', '', $this->readFactory->create($dir)->readFile($fileName) ) ) ) ) ) ) ); if (!$this->htmlDirectory->isExist()) { $this->htmlDirectory->create(); } $this->htmlDirectory->writeFile($this->getRelativeGeneratedPath($file), rtrim($content)); }
/** * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Framework\StoreManagerInterface $storeManager * @param \Magento\Catalog\Model\Product\Media\Config $catalogProductMediaConfig * @param \Magento\Core\Helper\File\Storage\Database $coreFileStorageDatabase * @param \Magento\Framework\App\Filesystem $filesystem * @param \Magento\Framework\Image\Factory $imageFactory * @param \Magento\Framework\View\Asset\Repository $assetRepo * @param \Magento\Framework\View\FileSystem $viewFileSystem * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param \Magento\Framework\Model\Resource\AbstractResource $resource * @param \Magento\Framework\Data\Collection\Db $resourceCollection * @param array $data */ public function __construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\StoreManagerInterface $storeManager, \Magento\Catalog\Model\Product\Media\Config $catalogProductMediaConfig, \Magento\Core\Helper\File\Storage\Database $coreFileStorageDatabase, \Magento\Framework\App\Filesystem $filesystem, \Magento\Framework\Image\Factory $imageFactory, \Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Framework\View\FileSystem $viewFileSystem, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Model\Resource\AbstractResource $resource = null, \Magento\Framework\Data\Collection\Db $resourceCollection = null, array $data = array()) { $this->_storeManager = $storeManager; $this->_catalogProductMediaConfig = $catalogProductMediaConfig; $this->_coreFileStorageDatabase = $coreFileStorageDatabase; parent::__construct($context, $registry, $resource, $resourceCollection, $data); $this->_mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::MEDIA_DIR); $result = $this->_mediaDirectory->create($this->_catalogProductMediaConfig->getBaseMediaPath()); $this->_imageFactory = $imageFactory; $this->_assetRepo = $assetRepo; $this->_viewFileSystem = $viewFileSystem; $this->_scopeConfig = $scopeConfig; }
/** * Returns an object for upload a media files * * @return \Magento\CatalogImportExport\Model\Import\Uploader * @throws \Magento\Framework\Exception\LocalizedException */ protected function _getUploader() { if (is_null($this->_fileUploader)) { $this->_fileUploader = $this->_uploaderFactory->create(); $this->_fileUploader->init(); $dirConfig = DirectoryList::getDefaultConfig(); $dirAddon = $dirConfig[DirectoryList::MEDIA][DirectoryList::PATH]; $DS = DIRECTORY_SEPARATOR; if (!empty($this->_parameters[\Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR])) { $tmpPath = $this->_parameters[\Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR]; } else { $tmpPath = $dirAddon . $DS . $this->_mediaDirectory->getRelativePath('import'); } if (!$this->_fileUploader->setTmpDir($tmpPath)) { throw new \Magento\Framework\Exception\LocalizedException(__('File directory \'%1\' is not readable.', $tmpPath)); } $destinationDir = "catalog/product"; $destinationPath = $dirAddon . $DS . $this->_mediaDirectory->getRelativePath($destinationDir); $this->_mediaDirectory->create($destinationPath); if (!$this->_fileUploader->setDestDir($destinationPath)) { throw new \Magento\Framework\Exception\LocalizedException(__('File directory \'%1\' is not writable.', $destinationPath)); } } return $this->_fileUploader; }
/** * Returns an object for upload a media files * * @return \Magento\CatalogImportExport\Model\Import\Uploader * @throws \Magento\Framework\Model\Exception */ protected function _getUploader() { if (is_null($this->_fileUploader)) { $this->_fileUploader = $this->_uploaderFactory->create(); $this->_fileUploader->init(); $tmpPath = $this->_mediaDirectory->getAbsolutePath('import'); if (!$this->_fileUploader->setTmpDir($tmpPath)) { throw new \Magento\Framework\Model\Exception(sprintf("File directory '%s' is not readable.", $tmpPath)); } $destinationDir = "catalog/product"; $destinationPath = $this->_mediaDirectory->getAbsolutePath($destinationDir); $this->_mediaDirectory->create($destinationDir); if (!$this->_fileUploader->setDestDir($destinationPath)) { throw new \Magento\Framework\Model\Exception(sprintf("File directory '%s' is not writable.", $destinationPath)); } } return $this->_fileUploader; }
/** * @throws LocalizedException */ protected function createBaseDir() { if (!$this->directory->create(Database::BASE_DIR)) { throw new LocalizedException(__('Cannot create db directory.')); } }