DryRunLogger.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup\Declaration\Schema;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. /**
  9. * This class is responsible for logging dry run SQL`s
  10. * By default it logs them into filesystem, but it can be extended and you can log them in CLI
  11. * Current problem with logging output to CLI, is that we have redudant things in CLI output, like modules progress
  12. */
  13. class DryRunLogger
  14. {
  15. /**
  16. * We will run installation or upgrade in Dry Run mode
  17. */
  18. const INPUT_KEY_DRY_RUN_MODE = 'dry-run';
  19. /**
  20. * File name, where all dry-run SQL`s will be puted
  21. */
  22. const FILE_NAME = 'dry-run-installation.log';
  23. /**
  24. * Allows to separate 2 different sql statements with this separator
  25. * Be default is used 2 empty lines
  26. */
  27. const LINE_SEPARATOR = "\n\n";
  28. /**
  29. * @var \Magento\Framework\Filesystem\Driver\File
  30. */
  31. private $fileDriver;
  32. /**
  33. * @var DirectoryList
  34. */
  35. private $directoryList;
  36. /**
  37. * @param \Magento\Framework\Filesystem\Driver\File $fileDriver
  38. * @param DirectoryList $directoryList
  39. */
  40. public function __construct(
  41. \Magento\Framework\Filesystem\Driver\File $fileDriver,
  42. DirectoryList $directoryList
  43. ) {
  44. $this->fileDriver = $fileDriver;
  45. $this->directoryList = $directoryList;
  46. }
  47. /**
  48. * Create log directory if it doest not exists
  49. *
  50. * @param string $logFolderPath
  51. * @throws \Exception
  52. */
  53. private function assertLogFolderExists($logFolderPath)
  54. {
  55. if (!$this->fileDriver->isDirectory($logFolderPath)) {
  56. $this->fileDriver->createDirectory($logFolderPath);
  57. }
  58. if (!$this->fileDriver->isDirectory($logFolderPath)) {
  59. throw new \Exception(sprintf("Can`t create log directory: %s", $logFolderPath));
  60. }
  61. }
  62. /**
  63. * @param string $fileName
  64. * @throws \Exception
  65. */
  66. private function assertFileExists($fileName)
  67. {
  68. if (!$this->fileDriver->isExists($fileName)) {
  69. $this->fileDriver->touch($fileName);
  70. }
  71. if (!$this->fileDriver->isExists($fileName)) {
  72. throw new \Exception(sprintf("Can`t create file %s", $fileName));
  73. }
  74. }
  75. /**
  76. * Make file empty from request to request
  77. * @throws \Exception
  78. * @return void
  79. */
  80. public function prepareToDryRun()
  81. {
  82. if ($this->fileDriver->isExists($this->getLoggerFile())) {
  83. if (!$this->fileDriver->isWritable($this->getLoggerFile())) {
  84. throw new \Exception(sprintf('Dry run logger file is not writable'));
  85. }
  86. $this->fileDriver->deleteFile($this->getLoggerFile());
  87. $this->fileDriver->touch($this->getLoggerFile());
  88. }
  89. }
  90. /**
  91. * Return folder path, where dry run logged file will be placed
  92. *
  93. * @return string
  94. */
  95. private function getLoggerFolder()
  96. {
  97. return $this->directoryList->getPath(DirectoryList::VAR_DIR) .
  98. DIRECTORY_SEPARATOR . 'log';
  99. }
  100. /**
  101. * Return dry run logger file
  102. *
  103. * @return string
  104. */
  105. private function getLoggerFile()
  106. {
  107. return $this->getLoggerFolder() . DIRECTORY_SEPARATOR . self::FILE_NAME;
  108. }
  109. /**
  110. * Do log of SQL query, 2 different SQL`s will be divided by one empty line
  111. *
  112. * @param string $sql
  113. * @throws \Exception
  114. * @return void
  115. */
  116. public function log($sql)
  117. {
  118. $loggerFolder = $this->getLoggerFolder();
  119. $loggerFile = $this->getLoggerFile();
  120. $this->assertLogFolderExists($loggerFolder);
  121. $this->assertFileExists($loggerFile);
  122. if ($this->fileDriver->isWritable($loggerFile)) {
  123. $fd = $this->fileDriver->fileOpen($loggerFile, 'a');
  124. $this->fileDriver->fileWrite($fd, $sql . self::LINE_SEPARATOR);
  125. } else {
  126. throw new \Exception(sprintf('Can`t write to file %s', $loggerFile));
  127. }
  128. }
  129. }