PatchManager.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\Configuration\Addon\Update;
  3. use ThurData\Servers\KerioEmail\Core\ModuleConstants;
  4. use ThurData\Servers\KerioEmail\Core\DependencyInjection;
  5. use ThurData\Servers\KerioEmail\Core\ServiceLocator;
  6. /**
  7. * Description of PatchManager
  8. *
  9. * @autor ThurData <info@thurdata.ch>
  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. DependencyInjection::create($className)->setVersion($version)->execute();
  31. }
  32. catch (\Exception $exc)
  33. {
  34. ServiceLocator::call("errorManager")
  35. ->addError(
  36. self::class,
  37. $exc->getMessage(),
  38. [
  39. "newVersion" => $newVersion,
  40. "oldVersion" => $oldVersion,
  41. "updateVersion" => $version,
  42. "fullFileName" => $fullPath . DS . $fileName . ".php"
  43. ]
  44. );
  45. }
  46. }
  47. },
  48. array_keys($this->getUpdateFiles()),
  49. $this->getUpdateFiles()
  50. );
  51. return $this;
  52. }
  53. protected function checkVersion($newVersion, $oldVersion, $fileVersion)
  54. {
  55. if (version_compare($oldVersion, $fileVersion, "<"))
  56. {
  57. return true;
  58. }
  59. return false;
  60. }
  61. protected function getUpdatePath()
  62. {
  63. return $this->updatePath;
  64. }
  65. protected function getUpdateFiles()
  66. {
  67. return $this->updateFiles;
  68. }
  69. protected function loadUpdatePath()
  70. {
  71. $files = scandir($this->getUpdatePath(), 1);
  72. if (count($files) != 0)
  73. {
  74. foreach ($files as $file)
  75. {
  76. if ($file !== "." && $file !== "..")
  77. {
  78. $version = $this->generateVersion($file);
  79. $this->updateFiles[$version] = explode(".", $file)[0];
  80. }
  81. }
  82. }
  83. }
  84. protected function generateVersion($fileName)
  85. {
  86. $name = explode('.', $fileName)[0];
  87. return str_replace(["M","P"],".",substr($name,1));
  88. }
  89. }