-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from zlodes/dev-other-archive-extensions
Added ability to use tar and tar.gz or any other skeleton archive format. Closes #91
- Loading branch information
Showing
9 changed files
with
242 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace JeroenG\Packager\ArchiveExtractors; | ||
|
||
abstract class Extractor | ||
{ | ||
/** | ||
* @param string $pathToArchive | ||
* @param string $pathToDirectory | ||
* | ||
* @return void | ||
*/ | ||
abstract public function extract(string $pathToArchive, string $pathToDirectory): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace JeroenG\Packager\ArchiveExtractors; | ||
|
||
use Illuminate\Support\Arr; | ||
use InvalidArgumentException; | ||
|
||
final class Manager | ||
{ | ||
/** @var Extractor[] */ | ||
protected $extractorsMap = []; | ||
|
||
public function __construct() | ||
{ | ||
$this->addExtractor('zip', new Zip()) | ||
->addExtractor('tar', new Tar()) | ||
->addExtractor('tar.gz', new TarGz()); | ||
} | ||
|
||
/** | ||
* @param string $archiveExtension | ||
* | ||
* @return Extractor | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function getExtractor(string $archiveExtension): Extractor | ||
{ | ||
$extractor = Arr::get($this->extractorsMap, $archiveExtension); | ||
|
||
if (! $extractor) { | ||
throw new InvalidArgumentException("There are no extractors for extension '{$archiveExtension}'!"); | ||
} | ||
|
||
return $extractor; | ||
} | ||
|
||
/** | ||
* @param string $archiveExtension | ||
* @param Extractor $instance | ||
* | ||
* @return self | ||
*/ | ||
public function addExtractor(string $archiveExtension, Extractor $instance): self | ||
{ | ||
$this->extractorsMap[$archiveExtension] = $instance; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace JeroenG\Packager\ArchiveExtractors; | ||
|
||
use PharData; | ||
|
||
class Tar extends Extractor | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function extract(string $pathToArchive, string $pathToDirectory): void | ||
{ | ||
$archive = new PharData($pathToArchive); | ||
$archive->extractTo($pathToDirectory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace JeroenG\Packager\ArchiveExtractors; | ||
|
||
use PharData; | ||
|
||
class TarGz extends Extractor | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function extract(string $pathToArchive, string $pathToDirectory): void | ||
{ | ||
$pathToTarArchive = $this->extractTarPathFromGz($pathToArchive); | ||
$archive = new PharData($pathToTarArchive); | ||
$archive->extractTo($pathToDirectory); | ||
|
||
unlink($pathToTarArchive); | ||
} | ||
|
||
/** | ||
* Get the path to the tar within the gz folder. | ||
* | ||
* @param string $pathToArchive | ||
* @return string | ||
*/ | ||
private function extractTarPathFromGz(string $pathToArchive): string | ||
{ | ||
$phar = new PharData($pathToArchive); | ||
$phar->decompress(); | ||
|
||
// Remove .gz and return path. | ||
return str_replace('.gz', '', $pathToArchive); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace JeroenG\Packager\ArchiveExtractors; | ||
|
||
use ZipArchive; | ||
|
||
class Zip extends Extractor | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function extract(string $pathToArchive, string $pathToDirectory): void | ||
{ | ||
$archive = new ZipArchive; | ||
$archive->open($pathToArchive); | ||
$archive->extractTo($pathToDirectory); | ||
$archive->close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace JeroenG\Packager\Tests; | ||
|
||
use Illuminate\Config\Repository; | ||
use Illuminate\Support\Facades\Artisan; | ||
|
||
class SkeletonArchiveExtractorsTest extends TestCase | ||
{ | ||
public function test_new_package_is_created_with_tar_gz_skeleton() | ||
{ | ||
Artisan::call('packager:new', ['vendor' => 'MyVendor', 'name' => 'MyPackage']); | ||
|
||
$this->seeInConsoleOutput('Package created successfully!'); | ||
} | ||
|
||
protected function getEnvironmentSetUp($app) | ||
{ | ||
/** @var Repository $config */ | ||
$config = $app['config']; | ||
|
||
// Change the extension in github archive url form .zip to .tar.gz | ||
$originalZipUrl = $config->get('packager.skeleton'); | ||
$tarGzUrl = str_replace('.zip', '.tar.gz', $originalZipUrl); | ||
|
||
$config->set('packager.skeleton', $tarGzUrl); | ||
} | ||
} |