MaintenanceMode.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © 2013-2017 Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Update;
  7. use Magento\Update\Status;
  8. /**
  9. * Class for handling Magento maintenance mode.
  10. */
  11. class MaintenanceMode
  12. {
  13. /**
  14. * Path to the maintenance flag file
  15. *
  16. * @var string
  17. */
  18. protected $flagFile;
  19. /**
  20. * Path to the file with white-listed IP addresses
  21. *
  22. * @var string
  23. */
  24. protected $ipFile;
  25. /**
  26. * @var Status
  27. */
  28. protected $status;
  29. /**
  30. * Initialize.
  31. *
  32. * @param string|null $flagFile
  33. * @param string|null $ipFile
  34. * @param Status|null $status
  35. */
  36. public function __construct($flagFile = null, $ipFile = null, Status $status = null)
  37. {
  38. $this->flagFile = $flagFile ? $flagFile : MAGENTO_BP . '/var/.maintenance.flag';
  39. $this->ipFile = $ipFile ? $ipFile : MAGENTO_BP . '/var/.maintenance.ip';
  40. $this->status = $status ? $status : new Status();
  41. }
  42. /**
  43. * Check whether Magento maintenance mode is on.
  44. *
  45. * @return bool
  46. */
  47. public function isOn()
  48. {
  49. return file_exists($this->flagFile);
  50. }
  51. /**
  52. * Set maintenance mode.
  53. *
  54. * @param bool $isOn
  55. * @return $this
  56. * @throws \RuntimeException
  57. */
  58. public function set($isOn)
  59. {
  60. if ($isOn) {
  61. if (touch($this->flagFile)) {
  62. $this->status->add("Magento maintenance mode is enabled.", \Psr\Log\LogLevel::INFO);
  63. } else {
  64. throw new \RuntimeException("Magento maintenance mode cannot be enabled.");
  65. }
  66. } else if (file_exists($this->flagFile)) {
  67. if (file_exists($this->ipFile)) {
  68. /** Maintenance mode should not be unset from updater application if it was set manually by the admin */
  69. $this->status->add(
  70. "Magento maintenance mode was not disabled. It can be disabled from the Magento Backend.",
  71. \Psr\Log\LogLevel::INFO
  72. );
  73. } else if (unlink($this->flagFile)) {
  74. $this->status->add("Magento maintenance mode is disabled.", \Psr\Log\LogLevel::INFO);
  75. } else {
  76. throw new \RuntimeException("Magento maintenance mode cannot be disabled.");
  77. }
  78. }
  79. return $this;
  80. }
  81. }