Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve #14, introduce the extractor abstraction layer
In this commit, we provide two method to extract a tar.gz file, there are call 'tar' command and Phar extension. Also we directly extract files in tarball to the target directory while upgrading. Signed-off-by: RazeSoldier <razesoldier@outlook.com>
- Loading branch information
1 parent
ad7814d
commit d2fd566
Showing
8 changed files
with
215 additions
and
43 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
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,25 @@ | ||
<?php | ||
/** | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* http://www.gnu.org/copyleft/gpl.html | ||
* | ||
* @file | ||
*/ | ||
|
||
namespace RazeSoldier\MWExtUpgrader\Extractor; | ||
|
||
class ExtractException extends \Exception | ||
{ | ||
} |
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,30 @@ | ||
<?php | ||
/** | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* http://www.gnu.org/copyleft/gpl.html | ||
* | ||
* @file | ||
*/ | ||
|
||
namespace RazeSoldier\MWExtUpgrader\Extractor; | ||
|
||
/** | ||
* Used to extract a tar.gz file, please use {@link ExtractorFactory} to get a new instance | ||
* @package RazeSoldier\MWExtUpgrader\Extractor | ||
*/ | ||
interface Extractor | ||
{ | ||
public function extract(string $tarballFilePath, string $dstPath); | ||
} |
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,42 @@ | ||
<?php | ||
/** | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* http://www.gnu.org/copyleft/gpl.html | ||
* | ||
* @file | ||
*/ | ||
|
||
namespace RazeSoldier\MWExtUpgrader\Extractor; | ||
|
||
use Symfony\Component\Process\ExecutableFinder; | ||
|
||
class ExtractorFactory | ||
{ | ||
/** | ||
* If 'tar' command unavailable will use native (Phar) | ||
* @return Extractor | ||
*/ | ||
public static function make(): Extractor { | ||
if (($path = self::checkTarCommandExist()) !== null) { | ||
return new TarExtractor($path); | ||
} | ||
return new NativeExtractor(); | ||
} | ||
|
||
private static function checkTarCommandExist():? string { | ||
$finder = new ExecutableFinder(); | ||
return $finder->find('tar'); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
/** | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* http://www.gnu.org/copyleft/gpl.html | ||
* | ||
* @file | ||
*/ | ||
|
||
namespace RazeSoldier\MWExtUpgrader\Extractor; | ||
|
||
/** | ||
* Use {@link \PharData} to extract .tar.gz file | ||
* @package RazeSoldier\MWExtUpgrader\Extractor | ||
*/ | ||
class NativeExtractor implements Extractor | ||
{ | ||
public function extract(string $tarballFilePath, string $dstPath) | ||
{ | ||
$pharData = new \PharData($tarballFilePath); | ||
$pharData->extractTo($dstPath); | ||
} | ||
} |
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,65 @@ | ||
<?php | ||
/** | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* http://www.gnu.org/copyleft/gpl.html | ||
* | ||
* @file | ||
*/ | ||
|
||
namespace RazeSoldier\MWExtUpgrader\Extractor; | ||
|
||
use Symfony\Component\Process\Exception\ProcessSignaledException; | ||
use Symfony\Component\Process\Exception\ProcessTimedOutException; | ||
use Symfony\Component\Process\Exception\RuntimeException; | ||
use Symfony\Component\Process\Process; | ||
|
||
/** | ||
* Use 'tar' command to extract .tar.gz file | ||
* @package RazeSoldier\MWExtUpgrader\Extractor | ||
*/ | ||
class TarExtractor implements Extractor | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $executablePath; | ||
|
||
public function __construct(string $executablePath) | ||
{ | ||
$this->executablePath = $executablePath; | ||
} | ||
|
||
/** | ||
* @param string $tarballFilePath | ||
* @param string $dstPath | ||
* @throws ExtractException | ||
*/ | ||
public function extract(string $tarballFilePath, string $dstPath) | ||
{ | ||
if (!is_dir($dstPath)) { | ||
mkdir($dstPath); | ||
} | ||
|
||
$process = new Process([$this->executablePath, '-xzf', $tarballFilePath, '-C', $dstPath]); | ||
try { | ||
$exitCode = $process->run(); | ||
} catch (ProcessSignaledException | RuntimeException | ProcessTimedOutException $e) { | ||
throw new ExtractException($e->getMessage(), 0, $e); | ||
} | ||
if ($exitCode !== 0) { | ||
throw new ExtractException("Failed to extract $tarballFilePath to $dstPath (error message: {$process->getErrorOutput()})"); | ||
} | ||
} | ||
} |
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