|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Fc2blog\Model; |
| 5 | + |
| 6 | +use JsonException; |
| 7 | +use RuntimeException; |
| 8 | + |
| 9 | +class SystemUpdateModel |
| 10 | +{ |
| 11 | + /** @var string */ |
| 12 | + static public $releases_url = "https://www.github.com/uzulla/fc2blog/releases"; // TODO: change to fc2blog/blog |
| 13 | + /** @var string */ |
| 14 | +// static public $releases_api_url = "https://api.github.com/repos/uzulla/tag-release-test/releases"; // TODO: change to fc2blog/blog |
| 15 | + static public $releases_api_url = "https://api.github.com/repos/uzulla/fc2blog/releases"; // TODO: change to fc2blog/blog |
| 16 | + |
| 17 | + /** |
| 18 | + * Get release info list from GitHub. |
| 19 | + */ |
| 20 | + public static function getReleaseInfo(): ?array |
| 21 | + { |
| 22 | + // TODO cache |
| 23 | + // app/temp/releases.json の最終更新時刻をみて、1h以内ならそれを利用する |
| 24 | + |
| 25 | + $options = ['http' => ['header' => "User-Agent: fc2blog_installer"]]; |
| 26 | + |
| 27 | + $releases_json = @file_get_contents( |
| 28 | + static::$releases_api_url, |
| 29 | + false, |
| 30 | + stream_context_create($options) |
| 31 | + ); |
| 32 | + |
| 33 | + // rate limit等 |
| 34 | + $pos = strpos($http_response_header[0], '403'); |
| 35 | + if ($pos !== false) { |
| 36 | + var_dump($http_response_header); |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + $pos = strpos($http_response_header[0], '200'); |
| 41 | + if ($pos === false) { |
| 42 | + throw new RuntimeException("api request failed: status code {$http_response_header[0]}"); |
| 43 | + } |
| 44 | + |
| 45 | + if ($releases_json === false) { |
| 46 | + throw new RuntimeException("api request failed"); |
| 47 | + } |
| 48 | + |
| 49 | + try { |
| 50 | + $releases_data = json_decode($releases_json, true, 512, JSON_THROW_ON_ERROR); |
| 51 | + } catch (JsonException $e) { |
| 52 | + throw new RuntimeException("json parse failed:{$e->getMessage()} on {$e->getFile()}:{$e->getLine()}"); |
| 53 | + } |
| 54 | + |
| 55 | + // app/temp/releases.json にjsonを書き出す |
| 56 | + |
| 57 | + return $releases_data; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get download url of `fc2blog_dist.zip` in assets from release array. |
| 62 | + * @param array $release |
| 63 | + * @return string|null |
| 64 | + */ |
| 65 | + public static function getZipDownloadUrl(array $release): ?string |
| 66 | + { |
| 67 | + if (!isset($release['assets'])) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + $found_asset = null; |
| 72 | + foreach ($release['assets'] as $release_asset) { |
| 73 | + if ($release_asset['name'] === 'fc2blog_dist.zip') { |
| 74 | + $found_asset = $release_asset; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (!is_array($found_asset) || !isset($found_asset['browser_download_url'])) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + return $found_asset['browser_download_url']; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get latest release that has vX.X.X tag from release list. |
| 87 | + * @param $release_list |
| 88 | + * @return array|null |
| 89 | + */ |
| 90 | + public static function getValidLatestRelease($release_list): ?array |
| 91 | + { |
| 92 | + foreach ($release_list as $release) { |
| 93 | + if (preg_match("/\Av[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\\z/", $release['tag_name'])) { |
| 94 | + if (isset($release['assets']) && is_array($release['assets']) && count($release['assets']) > 0) { |
| 95 | + return $release; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments