Grav.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Grav\Common\GPM\Remote;
  3. use \Doctrine\Common\Cache\FilesystemCache;
  4. class Grav extends AbstractPackageCollection
  5. {
  6. protected $repository = 'http://getgrav.org/downloads/grav.json';
  7. private $data;
  8. private $version;
  9. private $date;
  10. /**
  11. * @param bool $refresh
  12. * @param null $callback
  13. */
  14. public function __construct($refresh = false, $callback = null)
  15. {
  16. $cache_dir = self::getGrav()['locator']->findResource('cache://gpm', true, true);
  17. $this->cache = new FilesystemCache($cache_dir);
  18. $this->raw = $this->cache->fetch(md5($this->repository));
  19. $this->fetch($refresh, $callback);
  20. $this->data = json_decode($this->raw, true);
  21. $this->version = isset($this->data['version']) ? $this->data['version'] : '-';
  22. $this->date = isset($this->data['date']) ? $this->data['date'] : '-';
  23. if (isset($this->data['assets'])) foreach ($this->data['assets'] as $slug => $data) {
  24. $this->items[$slug] = new Package($data);
  25. }
  26. }
  27. /**
  28. * Returns the list of assets associated to the latest version of Grav
  29. * @return array list of assets
  30. */
  31. public function getAssets()
  32. {
  33. return $this->data['assets'];
  34. }
  35. /**
  36. * Returns the changelog list for each version of Grav
  37. * @param string $diff the version number to start the diff from
  38. *
  39. * @return array changelog list for each version
  40. */
  41. public function getChangelog($diff = null)
  42. {
  43. if (!$diff) {
  44. return $this->data['changelog'];
  45. }
  46. $diffLog = [];
  47. foreach ($this->data['changelog'] as $version => $changelog) {
  48. preg_match("/[\d\.]+/", $version, $cleanVersion);
  49. if (!$cleanVersion || version_compare($diff, $cleanVersion[0], ">=")) { continue; }
  50. $diffLog[$version] = $changelog;
  51. }
  52. return $diffLog;
  53. }
  54. /**
  55. * Returns the latest version of Grav available remotely
  56. * @return string
  57. */
  58. public function getVersion()
  59. {
  60. return $this->version;
  61. }
  62. /**
  63. * Return the release date of the latest Grav
  64. * @return string
  65. */
  66. public function getDate()
  67. {
  68. return $this->date;
  69. }
  70. public function isUpdatable()
  71. {
  72. return version_compare(GRAV_VERSION, $this->getVersion(), '<');
  73. }
  74. public function isSymlink()
  75. {
  76. return is_link(GRAV_ROOT . DS . 'index.php');
  77. }
  78. }