/update/app/code/Magento/Update/Status.php

https://gitlab.com/daigiangaitu91/magento · PHP · 221 lines · 97 code · 16 blank · 108 comment · 12 complexity · ebf4dbfa7e3a81f0389556e488da6b91 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2015 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Update;
  7. /**
  8. * Class which provides access to the current status of the Magento updater application.
  9. *
  10. * Each job is using this class to share information about its current status.
  11. * Current status can be seen on the updater app web page.
  12. */
  13. class Status
  14. {
  15. /**
  16. * Path to a file, which content is displayed on the updater web page.
  17. *
  18. * @var string
  19. */
  20. protected $statusFilePath;
  21. /**
  22. * Path to a log file, which contains all the information displayed on the web page.
  23. *
  24. * Note that it can be cleared only manually, it is not cleared by clear() method.
  25. *
  26. * @var string
  27. */
  28. protected $logFilePath;
  29. /**
  30. * Path to a flag, which exists when updater app is running.
  31. *
  32. * @var string
  33. */
  34. protected $updateInProgressFlagFilePath;
  35. /**
  36. * Path to a flag, which exists when error occurred during updater app execution.
  37. *
  38. * @var string
  39. */
  40. protected $updateErrorFlagFilePath;
  41. /**
  42. * Initialize.
  43. *
  44. * @param string|null $statusFilePath
  45. * @param string|null $logFilePath
  46. * @param string|null $updateInProgressFlagFilePath
  47. * @param string|null $updateErrorFlagFilePath
  48. */
  49. public function __construct(
  50. $statusFilePath = null,
  51. $logFilePath = null,
  52. $updateInProgressFlagFilePath = null,
  53. $updateErrorFlagFilePath = null
  54. ) {
  55. $this->statusFilePath = $statusFilePath ? $statusFilePath : MAGENTO_BP . '/var/.update_status.txt';
  56. $this->logFilePath = $logFilePath ? $logFilePath : MAGENTO_BP . '/var/update_status.log';
  57. $this->updateInProgressFlagFilePath = $updateInProgressFlagFilePath
  58. ? $updateInProgressFlagFilePath
  59. : MAGENTO_BP . '/var/.update_in_progress.flag';
  60. $this->updateErrorFlagFilePath = $updateErrorFlagFilePath
  61. ? $updateErrorFlagFilePath
  62. : MAGENTO_BP . '/var/.update_error.flag';
  63. }
  64. /**
  65. * Get current updater application status.
  66. *
  67. * @return string
  68. */
  69. public function get()
  70. {
  71. if (file_exists($this->statusFilePath)) {
  72. return file_get_contents($this->statusFilePath);
  73. }
  74. return '';
  75. }
  76. /**
  77. * Add status update.
  78. *
  79. * Add information to a temporary file which is used for status display on a web page and to a permanent status log.
  80. *
  81. * @param string $text
  82. * @return $this
  83. * @throws \RuntimeException
  84. */
  85. public function add($text)
  86. {
  87. $currentUtcTime = '[' . date('Y-m-d H:i:s T', time()) . '] ';
  88. $text = $currentUtcTime . $text;
  89. $this->writeMessageToFile($text, $this->logFilePath);
  90. $this->writeMessageToFile($text, $this->statusFilePath);
  91. return $this;
  92. }
  93. /**
  94. * Add status update to show progress
  95. *
  96. * @param string $text
  97. * @return $this
  98. * @throws \RuntimeException
  99. */
  100. public function addWithoutNewLine($text)
  101. {
  102. $this->writeMessageToFile($text, $this->logFilePath, false);
  103. $this->writeMessageToFile($text, $this->statusFilePath, false);
  104. return $this;
  105. }
  106. /**
  107. * Write status information to the file.
  108. *
  109. * @param string $text
  110. * @param string $filePath
  111. * @param bool $newline
  112. * @return $this
  113. * @throws \RuntimeException
  114. */
  115. protected function writeMessageToFile($text, $filePath, $newline = true)
  116. {
  117. $isNewFile = !file_exists($filePath);
  118. if (!$isNewFile && file_get_contents($filePath)) {
  119. $text = $newline ? PHP_EOL . "{$text}" :"{$text}";
  120. }
  121. if (false === file_put_contents($filePath, $text, FILE_APPEND)) {
  122. throw new \RuntimeException(sprintf('Cannot add status information to "%s"', $filePath));
  123. }
  124. if ($isNewFile) {
  125. chmod($filePath, 0777);
  126. }
  127. return $this;
  128. }
  129. /**
  130. * Clear current status text.
  131. *
  132. * Note that this method does not clear status information from the permanent status log.
  133. *
  134. * @return $this
  135. * @throws \RuntimeException
  136. */
  137. public function clear()
  138. {
  139. if (!file_exists($this->statusFilePath)) {
  140. return $this;
  141. } else if (false === file_put_contents($this->statusFilePath, '')) {
  142. throw new \RuntimeException(sprintf('Cannot clear status information from "%s"', $this->statusFilePath));
  143. }
  144. return $this;
  145. }
  146. /**
  147. * Check if updater application is running.
  148. *
  149. * @return bool
  150. */
  151. public function isUpdateInProgress()
  152. {
  153. return file_exists($this->updateInProgressFlagFilePath);
  154. }
  155. /**
  156. * Set current updater app status: true if update is in progress, false otherwise.
  157. *
  158. * @param bool $isInProgress
  159. * @return $this
  160. */
  161. public function setUpdateInProgress($isInProgress = true)
  162. {
  163. return $this->setFlagValue($this->updateInProgressFlagFilePath, $isInProgress);
  164. }
  165. /**
  166. * Check if error has occurred during updater application execution.
  167. *
  168. * @return bool
  169. */
  170. public function isUpdateError()
  171. {
  172. return file_exists($this->updateErrorFlagFilePath);
  173. }
  174. /**
  175. * Set current updater app status: true if error occurred during update app execution, false otherwise.
  176. *
  177. * @param bool $isErrorOccurred
  178. * @return $this
  179. */
  180. public function setUpdateError($isErrorOccurred = true)
  181. {
  182. return $this->setFlagValue($this->updateErrorFlagFilePath, $isErrorOccurred);
  183. }
  184. /**
  185. * Create flag in case when value is set to 'true', remove it if value is set to 'false'.
  186. *
  187. * @param string $pathToFlagFile
  188. * @param bool $value
  189. * @throws \RuntimeException
  190. * @return $this
  191. */
  192. protected function setFlagValue($pathToFlagFile, $value)
  193. {
  194. if ($value) {
  195. $updateInProgressFlagFile = fopen($pathToFlagFile, 'w');
  196. if (!$updateInProgressFlagFile) {
  197. throw new \RuntimeException(sprintf('"%s" cannot be created.', $pathToFlagFile));
  198. }
  199. fclose($updateInProgressFlagFile);
  200. } else if (file_exists($pathToFlagFile)) {
  201. unlink($pathToFlagFile);
  202. }
  203. return $this;
  204. }
  205. }