5
5
namespace Codedge \Updater ;
6
6
7
7
use Codedge \Updater \Events \HasWrongPermissions ;
8
+ use Exception ;
8
9
use GuzzleHttp \Client ;
9
10
use Illuminate \Support \Facades \File ;
10
11
use Symfony \Component \Finder \Finder ;
17
18
*/
18
19
abstract class AbstractRepositoryType
19
20
{
20
- const ACCESS_TOKEN_PREFIX = 'Bearer ' ;
21
-
22
21
/**
23
22
* @var array
24
23
*/
25
24
protected $ config ;
26
25
27
26
/**
28
- * Access token for private repository access.
27
+ * @var Finder|SplFileInfo[]
29
28
*/
30
- private $ accessToken = '' ;
29
+ protected $ pathToUpdate ;
31
30
32
31
/**
33
- * @var Finder|SplFileInfo[]
32
+ * @var string
34
33
*/
35
- protected $ pathToUpdate ;
34
+ public $ storagePath ;
36
35
37
36
/**
38
37
* Unzip an archive.
@@ -41,7 +40,7 @@ abstract class AbstractRepositoryType
41
40
* @param string $targetDir
42
41
* @param bool $deleteZipArchive
43
42
*
44
- * @throws \ Exception
43
+ * @throws Exception
45
44
*
46
45
* @return bool
47
46
*/
@@ -55,7 +54,7 @@ protected function unzipArchive($file = '', $targetDir = '', $deleteZipArchive =
55
54
$ res = $ zip ->open ($ file );
56
55
57
56
if (! $ res ) {
58
- throw new \ Exception ("Cannot open zip archive [ {$ file }]. " );
57
+ throw new Exception ("Cannot open zip archive [ {$ file }]. " );
59
58
}
60
59
61
60
if (empty ($ targetDir )) {
@@ -76,14 +75,14 @@ protected function unzipArchive($file = '', $targetDir = '', $deleteZipArchive =
76
75
/**
77
76
* Check a given directory recursively if all files are writeable.
78
77
*
79
- * @throws \ Exception
78
+ * @throws Exception
80
79
*
81
80
* @return bool
82
81
*/
83
82
protected function hasCorrectPermissionForUpdate (): bool
84
83
{
85
84
if (! $ this ->pathToUpdate ) {
86
- throw new \ Exception ('No directory set for update. Please set the update with: setPathToUpdate(path). ' );
85
+ throw new Exception ('No directory set for update. Please set the update with: setPathToUpdate(path). ' );
87
86
}
88
87
89
88
$ collection = collect ($ this ->pathToUpdate ->files ())->each (function ($ file ) { /* @var \SplFileInfo $file */
@@ -101,12 +100,12 @@ protected function hasCorrectPermissionForUpdate(): bool
101
100
* Download a file to a given location.
102
101
*
103
102
* @param Client $client
104
- * @param string $source
103
+ * @param string $source Url for the source (.zip)
105
104
* @param string $storagePath
106
105
*
107
106
* @return mixed|\Psr\Http\Message\ResponseInterface
108
107
*/
109
- protected function downloadRelease (Client $ client , $ source , $ storagePath )
108
+ protected function downloadRelease (Client $ client , string $ source , $ storagePath )
110
109
{
111
110
$ headers = [];
112
111
@@ -158,64 +157,24 @@ protected function setPathToUpdate(string $path, array $exclude)
158
157
}
159
158
160
159
/**
161
- * Create a releas sub-folder inside the storage dir.
160
+ * Create a release sub-folder inside the storage dir.
162
161
*
163
- * @param string $storagePath
162
+ * @param string $releaseFolder
164
163
* @param string $releaseName
165
164
*/
166
- public function createReleaseFolder ($ storagePath , $ releaseName )
165
+ public function createReleaseFolder (string $ releaseFolder , $ releaseName )
167
166
{
168
- $ subDirName = File::directories ($ storagePath );
169
- $ directories = File::directories ($ subDirName [0 ]);
170
-
171
- File::makeDirectory ($ storagePath .'/ ' .$ releaseName );
172
-
173
- foreach ($ directories as $ directory ) { /* @var string $directory */
174
- File::moveDirectory ($ directory , $ storagePath .'/ ' .$ releaseName .'/ ' .File::name ($ directory ));
175
- }
167
+ $ folders = File::directories ($ releaseFolder );
176
168
177
- $ files = File::allFiles ($ subDirName [0 ], true );
178
- foreach ($ files as $ file ) { /* @var \SplFileInfo $file */
179
- File::move ($ file ->getRealPath (), $ storagePath .'/ ' .$ releaseName .'/ ' .$ file ->getFilename ());
180
- }
181
-
182
- File::deleteDirectory ($ subDirName [0 ]);
183
- }
184
-
185
- /**
186
- * Get the access token.
187
- *
188
- * @param bool $withPrefix
189
- *
190
- * @return string
191
- */
192
- public function getAccessToken ($ withPrefix = true ): string
193
- {
194
- if ($ withPrefix ) {
195
- return self ::ACCESS_TOKEN_PREFIX .$ this ->accessToken ;
169
+ if (count ($ folders ) === 1 ) {
170
+ // Only one sub-folder inside extracted directory
171
+ File::moveDirectory ($ folders [0 ], $ this ->storagePath .$ releaseName );
172
+ File::deleteDirectory ($ folders [0 ]);
173
+ File::deleteDirectory ($ releaseFolder );
174
+ } else {
175
+ // Release (with all files and folders) is already inside, so we need to only rename the folder
176
+ File::moveDirectory ($ releaseFolder , $ this ->storagePath .$ releaseName );
196
177
}
197
-
198
- return $ this ->accessToken ;
199
- }
200
-
201
- /**
202
- * Set access token.
203
- *
204
- * @param string $token
205
- */
206
- public function setAccessToken (string $ token ): void
207
- {
208
- $ this ->accessToken = $ token ;
209
- }
210
-
211
- /**
212
- * Check if an access token has been set.
213
- *
214
- * @return bool
215
- */
216
- public function hasAccessToken (): bool
217
- {
218
- return ! empty ($ this ->accessToken );
219
178
}
220
179
221
180
/**
0 commit comments