From 8bc080661d9972bb64ffc29c05c7b9a732c7b5a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Tue, 4 Feb 2020 14:26:00 +0100 Subject: [PATCH 1/2] Resolve #69 --- src/AbstractRepositoryType.php | 15 ++++++++++++- .../GithubRepositoryType.php | 9 ++++---- .../HttpRepositoryType.php | 8 ++++--- tests/AbstractRepositoryTypeTest.php | 21 +++++++++++++++++++ 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/AbstractRepositoryType.php b/src/AbstractRepositoryType.php index 3e66aec..a52e274 100644 --- a/src/AbstractRepositoryType.php +++ b/src/AbstractRepositoryType.php @@ -5,7 +5,7 @@ namespace Codedge\Updater; use Codedge\Updater\Events\HasWrongPermissions; -use File; +use Illuminate\Support\Facades\File; use GuzzleHttp\Client; use Symfony\Component\Finder\Finder; @@ -217,4 +217,17 @@ public function hasAccessToken(): bool { return ! empty($this->accessToken); } + + /** + * Check if files in one array (i. e. directory) are also exist in a second one. + * + * @param array $directory + * @param array $excludedDirs + * + * @return bool + */ + public function isDirectoryExcluded(array $directory, array $excludedDirs): bool + { + return count(array_intersect($directory, $excludedDirs)) ? true : false; + } } diff --git a/src/SourceRepositoryTypes/GithubRepositoryType.php b/src/SourceRepositoryTypes/GithubRepositoryType.php index d898b01..b25d1e3 100644 --- a/src/SourceRepositoryTypes/GithubRepositoryType.php +++ b/src/SourceRepositoryTypes/GithubRepositoryType.php @@ -9,7 +9,7 @@ use Codedge\Updater\Events\UpdateAvailable; use Codedge\Updater\Events\UpdateFailed; use Codedge\Updater\Events\UpdateSucceeded; -use File; +use Illuminate\Support\Facades\File; use GuzzleHttp\Client; use Storage; use Symfony\Component\Finder\Finder; @@ -138,15 +138,16 @@ public function update($version = '') : bool // Move all directories first collect((new Finder())->in($sourcePath)->exclude($this->config['exclude_folders'])->directories()->sort(function ($a, $b) { return strlen($b->getRealpath()) - strlen($a->getRealpath()); - }))->each(function ($directory) { /** @var \SplFileInfo $directory */ - if (count(array_intersect(File::directories( - $directory->getRealPath()), $this->config['exclude_folders']) == 0) + }))->each(function (/** @var \SplFileInfo $directory */ $directory) { + if (!$this->isDirectoryExcluded( + File::directories($directory->getRealPath()), $this->config['exclude_folders']) ) { File::copyDirectory( $directory->getRealPath(), base_path($directory->getRelativePath()).'/'.$directory->getBasename() ); } + File::deleteDirectory($directory->getRealPath()); }); diff --git a/src/SourceRepositoryTypes/HttpRepositoryType.php b/src/SourceRepositoryTypes/HttpRepositoryType.php index bcd8d10..999e9a4 100644 --- a/src/SourceRepositoryTypes/HttpRepositoryType.php +++ b/src/SourceRepositoryTypes/HttpRepositoryType.php @@ -9,7 +9,7 @@ use Codedge\Updater\Events\UpdateAvailable; use Codedge\Updater\Events\UpdateFailed; use Codedge\Updater\Events\UpdateSucceeded; -use File; +use Illuminate\Support\Facades\File; use GuzzleHttp\Client; use Illuminate\Database\Eloquent\Collection; use Psr\Http\Message\ResponseInterface; @@ -153,13 +153,15 @@ public function update($version = '') : bool collect((new Finder())->in($sourcePath)->exclude($this->config['exclude_folders'])->directories()->sort(function ($a, $b) { return strlen($b->getRealpath()) - strlen($a->getRealpath()); }))->each(function ($directory) { /** @var \SplFileInfo $directory */ - if (count(array_intersect(File::directories( - $directory->getRealPath()), $this->config['exclude_folders'])) == 0) { + if (!$this->isDirectoryExcluded( + File::directories($directory->getRealPath()), $this->config['exclude_folders']) + ) { File::copyDirectory( $directory->getRealPath(), base_path($directory->getRelativePath()).'/'.$directory->getBasename() ); } + File::deleteDirectory($directory->getRealPath()); }); diff --git a/tests/AbstractRepositoryTypeTest.php b/tests/AbstractRepositoryTypeTest.php index d330485..bdf38c8 100644 --- a/tests/AbstractRepositoryTypeTest.php +++ b/tests/AbstractRepositoryTypeTest.php @@ -2,6 +2,8 @@ namespace Codedge\Updater\Tests; +use Codedge\Updater\AbstractRepositoryType; + class AbstractRepositoryTypeTest extends TestCase { /** @@ -18,6 +20,25 @@ public function testCreateReleaseFolder($storagePath, $releaseName) $this->assertFileNotExists($dir, 'Release folder ['.$dir.'] does not exist.'); } + public function testFilesAllExcluded() + { + /** @var AbstractRepositoryType $mock */ + $mock = $this->getMockBuilder(AbstractRepositoryType::class)->getMock(); + $mock->method('isDirectoryExcluded')->willReturn(true); + $this->assertTrue($mock->isDirectoryExcluded(['a', 'b'], ['a'])); + $this->assertTrue($mock->isDirectoryExcluded(['a', 'b'], ['a', 'b'])); + $this->assertTrue($mock->isDirectoryExcluded(['a', 'b'], ['a', 'b', 'c'])); + } + + public function testFilesNotAllExcluded() + { + /** @var AbstractRepositoryType $mock */ + $mock = $this->getMockBuilder(AbstractRepositoryType::class)->getMock(); + $mock->method('isDirectoryExcluded')->willReturn(false); + $this->assertFalse($mock->isDirectoryExcluded(['a', 'b', 'c'], ['c'])); + $this->assertFalse($mock->isDirectoryExcluded(['a', 'b', 'c'], ['a', 'c'])); + } + public function pathProvider() { return [ From 65ab58a807e82bc8abd05b5ca62cb9ad3fed0068 Mon Sep 17 00:00:00 2001 From: codedge Date: Tue, 4 Feb 2020 14:26:18 +0100 Subject: [PATCH 2/2] Apply fixes from StyleCI (#72) --- src/AbstractRepositoryType.php | 8 ++++---- .../SourceRepositoryTypeContract.php | 8 ++++---- src/SourceRepository.php | 8 ++++---- .../GithubRepositoryType.php | 20 +++++++++---------- .../HttpRepositoryType.php | 20 +++++++++---------- src/UpdaterManager.php | 2 +- 6 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/AbstractRepositoryType.php b/src/AbstractRepositoryType.php index a52e274..db12f58 100644 --- a/src/AbstractRepositoryType.php +++ b/src/AbstractRepositoryType.php @@ -5,8 +5,8 @@ namespace Codedge\Updater; use Codedge\Updater\Events\HasWrongPermissions; -use Illuminate\Support\Facades\File; use GuzzleHttp\Client; +use Illuminate\Support\Facades\File; use Symfony\Component\Finder\Finder; /** @@ -45,7 +45,7 @@ abstract class AbstractRepositoryType * * @return bool */ - protected function unzipArchive($file = '', $targetDir = '', $deleteZipArchive = true) : bool + protected function unzipArchive($file = '', $targetDir = '', $deleteZipArchive = true): bool { if (empty($file) || ! File::exists($file)) { throw new \InvalidArgumentException("Archive [{$file}] cannot be found or is empty."); @@ -80,7 +80,7 @@ protected function unzipArchive($file = '', $targetDir = '', $deleteZipArchive = * * @return bool */ - protected function hasCorrectPermissionForUpdate() : bool + protected function hasCorrectPermissionForUpdate(): bool { if (! $this->pathToUpdate) { throw new \Exception('No directory set for update. Please set the update with: setPathToUpdate(path).'); @@ -133,7 +133,7 @@ protected function downloadRelease(Client $client, $source, $storagePath) * * @return bool */ - protected function isSourceAlreadyFetched($version) : bool + protected function isSourceAlreadyFetched($version): bool { $storagePath = $this->config['download_path'].'/'.$version; if (! File::exists($storagePath) || empty(File::directories($storagePath)) diff --git a/src/Contracts/SourceRepositoryTypeContract.php b/src/Contracts/SourceRepositoryTypeContract.php index 6ff000c..02f5fe5 100644 --- a/src/Contracts/SourceRepositoryTypeContract.php +++ b/src/Contracts/SourceRepositoryTypeContract.php @@ -18,7 +18,7 @@ public function fetch($version = ''); * * @return bool */ - public function update() : bool; + public function update(): bool; /** * Check repository if a newer version than the installed one is available. @@ -29,7 +29,7 @@ public function update() : bool; * * @return bool */ - public function isNewVersionAvailable($currentVersion = '') : bool; + public function isNewVersionAvailable($currentVersion = ''): bool; /** * Get the version that is currenly installed. @@ -40,7 +40,7 @@ public function isNewVersionAvailable($currentVersion = '') : bool; * * @return string */ - public function getVersionInstalled($prepend = '', $append = '') : string; + public function getVersionInstalled($prepend = '', $append = ''): string; /** * Get the latest version that has been published in a certain repository. @@ -51,5 +51,5 @@ public function getVersionInstalled($prepend = '', $append = '') : string; * * @return string */ - public function getVersionAvailable($prepend = '', $append = '') : string; + public function getVersionAvailable($prepend = '', $append = ''): string; } diff --git a/src/SourceRepository.php b/src/SourceRepository.php index 56b849f..dc2fb18 100644 --- a/src/SourceRepository.php +++ b/src/SourceRepository.php @@ -50,7 +50,7 @@ public function fetch($version = '') * * @return bool */ - public function update($version = '', $forceFetching = true) : bool + public function update($version = '', $forceFetching = true): bool { $version = $version ?: $this->getVersionAvailable(); @@ -72,7 +72,7 @@ public function update($version = '', $forceFetching = true) : bool * * @return bool */ - public function isNewVersionAvailable($currentVersion = '') : bool + public function isNewVersionAvailable($currentVersion = ''): bool { return $this->sourceRepository->isNewVersionAvailable($currentVersion); } @@ -86,7 +86,7 @@ public function isNewVersionAvailable($currentVersion = '') : bool * * @return string */ - public function getVersionInstalled($prepend = '', $append = '') : string + public function getVersionInstalled($prepend = '', $append = ''): string { return $this->sourceRepository->getVersionInstalled($prepend, $append); } @@ -100,7 +100,7 @@ public function getVersionInstalled($prepend = '', $append = '') : string * * @return string */ - public function getVersionAvailable($prepend = '', $append = '') : string + public function getVersionAvailable($prepend = '', $append = ''): string { return $this->sourceRepository->getVersionAvailable($prepend, $append); } diff --git a/src/SourceRepositoryTypes/GithubRepositoryType.php b/src/SourceRepositoryTypes/GithubRepositoryType.php index b25d1e3..09fbfa7 100644 --- a/src/SourceRepositoryTypes/GithubRepositoryType.php +++ b/src/SourceRepositoryTypes/GithubRepositoryType.php @@ -9,8 +9,8 @@ use Codedge\Updater\Events\UpdateAvailable; use Codedge\Updater\Events\UpdateFailed; use Codedge\Updater\Events\UpdateSucceeded; -use Illuminate\Support\Facades\File; use GuzzleHttp\Client; +use Illuminate\Support\Facades\File; use Storage; use Symfony\Component\Finder\Finder; @@ -56,7 +56,7 @@ public function __construct(Client $client, array $config) * * @return bool */ - public function isNewVersionAvailable($currentVersion = '') : bool + public function isNewVersionAvailable($currentVersion = ''): bool { $version = $currentVersion ?: $this->getVersionInstalled(); @@ -124,7 +124,7 @@ public function fetch($version = '') * * @return bool */ - public function update($version = '') : bool + public function update($version = ''): bool { $this->setPathToUpdate(base_path(), $this->config['exclude_folders']); @@ -139,7 +139,7 @@ public function update($version = '') : bool collect((new Finder())->in($sourcePath)->exclude($this->config['exclude_folders'])->directories()->sort(function ($a, $b) { return strlen($b->getRealpath()) - strlen($a->getRealpath()); }))->each(function (/** @var \SplFileInfo $directory */ $directory) { - if (!$this->isDirectoryExcluded( + if (! $this->isDirectoryExcluded( File::directories($directory->getRealPath()), $this->config['exclude_folders']) ) { File::copyDirectory( @@ -179,7 +179,7 @@ public function update($version = '') : bool * * @return string */ - public function getVersionInstalled($prepend = '', $append = '') : string + public function getVersionInstalled($prepend = '', $append = ''): string { return $prepend.$this->config['version_installed'].$append; } @@ -193,7 +193,7 @@ public function getVersionInstalled($prepend = '', $append = '') : string * * @return string */ - public function getVersionAvailable($prepend = '', $append = '') : string + public function getVersionAvailable($prepend = '', $append = ''): string { if ($this->versionFileExists()) { $version = $prepend.$this->getVersionFile().$append; @@ -241,7 +241,7 @@ protected function getRepositoryReleases() * * @return bool */ - protected function versionFileExists() : bool + protected function versionFileExists(): bool { return Storage::exists(static::NEW_VERSION_FILE); } @@ -253,7 +253,7 @@ protected function versionFileExists() : bool * * @return bool */ - protected function setVersionFile(string $content) : bool + protected function setVersionFile(string $content): bool { return Storage::put(static::NEW_VERSION_FILE, $content); } @@ -263,7 +263,7 @@ protected function setVersionFile(string $content) : bool * * @return string */ - protected function getVersionFile() : string + protected function getVersionFile(): string { return Storage::get(static::NEW_VERSION_FILE); } @@ -273,7 +273,7 @@ protected function getVersionFile() : string * * @return bool */ - protected function deleteVersionFile() : bool + protected function deleteVersionFile(): bool { return Storage::delete(static::NEW_VERSION_FILE); } diff --git a/src/SourceRepositoryTypes/HttpRepositoryType.php b/src/SourceRepositoryTypes/HttpRepositoryType.php index 999e9a4..28b9704 100644 --- a/src/SourceRepositoryTypes/HttpRepositoryType.php +++ b/src/SourceRepositoryTypes/HttpRepositoryType.php @@ -9,9 +9,9 @@ use Codedge\Updater\Events\UpdateAvailable; use Codedge\Updater\Events\UpdateFailed; use Codedge\Updater\Events\UpdateSucceeded; -use Illuminate\Support\Facades\File; use GuzzleHttp\Client; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Support\Facades\File; use Psr\Http\Message\ResponseInterface; use Storage; use Symfony\Component\Finder\Finder; @@ -70,7 +70,7 @@ public function __construct(Client $client, array $config) * * @return bool */ - public function isNewVersionAvailable($currentVersion = '') : bool + public function isNewVersionAvailable($currentVersion = ''): bool { $version = $currentVersion ?: $this->getVersionInstalled(); @@ -139,7 +139,7 @@ public function fetch($version = '') * * @return bool */ - public function update($version = '') : bool + public function update($version = ''): bool { $this->setPathToUpdate(base_path(), $this->config['exclude_folders']); @@ -153,7 +153,7 @@ public function update($version = '') : bool collect((new Finder())->in($sourcePath)->exclude($this->config['exclude_folders'])->directories()->sort(function ($a, $b) { return strlen($b->getRealpath()) - strlen($a->getRealpath()); }))->each(function ($directory) { /** @var \SplFileInfo $directory */ - if (!$this->isDirectoryExcluded( + if (! $this->isDirectoryExcluded( File::directories($directory->getRealPath()), $this->config['exclude_folders']) ) { File::copyDirectory( @@ -191,7 +191,7 @@ public function update($version = '') : bool * * @return string */ - public function getVersionInstalled($prepend = '', $append = '') : string + public function getVersionInstalled($prepend = '', $append = ''): string { return $this->config['version_installed']; } @@ -207,7 +207,7 @@ public function getVersionInstalled($prepend = '', $append = '') : string * * @return string */ - public function getVersionAvailable($prepend = '', $append = '') : string + public function getVersionAvailable($prepend = '', $append = ''): string { $version = ''; if ($this->versionFileExists()) { @@ -266,7 +266,7 @@ protected function getPackageReleases() * * @return bool */ - protected function versionFileExists() : bool + protected function versionFileExists(): bool { return Storage::exists(static::NEW_VERSION_FILE); } @@ -278,7 +278,7 @@ protected function versionFileExists() : bool * * @return bool */ - protected function setVersionFile(string $content) : bool + protected function setVersionFile(string $content): bool { return Storage::put(static::NEW_VERSION_FILE, $content); } @@ -288,7 +288,7 @@ protected function setVersionFile(string $content) : bool * * @return string */ - protected function getVersionFile() : string + protected function getVersionFile(): string { return Storage::get(static::NEW_VERSION_FILE); } @@ -298,7 +298,7 @@ protected function getVersionFile() : string * * @return bool */ - protected function deleteVersionFile() : bool + protected function deleteVersionFile(): bool { return Storage::delete(static::NEW_VERSION_FILE); } diff --git a/src/UpdaterManager.php b/src/UpdaterManager.php index fffa56a..5d4a8d1 100644 --- a/src/UpdaterManager.php +++ b/src/UpdaterManager.php @@ -53,7 +53,7 @@ public function __construct(Application $app) * * @return SourceRepository */ - public function source($name = '') : SourceRepository + public function source($name = ''): SourceRepository { $name = $name ?: $this->getDefaultSourceRepository();