GravCore.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @package Grav\Common\GPM
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\GPM\Remote;
  9. use Grav\Common\Grav;
  10. use \Doctrine\Common\Cache\FilesystemCache;
  11. class GravCore extends AbstractPackageCollection
  12. {
  13. protected $repository = 'https://getgrav.org/downloads/grav.json';
  14. private $data;
  15. private $version;
  16. private $date;
  17. private $min_php;
  18. /**
  19. * @param bool $refresh
  20. * @param null $callback
  21. * @throws \InvalidArgumentException
  22. */
  23. public function __construct($refresh = false, $callback = null)
  24. {
  25. $channel = Grav::instance()['config']->get('system.gpm.releases', 'stable');
  26. $cache_dir = Grav::instance()['locator']->findResource('cache://gpm', true, true);
  27. $this->cache = new FilesystemCache($cache_dir);
  28. $this->repository .= '?v=' . GRAV_VERSION . '&' . $channel . '=1';
  29. $this->raw = $this->cache->fetch(md5($this->repository));
  30. $this->fetch($refresh, $callback);
  31. $this->data = json_decode($this->raw, true);
  32. $this->version = $this->data['version'] ?? '-';
  33. $this->date = $this->data['date'] ?? '-';
  34. $this->min_php = $this->data['min_php'] ?? null;
  35. if (isset($this->data['assets'])) {
  36. foreach ((array)$this->data['assets'] as $slug => $data) {
  37. $this->items[$slug] = new Package($data);
  38. }
  39. }
  40. }
  41. /**
  42. * Returns the list of assets associated to the latest version of Grav
  43. *
  44. * @return array list of assets
  45. */
  46. public function getAssets()
  47. {
  48. return $this->data['assets'];
  49. }
  50. /**
  51. * Returns the changelog list for each version of Grav
  52. *
  53. * @param string $diff the version number to start the diff from
  54. *
  55. * @return array changelog list for each version
  56. */
  57. public function getChangelog($diff = null)
  58. {
  59. if (!$diff) {
  60. return $this->data['changelog'];
  61. }
  62. $diffLog = [];
  63. foreach ((array)$this->data['changelog'] as $version => $changelog) {
  64. preg_match("/[\w\-\.]+/", $version, $cleanVersion);
  65. if (!$cleanVersion || version_compare($diff, $cleanVersion[0], '>=')) {
  66. continue;
  67. }
  68. $diffLog[$version] = $changelog;
  69. }
  70. return $diffLog;
  71. }
  72. /**
  73. * Return the release date of the latest Grav
  74. *
  75. * @return string
  76. */
  77. public function getDate()
  78. {
  79. return $this->date;
  80. }
  81. /**
  82. * Determine if this version of Grav is eligible to be updated
  83. *
  84. * @return mixed
  85. */
  86. public function isUpdatable()
  87. {
  88. return version_compare(GRAV_VERSION, $this->getVersion(), '<');
  89. }
  90. /**
  91. * Returns the latest version of Grav available remotely
  92. *
  93. * @return string
  94. */
  95. public function getVersion()
  96. {
  97. return $this->version;
  98. }
  99. /**
  100. * Returns the minimum PHP version
  101. *
  102. * @return null|string
  103. */
  104. public function getMinPHPVersion()
  105. {
  106. // If non min set, assume current PHP version
  107. if (null === $this->min_php) {
  108. $this->min_php = phpversion();
  109. }
  110. return $this->min_php;
  111. }
  112. /**
  113. * Is this installation symlinked?
  114. *
  115. * @return bool
  116. */
  117. public function isSymlink()
  118. {
  119. return is_link(GRAV_ROOT . DS . 'index.php');
  120. }
  121. }