Upgrader.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @package Grav\Common\GPM
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\GPM;
  9. use Grav\Common\GPM\Remote\GravCore;
  10. use InvalidArgumentException;
  11. /**
  12. * Class Upgrader
  13. *
  14. * @package Grav\Common\GPM
  15. */
  16. class Upgrader
  17. {
  18. /** @var GravCore Remote details about latest Grav version */
  19. private $remote;
  20. /** @var string|null */
  21. private $min_php;
  22. /**
  23. * Creates a new GPM instance with Local and Remote packages available
  24. *
  25. * @param boolean $refresh Applies to Remote Packages only and forces a refetch of data
  26. * @param callable|null $callback Either a function or callback in array notation
  27. * @throws InvalidArgumentException
  28. */
  29. public function __construct($refresh = false, $callback = null)
  30. {
  31. $this->remote = new Remote\GravCore($refresh, $callback);
  32. }
  33. /**
  34. * Returns the release date of the latest version of Grav
  35. *
  36. * @return string
  37. */
  38. public function getReleaseDate()
  39. {
  40. return $this->remote->getDate();
  41. }
  42. /**
  43. * Returns the version of the installed Grav
  44. *
  45. * @return string
  46. */
  47. public function getLocalVersion()
  48. {
  49. return GRAV_VERSION;
  50. }
  51. /**
  52. * Returns the version of the remotely available Grav
  53. *
  54. * @return string
  55. */
  56. public function getRemoteVersion()
  57. {
  58. return $this->remote->getVersion();
  59. }
  60. /**
  61. * Returns an array of assets available to download remotely
  62. *
  63. * @return array
  64. */
  65. public function getAssets()
  66. {
  67. return $this->remote->getAssets();
  68. }
  69. /**
  70. * Returns the changelog list for each version of Grav
  71. *
  72. * @param string|null $diff the version number to start the diff from
  73. * @return array return the changelog list for each version
  74. */
  75. public function getChangelog($diff = null)
  76. {
  77. return $this->remote->getChangelog($diff);
  78. }
  79. /**
  80. * Make sure this meets minimum PHP requirements
  81. *
  82. * @return bool
  83. */
  84. public function meetsRequirements()
  85. {
  86. if (version_compare(PHP_VERSION, $this->minPHPVersion(), '<')) {
  87. return false;
  88. }
  89. return true;
  90. }
  91. /**
  92. * Get minimum PHP version from remote
  93. *
  94. * @return string
  95. */
  96. public function minPHPVersion()
  97. {
  98. if (null === $this->min_php) {
  99. $this->min_php = $this->remote->getMinPHPVersion();
  100. }
  101. return $this->min_php;
  102. }
  103. /**
  104. * Checks if the currently installed Grav is upgradable to a newer version
  105. *
  106. * @return bool True if it's upgradable, False otherwise.
  107. */
  108. public function isUpgradable()
  109. {
  110. return version_compare($this->getLocalVersion(), $this->getRemoteVersion(), '<');
  111. }
  112. /**
  113. * Checks if Grav is currently symbolically linked
  114. *
  115. * @return bool True if Grav is symlinked, False otherwise.
  116. */
  117. public function isSymlink()
  118. {
  119. return $this->remote->isSymlink();
  120. }
  121. }