ChangeDetector.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Deploy\Model\DeploymentConfig\Hash\Generator as HashGenerator;
  8. /**
  9. * Configuration data changes detector.
  10. *
  11. * Detects changes in specific sections of the deployment configuration files.
  12. * This detector checks that configuration data from sections was not changed.
  13. */
  14. class ChangeDetector
  15. {
  16. /**
  17. * Hash storage.
  18. *
  19. * @var Hash
  20. */
  21. private $configHash;
  22. /**
  23. * Hash generator of config data.
  24. *
  25. * @var HashGenerator
  26. */
  27. private $hashGenerator;
  28. /**
  29. * Config data collector of specific sections.
  30. *
  31. * @var DataCollector
  32. */
  33. private $dataConfigCollector;
  34. /**
  35. * @param Hash $configHash The hash storage
  36. * @param HashGenerator $hashGenerator The hash generator of config data
  37. * @param DataCollector $dataConfigCollector The config data collector of specific sections
  38. */
  39. public function __construct(
  40. Hash $configHash,
  41. HashGenerator $hashGenerator,
  42. DataCollector $dataConfigCollector
  43. ) {
  44. $this->configHash = $configHash;
  45. $this->hashGenerator = $hashGenerator;
  46. $this->dataConfigCollector = $dataConfigCollector;
  47. }
  48. /**
  49. * Checks if config data in the deployment configuration files was changed.
  50. *
  51. * Checks if config data was changed based on its hash.
  52. * If the new hash of config data and the saved hash are different returns true.
  53. * If config data is empty always returns false.
  54. * In the other cases returns false.
  55. *
  56. * @param string $sectionName The section name for check data of the specific section
  57. * @return bool
  58. */
  59. public function hasChanges($sectionName = null)
  60. {
  61. $configs = $this->dataConfigCollector->getConfig($sectionName);
  62. $hashes = $this->configHash->get();
  63. foreach ($configs as $section => $config) {
  64. if (null === $config) {
  65. continue;
  66. }
  67. $savedHash = isset($hashes[$section]) ? $hashes[$section] : null;
  68. $generatedHash = empty($config) && !$savedHash ? null : $this->hashGenerator->generate($config);
  69. if ($generatedHash !== $savedHash) {
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. }