PatchBackwardCompatability.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\Setup\Patch;
  8. use Magento\Framework\Module\ModuleResource;
  9. /**
  10. * This class is information expert in questions of backward compatibility in data and schema patches
  11. */
  12. class PatchBackwardCompatability
  13. {
  14. /**
  15. * @var ModuleResource
  16. */
  17. private $moduleResource;
  18. /**
  19. * @param ModuleResource $moduleResource
  20. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  21. * @SuppressWarnings(Magento.TypeDuplication)
  22. */
  23. public function __construct(ModuleResource $moduleResource)
  24. {
  25. $this->moduleResource = $moduleResource;
  26. }
  27. /**
  28. * Check is patch skipable by data setup version in DB
  29. *
  30. * @param string $patchClassName
  31. * @param string $moduleName
  32. * @return bool
  33. */
  34. public function isSkipableByDataSetupVersion(string $patchClassName, string $moduleName) : bool
  35. {
  36. $dbVersion = (string) $this->moduleResource->getDataVersion($moduleName);
  37. return in_array(PatchVersionInterface::class, class_implements($patchClassName)) &&
  38. version_compare(call_user_func([$patchClassName, 'getVersion']), $dbVersion) <= 0;
  39. }
  40. /**
  41. * Check is patch skipable by schema setup version in DB
  42. *
  43. * @param string $patchClassName
  44. * @param string $moduleName
  45. * @return bool
  46. */
  47. public function isSkipableBySchemaSetupVersion(string $patchClassName, string $moduleName) : bool
  48. {
  49. $dbVersion = (string) $this->moduleResource->getDbVersion($moduleName);
  50. return in_array(PatchVersionInterface::class, class_implements($patchClassName)) &&
  51. version_compare(call_user_func([$patchClassName, 'getVersion']), $dbVersion) <= 0;
  52. }
  53. }