1 | <?php |
||
2 | |||
3 | namespace Apps\Model\Admin\Main; |
||
4 | |||
5 | use Apps\ActiveRecord\System; |
||
6 | use Extend\Version; |
||
7 | use Ffcms\Core\Arch\Model; |
||
8 | use Ffcms\Core\Helper\Date; |
||
9 | use Ffcms\Core\Helper\FileSystem\File; |
||
10 | use Ffcms\Core\Helper\Type\Any; |
||
11 | use Ffcms\Core\Helper\Type\Arr; |
||
12 | |||
13 | /** |
||
14 | * Class EntityUpdate. Update entity business logic - update checks & statistic collection |
||
15 | * @package Apps\Model\Admin\Main |
||
16 | */ |
||
17 | class EntityUpdate extends Model |
||
18 | { |
||
19 | const API_LATEST_RELEASE = 'https://api.github.com/repos/phpffcms/ffcms/releases/latest'; |
||
20 | public static $apiZipTypes = [ |
||
21 | 'application/zip', |
||
22 | 'application/x-zip-compressed' |
||
23 | ]; |
||
24 | |||
25 | public $scriptVersion; |
||
26 | public $dbVersion; |
||
27 | public $versionsEqual = false; |
||
28 | |||
29 | public $lastVersion; |
||
30 | public $lastInfo = [ |
||
31 | 'name' => null, |
||
32 | 'created_at' => null, |
||
33 | 'download_url' => null |
||
34 | ]; |
||
35 | |||
36 | public $haveRemoteNew = false; |
||
37 | |||
38 | /** |
||
39 | * Set default model properties |
||
40 | */ |
||
41 | public function before() |
||
42 | { |
||
43 | $this->scriptVersion = Version::VERSION; |
||
44 | $this->dbVersion = System::getVar('version')['data']; |
||
45 | |||
46 | $this->versionsEqual = (version_compare($this->scriptVersion, $this->dbVersion) === 0); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
47 | $this->findLatestVersion(); |
||
48 | $this->haveRemoteNew = ($this->lastVersion !== null && version_compare($this->scriptVersion, $this->lastVersion) === -1); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Find latest release in github API and get required info |
||
53 | */ |
||
54 | private function findLatestVersion() |
||
55 | { |
||
56 | // get remote api with json response |
||
57 | $gitJson = File::getFromUrl(static::API_LATEST_RELEASE); |
||
58 | if (!$gitJson) { |
||
59 | return; |
||
60 | } |
||
61 | |||
62 | // parse api response to model attributes |
||
63 | $git = json_decode($gitJson, true); |
||
64 | $this->lastVersion = $git['tag_name']; |
||
65 | // get download url to full compiled distributive (uploaded to each release as .zip archive, placed in release.assets) |
||
66 | $download = null; |
||
67 | if (Any::isArray($git['assets'])) { |
||
68 | foreach ($git['assets'] as $asset) { |
||
69 | if (Arr::in($asset['content_type'], static::$apiZipTypes) && $asset['state'] === 'uploaded') { |
||
70 | $download = $asset['browser_download_url']; |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | $this->lastInfo = [ |
||
75 | 'name' => $git['name'], |
||
76 | 'created_at' => Date::convertToDatetime($git['published_at'], Date::FORMAT_TO_HOUR), |
||
77 | 'download_url' => $download |
||
78 | ]; |
||
79 | } |
||
80 | } |
||
81 |