1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the SVN-Buddy library. |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
* |
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
8
|
|
|
* @link https://github.com/console-helpers/svn-buddy |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\SVNBuddy\Updater; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use Humbug\SelfUpdate\Exception\HttpRequestException; |
15
|
|
|
use Humbug\SelfUpdate\FileDownloader; |
16
|
|
|
use Humbug\SelfUpdate\Strategy\StrategyInterface; |
17
|
|
|
use Humbug\SelfUpdate\Updater; |
|
|
|
|
18
|
|
|
|
19
|
|
|
class VersionUpdateStrategy implements StrategyInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
const RELEASES_URL = 'https://raw.githubusercontent.com/console-helpers/svn-buddy-updater/master/releases.json'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Stability. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $stability = Stability::STABLE; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Local version. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $localVersion; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Url, from where remote version can be downloaded. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $remoteUrl; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Sets stability. |
47
|
|
|
* |
48
|
|
|
* @param string $stability Stability. |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
1 |
|
public function setStability($stability) |
53
|
|
|
{ |
54
|
1 |
|
$this->stability = $stability; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set version string of the local phar |
59
|
|
|
* |
60
|
|
|
* @param string $version Version. |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
1 |
|
public function setCurrentLocalVersion($version) |
65
|
|
|
{ |
66
|
1 |
|
$this->localVersion = $version; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Download the remote Phar file. |
71
|
|
|
* |
72
|
|
|
* @param Updater $updater Updater. |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
* @throws \LogicException When there is nothing to download. |
76
|
|
|
*/ |
77
|
|
|
public function download(Updater $updater) |
78
|
|
|
{ |
79
|
|
|
if ( !$this->remoteUrl ) { |
80
|
|
|
throw new \LogicException('Run "hasUpdate()" on updater prior to downloading new version.'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
file_put_contents($updater->getTempPharFile(), $this->downloadFile($this->remoteUrl)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Retrieve the current version available remotely. |
88
|
|
|
* |
89
|
|
|
* @param Updater $updater Updater. |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
* @throws \LogicException When update channel doesn't exist. |
93
|
|
|
*/ |
94
|
|
|
public function getCurrentRemoteVersion(Updater $updater) |
95
|
|
|
{ |
96
|
|
|
$this->remoteUrl = ''; |
97
|
|
|
$releases = json_decode( |
98
|
|
|
$this->downloadFile(self::RELEASES_URL), |
99
|
|
|
true |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
if ( !isset($releases[$this->stability]) ) { |
103
|
|
|
throw new \LogicException('The "' . $this->stability . '" update channel not found.'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$version = key($releases[$this->stability]); |
107
|
|
|
$this->remoteUrl = $releases[$this->stability][$version]['phar_download_url']; |
108
|
|
|
|
109
|
|
|
return $version; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retrieve the current version of the local phar file. |
114
|
|
|
* |
115
|
|
|
* @param Updater $updater Updater. |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function getCurrentLocalVersion(Updater $updater) |
120
|
|
|
{ |
121
|
|
|
return $this->localVersion; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Downloads file securely. |
126
|
|
|
* |
127
|
|
|
* @param string $url Url. |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
* @throws HttpRequestException When failed to download a file. |
131
|
|
|
*/ |
132
|
|
|
protected function downloadFile($url) |
133
|
|
|
{ |
134
|
|
|
// If not for "CN_match" on PHP < 5.6 just "humbug_get_contents" function could be used. |
135
|
|
|
$file_downloader = new FileDownloader(); |
136
|
|
|
$result = $file_downloader->download($url); |
137
|
|
|
|
138
|
|
|
if ( false === $result ) { |
|
|
|
|
139
|
|
|
throw new HttpRequestException(sprintf('Request to URL failed: %s', $url)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $result; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: