GPM.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. namespace Grav\Common\GPM;
  3. use Grav\Common\Inflector;
  4. use Grav\Common\Iterator;
  5. use Grav\Common\Utils;
  6. class GPM extends Iterator
  7. {
  8. /**
  9. * Local installed Packages
  10. * @var Local\Packages
  11. */
  12. private $installed;
  13. /**
  14. * Remote available Packages
  15. * @var Remote\Packages
  16. */
  17. private $repository;
  18. /**
  19. * @var Remote\Grav
  20. */
  21. public $grav;
  22. /**
  23. * Internal cache
  24. * @var
  25. */
  26. protected $cache;
  27. protected $install_paths = ['plugins' => 'user/plugins/%name%', 'themes' => 'user/themes/%name%', 'skeletons' => 'user/'];
  28. /**
  29. * Creates a new GPM instance with Local and Remote packages available
  30. * @param boolean $refresh Applies to Remote Packages only and forces a refetch of data
  31. * @param callable $callback Either a function or callback in array notation
  32. */
  33. public function __construct($refresh = false, $callback = null)
  34. {
  35. $this->installed = new Local\Packages();
  36. try {
  37. $this->repository = new Remote\Packages($refresh, $callback);
  38. $this->grav = new Remote\Grav($refresh, $callback);
  39. } catch (\Exception $e) {
  40. }
  41. }
  42. /**
  43. * Returns the Locally installed packages
  44. * @return Iterator The installed packages
  45. */
  46. public function getInstalled()
  47. {
  48. return $this->installed;
  49. }
  50. /**
  51. * Returns the amount of locally installed packages
  52. * @return integer Amount of installed packages
  53. */
  54. public function countInstalled()
  55. {
  56. $installed = $this->getInstalled();
  57. return count($installed['plugins']) + count($installed['themes']);
  58. }
  59. /**
  60. * Return the instance of a specific Plugin
  61. * @param string $slug The slug of the Plugin
  62. * @return Local\Package The instance of the Plugin
  63. */
  64. public function getInstalledPlugin($slug)
  65. {
  66. return $this->installed['plugins'][$slug];
  67. }
  68. /**
  69. * Returns the Locally installed plugins
  70. * @return Iterator The installed plugins
  71. */
  72. public function getInstalledPlugins()
  73. {
  74. return $this->installed['plugins'];
  75. }
  76. /**
  77. * Checks if a Plugin is installed
  78. * @param string $slug The slug of the Plugin
  79. * @return boolean True if the Plugin has been installed. False otherwise
  80. */
  81. public function isPluginInstalled($slug)
  82. {
  83. return isset($this->installed['plugins'][$slug]);
  84. }
  85. /**
  86. * Return the instance of a specific Theme
  87. * @param string $slug The slug of the Theme
  88. * @return Local\Package The instance of the Theme
  89. */
  90. public function getInstalledTheme($slug)
  91. {
  92. return $this->installed['themes'][$slug];
  93. }
  94. /**
  95. * Returns the Locally installed themes
  96. * @return Iterator The installed themes
  97. */
  98. public function getInstalledThemes()
  99. {
  100. return $this->installed['themes'];
  101. }
  102. /**
  103. * Checks if a Theme is installed
  104. * @param string $slug The slug of the Theme
  105. * @return boolean True if the Theme has been installed. False otherwise
  106. */
  107. public function isThemeInstalled($slug)
  108. {
  109. return isset($this->installed['themes'][$slug]);
  110. }
  111. /**
  112. * Returns the amount of updates available
  113. * @return integer Amount of available updates
  114. */
  115. public function countUpdates()
  116. {
  117. $count = 0;
  118. $count += count($this->getUpdatablePlugins());
  119. $count += count($this->getUpdatableThemes());
  120. return $count;
  121. }
  122. /**
  123. * Returns an array of Plugins and Themes that can be updated.
  124. * Plugins and Themes are extended with the `available` property that relies to the remote version
  125. * @return array Array of updatable Plugins and Themes.
  126. * Format: ['total' => int, 'plugins' => array, 'themes' => array]
  127. */
  128. public function getUpdatable()
  129. {
  130. $plugins = $this->getUpdatablePlugins();
  131. $themes = $this->getUpdatableThemes();
  132. $items = [
  133. 'total' => count($plugins)+count($themes),
  134. 'plugins' => $plugins,
  135. 'themes' => $themes
  136. ];
  137. return $items;
  138. }
  139. /**
  140. * Returns an array of Plugins that can be updated.
  141. * The Plugins are extended with the `available` property that relies to the remote version
  142. * @return Iterator Array of updatable Plugins
  143. */
  144. public function getUpdatablePlugins()
  145. {
  146. $items = [];
  147. $repository = $this->repository['plugins'];
  148. // local cache to speed things up
  149. if (isset($this->cache[__METHOD__])) {
  150. return $this->cache[__METHOD__];
  151. }
  152. foreach ($this->installed['plugins'] as $slug => $plugin) {
  153. if (!isset($repository[$slug]) || $plugin->symlink) {
  154. continue;
  155. }
  156. $local_version = $plugin->version ? $plugin->version : 'Unknown';
  157. $remote_version = $repository[$slug]->version;
  158. if (version_compare($local_version, $remote_version) < 0) {
  159. $repository[$slug]->available = $remote_version;
  160. $repository[$slug]->version = $local_version;
  161. $items[$slug] = $repository[$slug];
  162. }
  163. }
  164. $this->cache[__METHOD__] = $items;
  165. return $items;
  166. }
  167. /**
  168. * Check if a Plugin or Theme is updatable
  169. * @param string $slug The slug of the package
  170. * @return boolean True if updatable. False otherwise or if not found
  171. */
  172. public function isUpdatable($slug)
  173. {
  174. return $this->isPluginUpdatable($slug) || $this->isThemeUpdatable($slug);
  175. }
  176. /**
  177. * Checks if a Plugin is updatable
  178. * @param string $plugin The slug of the Plugin
  179. * @return boolean True if the Plugin is updatable. False otherwise
  180. */
  181. public function isPluginUpdatable($plugin)
  182. {
  183. return array_key_exists($plugin, (array) $this->getUpdatablePlugins());
  184. }
  185. /**
  186. * Returns an array of Themes that can be updated.
  187. * The Themes are extended with the `available` property that relies to the remote version
  188. * @return Iterator Array of updatable Themes
  189. */
  190. public function getUpdatableThemes()
  191. {
  192. $items = [];
  193. $repository = $this->repository['themes'];
  194. // local cache to speed things up
  195. if (isset($this->cache[__METHOD__])) {
  196. return $this->cache[__METHOD__];
  197. }
  198. foreach ($this->installed['themes'] as $slug => $plugin) {
  199. if (!isset($repository[$slug]) || $plugin->symlink) {
  200. continue;
  201. }
  202. $local_version = $plugin->version ? $plugin->version : 'Unknown';
  203. $remote_version = $repository[$slug]->version;
  204. if (version_compare($local_version, $remote_version) < 0) {
  205. $repository[$slug]->available = $remote_version;
  206. $repository[$slug]->version = $local_version;
  207. $items[$slug] = $repository[$slug];
  208. }
  209. }
  210. $this->cache[__METHOD__] = $items;
  211. return $items;
  212. }
  213. /**
  214. * Checks if a Theme is Updatable
  215. * @param string $theme The slug of the Theme
  216. * @return boolean True if the Theme is updatable. False otherwise
  217. */
  218. public function isThemeUpdatable($theme)
  219. {
  220. return array_key_exists($theme, (array) $this->getUpdatableThemes());
  221. }
  222. /**
  223. * Returns a Plugin from the repository
  224. * @param string $slug The slug of the Plugin
  225. * @return mixed Package if found, NULL if not
  226. */
  227. public function getRepositoryPlugin($slug)
  228. {
  229. return @$this->repository['plugins'][$slug];
  230. }
  231. /**
  232. * Returns the list of Plugins available in the repository
  233. * @return Iterator The Plugins remotely available
  234. */
  235. public function getRepositoryPlugins()
  236. {
  237. return $this->repository['plugins'];
  238. }
  239. /**
  240. * Returns a Theme from the repository
  241. * @param string $slug The slug of the Theme
  242. * @return mixed Package if found, NULL if not
  243. */
  244. public function getRepositoryTheme($slug)
  245. {
  246. return @$this->repository['themes'][$slug];
  247. }
  248. /**
  249. * Returns the list of Themes available in the repository
  250. * @return Iterator The Themes remotely available
  251. */
  252. public function getRepositoryThemes()
  253. {
  254. return $this->repository['themes'];
  255. }
  256. /**
  257. * Returns the list of Plugins and Themes available in the repository
  258. * @return array Array of available Plugins and Themes
  259. * Format: ['plugins' => array, 'themes' => array]
  260. */
  261. public function getRepository()
  262. {
  263. return $this->repository;
  264. }
  265. /**
  266. * Searches for a Package in the repository
  267. * @param string $search Can be either the slug or the name
  268. * @return Remote\Package Package if found, FALSE if not
  269. */
  270. public function findPackage($search)
  271. {
  272. $search = strtolower($search);
  273. if ($found = $this->getRepositoryTheme($search)) {
  274. return $found;
  275. }
  276. if ($found = $this->getRepositoryPlugin($search)) {
  277. return $found;
  278. }
  279. foreach ($this->getRepositoryThemes() as $slug => $theme) {
  280. if ($search == $slug || $search == $theme->name) {
  281. return $theme;
  282. }
  283. }
  284. foreach ($this->getRepositoryPlugins() as $slug => $plugin) {
  285. if ($search == $slug || $search == $plugin->name) {
  286. return $plugin;
  287. }
  288. }
  289. return false;
  290. }
  291. /**
  292. * Returns the list of Plugins and Themes available in the repository
  293. * @return array Array of available Plugins and Themes
  294. * Format: ['plugins' => array, 'themes' => array]
  295. */
  296. /**
  297. * Searches for a list of Packages in the repository
  298. * @param array $searches An array of either slugs or names
  299. * @return array Array of found Packages
  300. * Format: ['total' => int, 'not_found' => array, <found-slugs>]
  301. */
  302. public function findPackages($searches = [])
  303. {
  304. $packages = ['total' => 0, 'not_found' => []];
  305. $inflector = new Inflector();
  306. foreach ($searches as $search) {
  307. $repository = '';
  308. // if this is an object, get the search data from the key
  309. if (is_object($search)) {
  310. $search = (array) $search;
  311. $key = key($search);
  312. $repository = $search[$key];
  313. $search = $key;
  314. }
  315. if ($found = $this->findPackage($search)) {
  316. // set override repository if provided
  317. if ($repository) {
  318. $found->override_repository = $repository;
  319. }
  320. if (!isset($packages[$found->package_type])) {
  321. $packages[$found->package_type] = [];
  322. }
  323. $packages[$found->package_type][$found->slug] = $found;
  324. $packages['total']++;
  325. } else {
  326. // make a best guess at the type based on the repo URL
  327. if (Utils::contains($repository, '-theme')) {
  328. $type = 'themes';
  329. } else {
  330. $type = 'plugins';
  331. }
  332. $not_found = new \stdClass();
  333. $not_found->name = $inflector->camelize($search);
  334. $not_found->slug = $search;
  335. $not_found->package_type = $type;
  336. $not_found->install_path = str_replace('%name%', $search, $this->install_paths[$type]);
  337. $not_found->override_repository = $repository;
  338. $packages['not_found'][$search] = $not_found;
  339. }
  340. }
  341. return $packages;
  342. }
  343. }