State.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup\SampleData;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem;
  9. class State implements StateInterface
  10. {
  11. /**
  12. * @var string
  13. */
  14. protected $fileName = '.sample-data-state.flag';
  15. /**
  16. * @var string|null
  17. */
  18. protected $filePath;
  19. /**
  20. * @var Filesystem
  21. */
  22. protected $filesystem;
  23. /**
  24. * @param Filesystem $filesystem
  25. */
  26. public function __construct(Filesystem $filesystem)
  27. {
  28. $this->filesystem = $filesystem;
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. public function hasError()
  34. {
  35. $isError = false;
  36. $stream = $this->openStream('r');
  37. if (!$stream) {
  38. return $isError;
  39. } elseif (strpos(trim($stream->read(400)), self::ERROR) !== false) {
  40. $isError = true;
  41. }
  42. $this->closeStream($stream);
  43. return $isError;
  44. }
  45. /**
  46. * @inheritdoc
  47. */
  48. public function setError()
  49. {
  50. if (!$this->hasError()) {
  51. $this->writeStream(self::ERROR);
  52. }
  53. }
  54. /**
  55. * @inheritdoc
  56. */
  57. public function isInstalled()
  58. {
  59. $isInstalled = false;
  60. /**@var $stream \Magento\Framework\Filesystem\File\WriteInterface */
  61. $stream = $this->openStream('r');
  62. if (!$stream) {
  63. return $isInstalled;
  64. } else {
  65. $state = trim($stream->read(400));
  66. if (strpos($state, self::ERROR) !== false || strpos($state, self::INSTALLED) !== false) {
  67. $isInstalled = true;
  68. }
  69. }
  70. $this->closeStream($stream);
  71. return $isInstalled;
  72. }
  73. /**
  74. * @inheritdoc
  75. */
  76. public function setInstalled()
  77. {
  78. if (!$this->isInstalled()) {
  79. $this->writeStream(self::INSTALLED);
  80. }
  81. }
  82. /**
  83. * @inheritdoc
  84. */
  85. public function clearState()
  86. {
  87. if ($this->openStream('w')) {
  88. $this->writeStream('');
  89. }
  90. }
  91. /**
  92. * @return \Magento\Framework\Filesystem\File\WriteInterface
  93. */
  94. protected function getStream()
  95. {
  96. if (!$stream = $this->openStream('w')) {
  97. $stream = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR)->openFile($this->fileName);
  98. }
  99. return $stream;
  100. }
  101. /**
  102. * @param string $mode
  103. * @return bool|\Magento\Framework\Filesystem\File\WriteInterface
  104. */
  105. protected function openStream($mode = 'w')
  106. {
  107. $fileName = $this->fileName;
  108. $stream = false;
  109. $directoryWrite = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
  110. if ($directoryWrite->isExist($fileName)) {
  111. $stream = $directoryWrite->openFile($fileName, $mode);
  112. }
  113. return $stream;
  114. }
  115. /**
  116. * @param string $data
  117. * @throws \Exception
  118. * @return void
  119. */
  120. protected function writeStream($data)
  121. {
  122. $stream = $this->getStream();
  123. if ($stream === false) {
  124. throw new \Exception(
  125. 'Please ensure that the ' . $this->fileName
  126. . ' file exists in the var directory and is writable.'
  127. );
  128. }
  129. $stream->write($data);
  130. $this->closeStream($stream);
  131. }
  132. /**
  133. * Closing file stream
  134. *
  135. * @param \Magento\Framework\Filesystem\File\WriteInterface $stream
  136. * @return void
  137. */
  138. protected function closeStream($stream)
  139. {
  140. if ($stream) {
  141. $stream->close();
  142. }
  143. }
  144. }