1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Deployer\Component\PharUpdate; |
4
|
|
|
|
5
|
|
|
use Deployer\Component\PharUpdate\Exception\InvalidArgumentException; |
6
|
|
|
use Deployer\Component\PharUpdate\Version\Parser; |
7
|
|
|
use Deployer\Component\PharUpdate\Version\Version; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Manages the Phar update process. |
11
|
|
|
* |
12
|
|
|
* @author Kevin Herrera <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Manager |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The update manifest. |
18
|
|
|
* |
19
|
|
|
* @var Manifest |
20
|
|
|
*/ |
21
|
|
|
private $manifest; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The running file (the Phar that will be updated). |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $runningFile; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sets the update manifest. |
32
|
|
|
* |
33
|
|
|
* @param Manifest $manifest The manifest. |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Manifest $manifest) |
36
|
|
|
{ |
37
|
|
|
$this->manifest = $manifest; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the manifest. |
42
|
|
|
* |
43
|
|
|
* @return Manifest The manifest. |
44
|
|
|
*/ |
45
|
|
|
public function getManifest() |
46
|
|
|
{ |
47
|
|
|
return $this->manifest; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns the running file (the Phar that will be updated). |
52
|
|
|
* |
53
|
|
|
* @return string The file. |
54
|
|
|
*/ |
55
|
|
|
public function getRunningFile() |
56
|
|
|
{ |
57
|
|
|
if (null === $this->runningFile) { |
58
|
|
|
$this->runningFile = realpath($_SERVER['argv'][0]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->runningFile; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Sets the running file (the Phar that will be updated). |
66
|
|
|
* |
67
|
|
|
* @param string $file The file name or path. |
68
|
|
|
* |
69
|
|
|
* @throws Exception\Exception |
70
|
|
|
* @throws InvalidArgumentException If the file path is invalid. |
71
|
|
|
*/ |
72
|
|
|
public function setRunningFile($file) |
73
|
|
|
{ |
74
|
|
|
if (false === is_file($file)) { |
75
|
|
|
throw InvalidArgumentException::create( |
76
|
|
|
'The file "%s" is not a file or it does not exist.', |
77
|
|
|
$file |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->runningFile = $file; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Updates the running Phar if any is available. |
86
|
|
|
* |
87
|
|
|
* @param string|Version $version The current version. |
88
|
|
|
* @param boolean $major Lock to current major version? |
89
|
|
|
* @param boolean $pre Allow pre-releases? |
90
|
|
|
* |
91
|
|
|
* @return boolean TRUE if an update was performed, FALSE if none available. |
92
|
|
|
*/ |
93
|
|
|
public function update($version, $major = false, $pre = false) |
94
|
|
|
{ |
95
|
|
|
if (false === ($version instanceof Version)) { |
96
|
|
|
$version = Parser::toVersion($version); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (null !== ($update = $this->manifest->findRecent( |
100
|
|
|
$version, |
101
|
|
|
$major, |
102
|
|
|
$pre |
103
|
|
|
))) { |
104
|
|
|
$update->getFile(); |
105
|
|
|
$update->copyTo($this->getRunningFile()); |
106
|
|
|
|
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|