Hash.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Model\DeploymentConfig;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\Flag;
  9. use Magento\Framework\Flag\FlagResource;
  10. use Magento\Framework\FlagFactory;
  11. /**
  12. * Saves and Retrieves deployment configuration hash.
  13. *
  14. * This hash keeps version of last imported data. Hash is used to define whether data was updated
  15. * and import is required.
  16. *
  17. * @see \Magento\Deploy\Model\DeploymentConfig\ChangeDetector::hasChanges()
  18. */
  19. class Hash
  20. {
  21. /**
  22. * Name of the section where deployment configuration hash is stored.
  23. */
  24. const CONFIG_KEY = 'config_hash';
  25. /**
  26. * Hash generator.
  27. *
  28. * @var Hash\Generator
  29. */
  30. private $configHashGenerator;
  31. /**
  32. * Config data collector.
  33. *
  34. * @var DataCollector
  35. */
  36. private $dataConfigCollector;
  37. /**
  38. * Flag Resource model.
  39. *
  40. * @var FlagResource
  41. */
  42. private $flagResource;
  43. /**
  44. * Factory class for \Magento\Framework\Flag
  45. *
  46. * @var FlagFactory
  47. */
  48. private $flagFactory;
  49. /**
  50. * @param Hash\Generator $configHashGenerator the hash generator
  51. * @param DataCollector $dataConfigCollector the config data collector
  52. * @param FlagResource $flagResource
  53. * @param FlagFactory $flagFactory
  54. */
  55. public function __construct(
  56. Hash\Generator $configHashGenerator,
  57. DataCollector $dataConfigCollector,
  58. FlagResource $flagResource,
  59. FlagFactory $flagFactory
  60. ) {
  61. $this->configHashGenerator = $configHashGenerator;
  62. $this->dataConfigCollector = $dataConfigCollector;
  63. $this->flagResource = $flagResource;
  64. $this->flagFactory = $flagFactory;
  65. }
  66. /**
  67. * Updates hash in the storage.
  68. *
  69. * If the specific section name is set, then hash will be updated only for this section,
  70. * in another case hash will be updated for all sections which defined in di.xml
  71. * The hash is generated based on data from configuration files.
  72. *
  73. * @param string $sectionName the specific section name
  74. * @return void
  75. * @throws LocalizedException is thrown when hash was not saved
  76. */
  77. public function regenerate($sectionName = null)
  78. {
  79. try {
  80. $hashes = $this->get();
  81. $configs = $this->dataConfigCollector->getConfig($sectionName);
  82. foreach ($configs as $section => $config) {
  83. $hashes[$section] = $this->configHashGenerator->generate($config);
  84. }
  85. /** @var Flag $flag */
  86. $flag = $this->getFlagObject();
  87. $flag->setFlagData($hashes);
  88. $this->flagResource->save($flag);
  89. } catch (\Exception $exception) {
  90. throw new LocalizedException(__("The hash isn't saved."), $exception);
  91. }
  92. }
  93. /**
  94. * Retrieves saved hashes from storage.
  95. *
  96. * @return array
  97. */
  98. public function get()
  99. {
  100. /** @var Flag $flag */
  101. $flag = $this->getFlagObject();
  102. return (array) ($flag->getFlagData() ?: []);
  103. }
  104. /**
  105. * Returns flag object.
  106. *
  107. * We use it for saving hashes of sections in the DB.
  108. *
  109. * @return Flag
  110. */
  111. private function getFlagObject()
  112. {
  113. /** @var Flag $flag */
  114. $flag = $this->flagFactory
  115. ->create(['data' => ['flag_code' => self::CONFIG_KEY]]);
  116. $this->flagResource->load($flag, self::CONFIG_KEY, 'flag_code');
  117. return $flag;
  118. }
  119. }