PatchManager.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\Configuration\Addon\Update;
  3. use ModulesGarden\ProxmoxAddon\Core\ModuleConstants;
  4. use ModulesGarden\ProxmoxAddon\Core\DependencyInjection;
  5. use ModulesGarden\ProxmoxAddon\Core\ServiceLocator;
  6. /**
  7. * Description of PatchManager
  8. *
  9. * @author Rafał Ossowski <rafal.os@modulesgarden.com>
  10. */
  11. class PatchManager
  12. {
  13. protected $updatePath;
  14. protected $updateFiles = [];
  15. public function __construct()
  16. {
  17. $this->updatePath = ModuleConstants::getModuleRootDir() . DS . "app" . DS . "Configuration" . DS . "Addon" . DS . "Update" . DS . "Patch";
  18. $this->loadUpdatePath();
  19. }
  20. public function run($newVersion, $oldVersion)
  21. {
  22. $fullPath = $this->getUpdatePath();
  23. array_map(function ($version, $fileName) use($newVersion, $oldVersion, $fullPath)
  24. {
  25. if ($this->checkVersion($newVersion, $oldVersion, $version))
  26. {
  27. try
  28. {
  29. $className = ModuleConstants::getRootNamespace() . "\App\Configuration\Addon\Update\Patch\\" . $fileName;
  30. if(method_exists($className,'setVersion')){
  31. DependencyInjection::create($className)->setVersion($version)->execute();
  32. }
  33. }
  34. catch (\Exception $ex)
  35. {
  36. ServiceLocator::call("errorManager")
  37. ->addError(
  38. self::class, $ex->getMessage(), [
  39. "newVersion" => $newVersion,
  40. "oldVersion" => $oldVersion,
  41. "updateVersion" => $version,
  42. "fullFileName" => $fullPath . DS . $fileName . ".php"
  43. ]
  44. );
  45. }
  46. }
  47. }, array_keys($this->getUpdateFiles()), $this->getUpdateFiles()
  48. );
  49. return $this;
  50. }
  51. protected function checkVersion($newVersion, $oldVersion, $fileVersion)
  52. {
  53. if (version_compare($oldVersion, $fileVersion, "<"))
  54. {
  55. return true;
  56. }
  57. return false;
  58. }
  59. protected function getUpdatePath()
  60. {
  61. return $this->updatePath;
  62. }
  63. protected function getUpdateFiles()
  64. {
  65. return $this->updateFiles;
  66. }
  67. protected function loadUpdatePath()
  68. {
  69. $files = scandir($this->getUpdatePath(), 1);
  70. if (count($files) != 0)
  71. {
  72. foreach ($files as $file)
  73. {
  74. if ($file !== "." && $file !== "..")
  75. {
  76. $version = $this->generateVersion($file);
  77. $this->updateFiles[$version] = explode(".", $file)[0];
  78. }
  79. }
  80. }
  81. }
  82. protected function generateVersion($fileName)
  83. {
  84. $name = explode('.', $fileName)[0];
  85. return str_replace(["M", "P"], ".", substr($name, 1));
  86. }
  87. }