Versions.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * @package Grav\Installer
  4. *
  5. * @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Installer;
  9. use Symfony\Component\Yaml\Yaml;
  10. use function is_array;
  11. use function is_string;
  12. /**
  13. * Grav Versions
  14. *
  15. * NOTE: This class can be initialized during upgrade from an older version of Grav. Make sure it runs there!
  16. */
  17. final class Versions
  18. {
  19. /** @var string */
  20. protected $filename;
  21. /** @var array */
  22. protected $items;
  23. /** @var bool */
  24. protected $updated = false;
  25. /** @var self[] */
  26. protected static $instance;
  27. /**
  28. * @param string|null $filename
  29. * @return self
  30. */
  31. public static function instance(string $filename = null): self
  32. {
  33. $filename = $filename ?? USER_DIR . 'config/versions.yaml';
  34. if (!isset(self::$instance[$filename])) {
  35. self::$instance[$filename] = new self($filename);
  36. }
  37. return self::$instance[$filename];
  38. }
  39. /**
  40. * @return bool True if the file was updated.
  41. */
  42. public function save(): bool
  43. {
  44. if (!$this->updated) {
  45. return false;
  46. }
  47. file_put_contents($this->filename, Yaml::dump($this->items, 4, 2));
  48. $this->updated = false;
  49. return true;
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function getAll(): array
  55. {
  56. return $this->items;
  57. }
  58. /**
  59. * @return array|null
  60. */
  61. public function getGrav(): ?array
  62. {
  63. return $this->get('core/grav');
  64. }
  65. /**
  66. * @return array
  67. */
  68. public function getPlugins(): array
  69. {
  70. return $this->get('plugins', []);
  71. }
  72. /**
  73. * @param string $name
  74. * @return array|null
  75. */
  76. public function getPlugin(string $name): ?array
  77. {
  78. return $this->get("plugins/{$name}");
  79. }
  80. /**
  81. * @return array
  82. */
  83. public function getThemes(): array
  84. {
  85. return $this->get('themes', []);
  86. }
  87. /**
  88. * @param string $name
  89. * @return array|null
  90. */
  91. public function getTheme(string $name): ?array
  92. {
  93. return $this->get("themes/{$name}");
  94. }
  95. /**
  96. * @param string $extension
  97. * @return array|null
  98. */
  99. public function getExtension(string $extension): ?array
  100. {
  101. return $this->get($extension);
  102. }
  103. /**
  104. * @param string $extension
  105. * @param array|null $value
  106. */
  107. public function setExtension(string $extension, ?array $value): void
  108. {
  109. if (null !== $value) {
  110. $this->set($extension, $value);
  111. } else {
  112. $this->undef($extension);
  113. }
  114. }
  115. /**
  116. * @param string $extension
  117. * @return string|null
  118. */
  119. public function getVersion(string $extension): ?string
  120. {
  121. $version = $this->get("{$extension}/version", null);
  122. return is_string($version) ? $version : null;
  123. }
  124. /**
  125. * @param string $extension
  126. * @param string|null $version
  127. */
  128. public function setVersion(string $extension, ?string $version): void
  129. {
  130. $this->updateHistory($extension, $version);
  131. }
  132. /**
  133. * NOTE: Updates also history.
  134. *
  135. * @param string $extension
  136. * @param string|null $version
  137. */
  138. public function updateVersion(string $extension, ?string $version): void
  139. {
  140. $this->set("{$extension}/version", $version);
  141. $this->updateHistory($extension, $version);
  142. }
  143. /**
  144. * @param string $extension
  145. * @return string|null
  146. */
  147. public function getSchema(string $extension): ?string
  148. {
  149. $version = $this->get("{$extension}/schema", null);
  150. return is_string($version) ? $version : null;
  151. }
  152. /**
  153. * @param string $extension
  154. * @param string|null $schema
  155. */
  156. public function setSchema(string $extension, ?string $schema): void
  157. {
  158. if (null !== $schema) {
  159. $this->set("{$extension}/schema", $schema);
  160. } else {
  161. $this->undef("{$extension}/schema");
  162. }
  163. }
  164. /**
  165. * @param string $extension
  166. * @return array
  167. */
  168. public function getHistory(string $extension): array
  169. {
  170. $name = "{$extension}/history";
  171. $history = $this->get($name, []);
  172. // Fix for broken Grav 1.6 history
  173. if ($extension === 'grav') {
  174. $history = $this->fixHistory($history);
  175. }
  176. return $history;
  177. }
  178. /**
  179. * @param string $extension
  180. * @param string|null $version
  181. */
  182. public function updateHistory(string $extension, ?string $version): void
  183. {
  184. $name = "{$extension}/history";
  185. $history = $this->getHistory($extension);
  186. $history[] = ['version' => $version, 'date' => gmdate('Y-m-d H:i:s')];
  187. $this->set($name, $history);
  188. }
  189. /**
  190. * Clears extension history. Useful when creating skeletons.
  191. *
  192. * @param string $extension
  193. */
  194. public function removeHistory(string $extension): void
  195. {
  196. $this->undef("{$extension}/history");
  197. }
  198. /**
  199. * @param array $history
  200. * @return array
  201. */
  202. private function fixHistory(array $history): array
  203. {
  204. if (isset($history['version'], $history['date'])) {
  205. $fix = [['version' => $history['version'], 'date' => $history['date']]];
  206. unset($history['version'], $history['date']);
  207. $history = array_merge($fix, $history);
  208. }
  209. return $history;
  210. }
  211. /**
  212. * Get value by using dot notation for nested arrays/objects.
  213. *
  214. * @param string $name Slash separated path to the requested value.
  215. * @param mixed $default Default value (or null).
  216. * @return mixed Value.
  217. */
  218. private function get(string $name, $default = null)
  219. {
  220. $path = explode('/', $name);
  221. $current = $this->items;
  222. foreach ($path as $field) {
  223. if (is_array($current) && isset($current[$field])) {
  224. $current = $current[$field];
  225. } else {
  226. return $default;
  227. }
  228. }
  229. return $current;
  230. }
  231. /**
  232. * Set value by using dot notation for nested arrays/objects.
  233. *
  234. * @param string $name Slash separated path to the requested value.
  235. * @param mixed $value New value.
  236. */
  237. private function set(string $name, $value): void
  238. {
  239. $path = explode('/', $name);
  240. $current = &$this->items;
  241. foreach ($path as $field) {
  242. // Handle arrays and scalars.
  243. if (!is_array($current)) {
  244. $current = [$field => []];
  245. } elseif (!isset($current[$field])) {
  246. $current[$field] = [];
  247. }
  248. $current = &$current[$field];
  249. }
  250. $current = $value;
  251. $this->updated = true;
  252. }
  253. /**
  254. * Unset value by using dot notation for nested arrays/objects.
  255. *
  256. * @param string $name Dot separated path to the requested value.
  257. */
  258. private function undef(string $name): void
  259. {
  260. $path = $name !== '' ? explode('/', $name) : [];
  261. if (!$path) {
  262. return;
  263. }
  264. $var = array_pop($path);
  265. $current = &$this->items;
  266. foreach ($path as $field) {
  267. if (!is_array($current) || !isset($current[$field])) {
  268. return;
  269. }
  270. $current = &$current[$field];
  271. }
  272. unset($current[$var]);
  273. $this->updated = true;
  274. }
  275. private function __construct(string $filename)
  276. {
  277. $this->filename = $filename;
  278. $content = is_file($filename) ? file_get_contents($filename) : null;
  279. if (false === $content) {
  280. throw new \RuntimeException('Versions file cannot be read');
  281. }
  282. $this->items = $content ? Yaml::parse($content) : [];
  283. }
  284. }