VersionUpdater.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace Grav\Installer;
  3. use DirectoryIterator;
  4. /**
  5. * Class VersionUpdater
  6. * @package Grav\Installer
  7. */
  8. final class VersionUpdater
  9. {
  10. /** @var string */
  11. private $name;
  12. /** @var string */
  13. private $path;
  14. /** @var string */
  15. private $version;
  16. /** @var Versions */
  17. private $versions;
  18. /** @var VersionUpdate[] */
  19. private $updates;
  20. /**
  21. * VersionUpdater constructor.
  22. * @param string $name
  23. * @param string $path
  24. * @param string $version
  25. * @param Versions $versions
  26. */
  27. public function __construct(string $name, string $path, string $version, Versions $versions)
  28. {
  29. $this->name = $name;
  30. $this->path = $path;
  31. $this->version = $version;
  32. $this->versions = $versions;
  33. $this->loadUpdates();
  34. }
  35. /**
  36. * Pre-installation method.
  37. */
  38. public function preflight(): void
  39. {
  40. foreach ($this->updates as $revision => $update) {
  41. $update->preflight($this);
  42. }
  43. }
  44. /**
  45. * Install method.
  46. */
  47. public function install(): void
  48. {
  49. $versions = $this->getVersions();
  50. $versions->updateVersion($this->name, $this->version);
  51. $versions->save();
  52. }
  53. /**
  54. * Post-installation method.
  55. */
  56. public function postflight(): void
  57. {
  58. $versions = $this->getVersions();
  59. foreach ($this->updates as $revision => $update) {
  60. $update->postflight($this);
  61. $versions->setSchema($this->name, $revision);
  62. $versions->save();
  63. }
  64. }
  65. /**
  66. * @return Versions
  67. */
  68. public function getVersions(): Versions
  69. {
  70. return $this->versions;
  71. }
  72. /**
  73. * @param string|null $name
  74. * @return string|null
  75. */
  76. public function getExtensionVersion(string $name = null): ?string
  77. {
  78. return $this->versions->getVersion($name ?? $this->name);
  79. }
  80. /**
  81. * @param string|null $name
  82. * @return string|null
  83. */
  84. public function getExtensionSchema(string $name = null): ?string
  85. {
  86. return $this->versions->getSchema($name ?? $this->name);
  87. }
  88. /**
  89. * @param string|null $name
  90. * @return array
  91. */
  92. public function getExtensionHistory(string $name = null): array
  93. {
  94. return $this->versions->getHistory($name ?? $this->name);
  95. }
  96. protected function loadUpdates(): void
  97. {
  98. $this->updates = [];
  99. $schema = $this->getExtensionSchema();
  100. $iterator = new DirectoryIterator($this->path);
  101. foreach ($iterator as $item) {
  102. if (!$item->isFile() || $item->getExtension() !== 'php') {
  103. continue;
  104. }
  105. $revision = $item->getBasename('.php');
  106. if (!$schema || version_compare($revision, $schema, '>')) {
  107. $realPath = $item->getRealPath();
  108. if ($realPath) {
  109. $this->updates[$revision] = new VersionUpdate($realPath, $this);
  110. }
  111. }
  112. }
  113. uksort($this->updates, 'version_compare');
  114. }
  115. }