1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Humbug |
4
|
|
|
* |
5
|
|
|
* @category Humbug |
6
|
|
|
* @package Humbug |
7
|
|
|
* @copyright Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com) |
8
|
|
|
* @license https://github.com/padraic/phar-updater/blob/master/LICENSE New BSD License |
9
|
|
|
* |
10
|
|
|
* This class is partially patterned after Composer's self-update. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Humbug\SelfUpdate\Strategy; |
14
|
|
|
|
15
|
|
|
use Humbug\SelfUpdate\Updater; |
16
|
|
|
use Humbug\SelfUpdate\Exception\HttpRequestException; |
17
|
|
|
use Humbug\SelfUpdate\Exception\InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
abstract class ShaStrategyAbstract implements StrategyInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $versionUrl; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $pharUrl; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Download the remote Phar file. |
34
|
|
|
* |
35
|
|
|
* @param Updater $updater |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function download(Updater $updater) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
/** Switch remote request errors to HttpRequestExceptions */ |
41
|
|
|
set_error_handler(array($updater, 'throwHttpRequestException')); |
42
|
|
|
$result = humbug_get_contents($this->getPharUrl()); |
43
|
|
|
restore_error_handler(); |
44
|
|
|
if (false === $result) { |
45
|
|
|
throw new HttpRequestException(sprintf( |
46
|
|
|
'Request to URL failed: %s', $this->getPharUrl() |
47
|
|
|
)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
file_put_contents($updater->getTempPharFile(), $result); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retrieve the current version available remotely. |
55
|
|
|
* |
56
|
|
|
* @param Updater $updater |
57
|
|
|
* @return string|bool |
58
|
|
|
*/ |
59
|
|
|
public function getCurrentRemoteVersion(Updater $updater) |
60
|
|
|
{ |
61
|
|
|
/** Switch remote request errors to HttpRequestExceptions */ |
62
|
|
|
set_error_handler(array($updater, 'throwHttpRequestException')); |
63
|
|
|
$version = humbug_get_contents($this->getVersionUrl()); |
64
|
|
|
restore_error_handler(); |
65
|
|
|
if (false === $version) { |
66
|
|
|
throw new HttpRequestException(sprintf( |
67
|
|
|
'Request to URL failed: %s', $this->getVersionUrl() |
68
|
|
|
)); |
69
|
|
|
} |
70
|
|
|
if (empty($version)) { |
71
|
|
|
throw new HttpRequestException( |
72
|
|
|
'Version request returned empty response.' |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
if (!preg_match('%^[a-z0-9]{40}%', $version, $matches)) { |
76
|
|
|
throw new HttpRequestException( |
77
|
|
|
'Version request returned incorrectly formatted response.' |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $matches[0]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Retrieve the current version of the local phar file. |
86
|
|
|
* |
87
|
|
|
* @param Updater $updater |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getCurrentLocalVersion(Updater $updater) |
91
|
|
|
{ |
92
|
|
|
return sha1_file($updater->getLocalPharFile()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set URL to phar file |
97
|
|
|
* |
98
|
|
|
* @param string $url |
99
|
|
|
*/ |
100
|
|
|
public function setPharUrl($url) |
101
|
|
|
{ |
102
|
|
|
if (!$this->validateAllowedUrl($url)) { |
103
|
|
|
throw new InvalidArgumentException( |
104
|
|
|
sprintf('Invalid url passed as argument: %s.', $url) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
$this->pharUrl = $url; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get URL for phar file |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getPharUrl() |
116
|
|
|
{ |
117
|
|
|
return $this->pharUrl; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set URL to version file |
122
|
|
|
* |
123
|
|
|
* @param string $url |
124
|
|
|
*/ |
125
|
|
|
public function setVersionUrl($url) |
126
|
|
|
{ |
127
|
|
|
if (!$this->validateAllowedUrl($url)) { |
128
|
|
|
throw new InvalidArgumentException( |
129
|
|
|
sprintf('Invalid url passed as argument: %s.', $url) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
$this->versionUrl = $url; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get URL for version file |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getVersionUrl() |
141
|
|
|
{ |
142
|
|
|
return $this->versionUrl; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
protected function validateAllowedUrl($url) |
146
|
|
|
{ |
147
|
|
|
if (filter_var($url, FILTER_VALIDATE_URL) |
148
|
|
|
&& in_array(parse_url($url, PHP_URL_SCHEME), array('http', 'https', 'file'))) { |
149
|
|
|
return true; |
150
|
|
|
} |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.