statusFilePath = $statusFilePath ? $statusFilePath : MAGENTO_BP . '/var/.update_status.txt'; $this->logFilePath = $logFilePath ? $logFilePath : MAGENTO_BP . '/var/log/update.log'; $this->updateInProgressFlagFilePath = $updateInProgressFlagFilePath ? $updateInProgressFlagFilePath : MAGENTO_BP . '/var/.update_in_progress.flag'; $this->updateErrorFlagFilePath = $updateErrorFlagFilePath ? $updateErrorFlagFilePath : MAGENTO_BP . '/var/.update_error.flag'; $updateLoggerFactory = new UpdateLoggerFactory(); $this->logger = $updateLoggerFactory->create(); } /** * Get current updater application status. * * @return string */ public function get() { if (file_exists($this->statusFilePath)) { return file_get_contents($this->statusFilePath); } return ''; } /** * Add status update. * * Add information to a temporary file which is used for status display on a web page and to a permanent status log. * * @param string $text * @return $this * @throws \RuntimeException */ public function add($text, $severity = \Psr\Log\LogLevel::INFO) { $this->logger->log($severity, $text); $currentUtcTime = '[' . date('Y-m-d H:i:s T', time()) . '] '; $text = $currentUtcTime . $text; $this->writeMessageToFile($text, $this->statusFilePath); return $this; } /** * Add status update to show progress * * @param string $text * @return $this * @throws \RuntimeException */ public function addWithoutNewLine($text) { $this->writeMessageToFile($text, $this->logFilePath, false); $this->writeMessageToFile($text, $this->statusFilePath, false); return $this; } /** * Write status information to the file. * * @param string $text * @param string $filePath * @param bool $newline * @return $this * @throws \RuntimeException */ protected function writeMessageToFile($text, $filePath, $newline = true) { $isNewFile = !file_exists($filePath); if (!$isNewFile && file_get_contents($filePath)) { $text = $newline ? PHP_EOL . "{$text}" :"{$text}"; } if (false === file_put_contents($filePath, $text, FILE_APPEND)) { throw new \RuntimeException(sprintf('Cannot add status information to "%s"', $filePath)); } if ($isNewFile) { chmod($filePath, 0777); } return $this; } /** * Clear current status text. * * Note that this method does not clear status information from the permanent status log. * * @return $this * @throws \RuntimeException */ public function clear() { if (!file_exists($this->statusFilePath)) { return $this; } else if (false === file_put_contents($this->statusFilePath, '')) { throw new \RuntimeException(sprintf('Cannot clear status information from "%s"', $this->statusFilePath)); } return $this; } /** * Check if updater application is running. * * @return bool */ public function isUpdateInProgress() { return file_exists($this->updateInProgressFlagFilePath); } /** * Set current updater app status: true if update is in progress, false otherwise. * * @param bool $isInProgress * @return $this */ public function setUpdateInProgress($isInProgress = true) { return $this->setFlagValue($this->updateInProgressFlagFilePath, $isInProgress); } /** * Check if error has occurred during updater application execution. * * @return bool */ public function isUpdateError() { return file_exists($this->updateErrorFlagFilePath); } /** * Set current updater app status: true if error occurred during update app execution, false otherwise. * * @param bool $isErrorOccurred * @return $this */ public function setUpdateError($isErrorOccurred = true) { return $this->setFlagValue($this->updateErrorFlagFilePath, $isErrorOccurred); } /** * Create flag in case when value is set to 'true', remove it if value is set to 'false'. * * @param string $pathToFlagFile * @param bool $value * @throws \RuntimeException * @return $this */ protected function setFlagValue($pathToFlagFile, $value) { if ($value) { $updateInProgressFlagFile = fopen($pathToFlagFile, 'w'); if (!$updateInProgressFlagFile) { throw new \RuntimeException(sprintf('"%s" cannot be created.', $pathToFlagFile)); } fclose($updateInProgressFlagFile); } else if (file_exists($pathToFlagFile)) { unlink($pathToFlagFile); } return $this; } }