/** * Clear temporary directories * * @param \Magento\Install\Controller\Index\Index $subject * @param \Magento\Framework\App\RequestInterface $request * * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeDispatch(\Magento\Install\Controller\Index\Index $subject, \Magento\Framework\App\RequestInterface $request) { if (!$this->appState->isInstalled()) { foreach ($this->varDirectory->read() as $dir) { if ($this->varDirectory->isDirectory($dir)) { try { $this->varDirectory->delete($dir); } catch (FilesystemException $exception) { $this->logger->log($exception->getMessage()); } } } } }
/** * Create Backup * * @return $this */ public function scheduledBackup() { if (!$this->_scopeConfig->isSetFlag(self::XML_PATH_BACKUP_ENABLED, ScopeInterface::SCOPE_STORE)) { return $this; } if ($this->_scopeConfig->isSetFlag(self::XML_PATH_BACKUP_MAINTENANCE_MODE, ScopeInterface::SCOPE_STORE)) { $this->maintenanceMode->set(true); } $type = $this->_scopeConfig->getValue(self::XML_PATH_BACKUP_TYPE, ScopeInterface::SCOPE_STORE); $this->_errors = array(); try { $backupManager = $this->_backupFactory->create($type)->setBackupExtension($this->_backupData->getExtensionByType($type))->setTime(time())->setBackupsDir($this->_backupData->getBackupsDir()); $this->_coreRegistry->register('backup_manager', $backupManager); if ($type != \Magento\Framework\Backup\Factory::TYPE_DB) { $backupManager->setRootDir($this->_filesystem->getPath(\Magento\Framework\App\Filesystem::ROOT_DIR))->addIgnorePaths($this->_backupData->getBackupIgnorePaths()); } $backupManager->create(); $message = $this->_backupData->getCreateSuccessMessageByType($type); $this->_logger->log($message); } catch (\Exception $e) { $this->_errors[] = $e->getMessage(); $this->_errors[] = $e->getTrace(); $this->_logger->log($e->getMessage(), \Zend_Log::ERR); $this->_logger->logException($e); } if ($this->_scopeConfig->isSetFlag(self::XML_PATH_BACKUP_MAINTENANCE_MODE, ScopeInterface::SCOPE_STORE)) { $this->maintenanceMode->set(false); } return $this; }
/** * Show error as exception or log it * * @param string $errorMessage * @throws \Exception * @return void */ protected function _processError($errorMessage) { if ($this->_appState->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) { parent::_processError($errorMessage); } else { $exception = new \Exception($errorMessage); $errorMessage .= $exception->getTraceAsString(); $this->_logger->log($errorMessage, \Zend_Log::ERR); } }
/** * Run module modification files. Return version of last applied upgrade (false if no upgrades applied) * @param string $actionType * @param string $fromVersion * @param string $toVersion * @return false|string * @throws \Magento\Framework\Exception */ protected function _modifyResourceDb($actionType, $fromVersion, $toVersion) { switch ($actionType) { case self::TYPE_DB_INSTALL: case self::TYPE_DB_UPGRADE: $files = $this->_getAvailableDbFiles($actionType, $fromVersion, $toVersion); break; case self::TYPE_DATA_INSTALL: case self::TYPE_DATA_UPGRADE: $files = $this->_getAvailableDataFiles($actionType, $fromVersion, $toVersion); break; default: $files = array(); break; } if (empty($files) || !$this->getConnection()) { return false; } $version = false; foreach ($files as $file) { $fileName = $file['fileName']; $fileType = pathinfo($fileName, PATHINFO_EXTENSION); $this->getConnection()->disallowDdlCache(); try { switch ($fileType) { case 'php': $result = $this->_includeFile($fileName); break; case 'sql': $sql = $this->modulesDir->readFile($this->modulesDir->getRelativePath($fileName)); if (!empty($sql)) { $result = $this->run($sql); } else { $result = true; } break; default: $result = false; break; } if ($result) { $this->_setResourceVersion($actionType, $file['toVersion']); $this->_logger->log($fileName); } else { $this->_logger->log("Failed resource setup: {$fileName}"); } } catch (\Exception $e) { throw new \Magento\Framework\Exception(sprintf('Error in file: "%s" - %s', $fileName, $e->getMessage()), 0, $e); } $version = $file['toVersion']; $this->getConnection()->allowDdlCache(); } return $version; }
/** * Save file to storage * * @param string $filePath * @param string $content * @param bool $overwrite * @throws \Magento\Framework\Model\Exception * @return bool */ public function saveFile($filePath, $content, $overwrite = false) { try { $directoryInstance = $this->_filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::MEDIA_DIR); if (!$directoryInstance->isFile($filePath) || $overwrite && $directoryInstance->delete($filePath)) { $directoryInstance->writeFile($filePath, $content); return true; } } catch (\Magento\Framework\Filesystem\FilesystemException $e) { $this->_logger->log($e->getMessage()); throw new \Magento\Framework\Model\Exception(__('Unable to save file: %1', $filePath)); } return false; }
/** * Create package file compatible with previous version of Magento Connect Manager * * @return boolean */ public function createPackageV1x() { try { $this->writeDirectory->create('pear/'); } catch (\Magento\Framework\Filesystem\FilesystemException $e) { $this->logger->addStreamLog(\Magento\Framework\Logger::LOGGER_EXCEPTION); $this->logger->log($e->getMessage()); return false; } if (!$this->getPackageXml()) { $this->generatePackageXml(); } $this->getPackage()->saveV1x($this->writeDirectory->getAbsolutePath('pear/')); return true; }
public function testLogNoKey() { $key = 'key'; $this->model->log($key); $this->assertFalse($this->model->hasLog($key)); }
/** * Create destination folder if not exists and return full file path * * @param string $destination * @param string $newName * @return string * @throws \Exception */ protected function _prepareDestination($destination = null, $newName = null) { if (empty($destination)) { $destination = $this->_fileSrcPath; } else { if (empty($newName)) { $info = pathinfo($destination); $newName = $info['basename']; $destination = $info['dirname']; } } if (empty($newName)) { $newFileName = $this->_fileSrcName; } else { $newFileName = $newName; } $fileName = $destination . '/' . $newFileName; if (!is_writable($destination)) { try { $this->directoryWrite->create($this->directoryWrite->getRelativePath($destination)); } catch (\Magento\Framework\Filesystem\FilesystemException $e) { $this->logger->addStreamLog(\Magento\Framework\Logger::LOGGER_SYSTEM); $this->logger->log($e->getMessage()); throw new \Exception('Unable to write file into directory ' . $destination . '. Access forbidden.'); } } return $fileName; }
/** * Create block object instance based on block type * * @param string|\Magento\Framework\View\Element\AbstractBlock $block * @param array $attributes * @throws \Magento\Framework\Model\Exception * @return \Magento\Framework\View\Element\AbstractBlock */ protected function _getBlockInstance($block, array $attributes = array()) { if ($block && is_string($block)) { try { $block = $this->_blockFactory->createBlock($block, $attributes); } catch (\ReflectionException $e) { $this->_logger->log($e->getMessage()); } } if (!$block instanceof \Magento\Framework\View\Element\AbstractBlock) { throw new \Magento\Framework\Model\Exception(__('Invalid block type: %1', $block)); } return $block; }
/** * Log xml errors to system log * * @param string $fileName * @param array $libXmlErrors * @return void */ protected function _logXmlErrors($fileName, $libXmlErrors) { $errors = array(); if (count($libXmlErrors)) { foreach ($libXmlErrors as $error) { $errors[] = "{$error->message} Line: {$error->line}"; } $this->_logger->log(sprintf("Theme layout update file '%s' is not valid.\n%s", $fileName, implode("\n", $errors)), \Zend_Log::ERR); } }
/** * Log query * * @param string $sql * @return void */ protected function _logQuery($sql) { $this->_logger->log(is_null($sql) ? $this->getSelect()->__toString() : $sql); }
/** * Prepare shipping rate result based on response * * @param string $response * @return Result */ protected function _parseCgiResponse($response) { $costArr = array(); $priceArr = array(); if (strlen(trim($response)) > 0) { $rRows = explode("\n", $response); $allowedMethods = explode(",", $this->getConfigData('allowed_methods')); foreach ($rRows as $rRow) { $row = explode('%', $rRow); switch (substr($row[0], -1)) { case 3: case 4: if (in_array($row[1], $allowedMethods)) { $responsePrice = $this->_localeFormat->getNumber($row[8]); $costArr[$row[1]] = $responsePrice; $priceArr[$row[1]] = $this->getMethodPrice($responsePrice, $row[1]); } break; case 5: $errorTitle = $row[1]; $message = __('Sorry, something went wrong. Please try again or contact us and we\'ll try to help.'); $this->_logger->log($message . ': ' . $errorTitle); break; case 6: if (in_array($row[3], $allowedMethods)) { $responsePrice = $this->_localeFormat->getNumber($row[10]); $costArr[$row[3]] = $responsePrice; $priceArr[$row[3]] = $this->getMethodPrice($responsePrice, $row[3]); } break; default: break; } } asort($priceArr); } $result = $this->_rateFactory->create(); if (empty($priceArr)) { $error = $this->_rateErrorFactory->create(); $error->setCarrier('ups'); $error->setCarrierTitle($this->getConfigData('title')); $error->setErrorMessage($this->getConfigData('specificerrmsg')); $result->append($error); } else { foreach ($priceArr as $method => $price) { $rate = $this->_rateMethodFactory->create(); $rate->setCarrier('ups'); $rate->setCarrierTitle($this->getConfigData('title')); $rate->setMethod($method); $methodArray = $this->configHelper->getCode('method', $method); $rate->setMethodTitle($methodArray); $rate->setCost($costArr[$method]); $rate->setPrice($price); $result->append($rate); } } return $result; }