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 ConsoleHelpers\ConsoleKit\Config\ConfigEditor; |
||
15 | use ConsoleHelpers\SVNBuddy\Process\IProcessFactory; |
||
16 | |||
17 | class UpdateManager |
||
18 | { |
||
19 | |||
20 | const NEW_VERSION_CHECK_INTERVAL = '1 day'; |
||
21 | |||
22 | /** |
||
23 | * Updater. |
||
24 | * |
||
25 | * @var Updater |
||
26 | */ |
||
27 | protected $updater; |
||
28 | |||
29 | /** |
||
30 | * Config editor. |
||
31 | * |
||
32 | * @var ConfigEditor |
||
33 | */ |
||
34 | protected $configEditor; |
||
35 | |||
36 | /** |
||
37 | * Process factory. |
||
38 | * |
||
39 | * @var IProcessFactory |
||
40 | */ |
||
41 | protected $processFactory; |
||
42 | |||
43 | /** |
||
44 | * File, containing new version. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $newVersionFilename; |
||
49 | |||
50 | /** |
||
51 | * UpdateManager constructor. |
||
52 | * |
||
53 | * @param Updater $updater Updater. |
||
54 | * @param ConfigEditor $config_editor Config editor. |
||
55 | * @param IProcessFactory $process_factory Process factory. |
||
56 | * @param string $working_directory Working directory. |
||
57 | */ |
||
58 | public function __construct( |
||
59 | Updater $updater, |
||
60 | ConfigEditor $config_editor, |
||
61 | IProcessFactory $process_factory, |
||
62 | $working_directory |
||
63 | ) { |
||
64 | $this->updater = $updater; |
||
65 | $this->configEditor = $config_editor; |
||
66 | $this->processFactory = $process_factory; |
||
67 | $this->newVersionFilename = $working_directory . '/new-version'; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Sets update channel. |
||
72 | * |
||
73 | * @param string $update_channel Update channel. |
||
74 | * |
||
75 | * @return void |
||
76 | * @throws \InvalidArgumentException When unknown update channel is given. |
||
77 | */ |
||
78 | public function setUpdateChannel($update_channel) |
||
79 | { |
||
80 | $valid_channels = array(Stability::STABLE, Stability::SNAPSHOT, Stability::PREVIEW); |
||
81 | |||
82 | if ( !in_array($update_channel, $valid_channels) ) { |
||
83 | throw new \InvalidArgumentException('Update channel "' . $update_channel . '" not found.'); |
||
84 | } |
||
85 | |||
86 | $this->configEditor->set('update-channel', $update_channel); |
||
87 | |||
88 | /** @var VersionUpdateStrategy $update_strategy */ |
||
89 | $update_strategy = $this->updater->getStrategy(); |
||
90 | |||
91 | if ( $update_strategy instanceof VersionUpdateStrategy ) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
92 | $update_strategy->setStability($update_channel); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Returns new version. |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | public function getNewVersion() |
||
102 | { |
||
103 | if ( !file_exists($this->newVersionFilename) ) { |
||
104 | $new_version = ''; |
||
105 | $last_checked = 0; |
||
106 | } |
||
107 | else { |
||
108 | $new_version = file_get_contents($this->newVersionFilename); |
||
109 | $last_checked = filemtime($this->newVersionFilename); |
||
110 | } |
||
111 | |||
112 | if ( $last_checked < strtotime('-' . self::NEW_VERSION_CHECK_INTERVAL) ) { |
||
113 | $process = $this->processFactory->createCommandProcess('self-update', array('--check')); |
||
114 | shell_exec($process->getCommandLine() . ' > /dev/null 2>&1 &'); |
||
115 | |||
116 | return ''; |
||
117 | } |
||
118 | |||
119 | return $new_version; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Sets new version. |
||
124 | * |
||
125 | * @param string $version Version. |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | public function setNewVersion($version) |
||
130 | { |
||
131 | file_put_contents($this->newVersionFilename, $version); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Returns update channel. |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getUpdateChannel() |
||
140 | { |
||
141 | return $this->configEditor->get('update-channel'); |
||
0 ignored issues
–
show
|
|||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Returns updater object. |
||
146 | * |
||
147 | * @return Updater |
||
148 | */ |
||
149 | public function getUpdater() |
||
150 | { |
||
151 | return $this->updater; |
||
152 | } |
||
153 | |||
154 | } |
||
155 |