Skip to content

Commit

Permalink
Added some extensions: tar, gz, bz2
Browse files Browse the repository at this point in the history
* Added header zone in formatter file
* Updated testing
* Updated README
  • Loading branch information
mmoreram committed Aug 31, 2014
1 parent 40e4341 commit 2684dee
Show file tree
Hide file tree
Showing 23 changed files with 393 additions and 39 deletions.
13 changes: 12 additions & 1 deletion .formatter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,15 @@ use-sort:
- _main
group-type: each
sort-type: alph
sort-direction: asc
sort-direction: asc
header: |
/**
* This file is part of the Extractor package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
*/
62 changes: 62 additions & 0 deletions Adapter/TarBz2ExtractorAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* This file is part of the Extractor package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
*/

namespace Mmoreram\Extractor\Adapter;

use Mmoreram\Extractor\Adapter\Abstracts\AbstractExtractorAdapter;
use Mmoreram\Extractor\Adapter\Interfaces\ExtractorAdapterInterface;
use Phar;
use PharData;
use Symfony\Component\Finder\Finder;

/**
* Class TarBz2ExtractorAdapter
*/
class TarBz2ExtractorAdapter extends AbstractExtractorAdapter implements ExtractorAdapterInterface
{
/**
* Checks if current adapter can be used
*
* @return boolean Adapter usable
*/
public function isAvailable()
{
return 'Bz2';
}

/**
* Return the adapter identifier
*
* @return string Adapter identifier
*/
public function getIdentifier()
{
return class_exists('\PharData');
}

/**
* Extract files from a filepath
*
* @param string $filePath File path
*
* @return Finder
*/
public function extract($filePath)
{
$tmpDirectory = $this->getRandomTemporaryDir();
$pharArchive = new PharData($filePath, null, null, Phar::BZ2);
$pharArchive->extractTo($tmpDirectory);

return $this->createFinderFromDirectory($tmpDirectory);
}
}
62 changes: 62 additions & 0 deletions Adapter/TarExtractorAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* This file is part of the Extractor package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
*/

namespace Mmoreram\Extractor\Adapter;

use Mmoreram\Extractor\Adapter\Abstracts\AbstractExtractorAdapter;
use Mmoreram\Extractor\Adapter\Interfaces\ExtractorAdapterInterface;
use Phar;
use PharData;
use Symfony\Component\Finder\Finder;

/**
* Class TarExtractorAdapter
*/
class TarExtractorAdapter extends AbstractExtractorAdapter implements ExtractorAdapterInterface
{
/**
* Checks if current adapter can be used
*
* @return boolean Adapter usable
*/
public function isAvailable()
{
return 'Tar';
}

/**
* Return the adapter identifier
*
* @return string Adapter identifier
*/
public function getIdentifier()
{
return class_exists('\PharData');
}

/**
* Extract files from a filepath
*
* @param string $filePath File path
*
* @return Finder
*/
public function extract($filePath)
{
$tmpDirectory = $this->getRandomTemporaryDir();
$pharArchive = new PharData($filePath, null, null, Phar::TAR);
$pharArchive->extractTo($tmpDirectory);

return $this->createFinderFromDirectory($tmpDirectory);
}
}
62 changes: 62 additions & 0 deletions Adapter/TarGzExtractorAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* This file is part of the Extractor package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
*/

namespace Mmoreram\Extractor\Adapter;

use Mmoreram\Extractor\Adapter\Abstracts\AbstractExtractorAdapter;
use Mmoreram\Extractor\Adapter\Interfaces\ExtractorAdapterInterface;
use Phar;
use PharData;
use Symfony\Component\Finder\Finder;

/**
* Class TarGzExtractorAdapter
*/
class TarGzExtractorAdapter extends AbstractExtractorAdapter implements ExtractorAdapterInterface
{
/**
* Checks if current adapter can be used
*
* @return boolean Adapter usable
*/
public function isAvailable()
{
return 'Gz';
}

/**
* Return the adapter identifier
*
* @return string Adapter identifier
*/
public function getIdentifier()
{
return class_exists('\PharData');
}

/**
* Extract files from a filepath
*
* @param string $filePath File path
*
* @return Finder
*/
public function extract($filePath)
{
$tmpDirectory = $this->getRandomTemporaryDir();
$pharArchive = new PharData($filePath, null, null, Phar::GZ);
$pharArchive->extractTo($tmpDirectory);

return $this->createFinderFromDirectory($tmpDirectory);
}
}
2 changes: 1 addition & 1 deletion Exception/ExtensionNotSupportedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ExtensionNotSupportedException extends Exception
*/
public function __construct($message = "", $code = 0, Exception $previous = null)
{
$message = 'Extension "' . $message . '" is currenly not supported. Extensions supported are [RAR, ZIP, PHAR]';
$message = 'Extension "' . $message . '" is currenly not supported. Extensions supported are [RAR, ZIP, PHAR, TAR, GZ, BZ2]';

parent::__construct($message, $code, $previous);
}
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ works if the php extension is installed.
* Zip - http://php.net/manual/en/book.zip.php
* Rar - http://php.net/manual/en/book.rar.php
* Phar - http://php.net/manual/en/book.phar.php
* Tar
* GZ
* BZ2
17 changes: 13 additions & 4 deletions Resolver/ExtensionResolver.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
<?php

/**
* This file is part of the Elcodi package.
*
* Copyright (c) 2014 Elcodi.com
* This file is part of the Extractor package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
* @author Aldo Chiecchia <[email protected]>
*/

namespace Mmoreram\Extractor\Resolver;
Expand Down Expand Up @@ -51,6 +48,18 @@ public function getAdapterNamespaceGivenExtension($fileExtension)
$adapterNamespace .= 'PharExtractorAdapter';
break;

case 'tar':
$adapterNamespace .= 'TarExtractorAdapter';
break;

case 'gz':
$adapterNamespace .= 'TarGzExtractorAdapter';
break;

case 'bz2':
$adapterNamespace .= 'TarBz2ExtractorAdapter';
break;

default:
throw new ExtensionNotSupportedException($fileExtension);
}
Expand Down
5 changes: 1 addition & 4 deletions Resolver/Interfaces/ExtensionResolverInterface.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
<?php

/**
* This file is part of the Elcodi package.
*
* Copyright (c) 2014 Elcodi.com
* This file is part of the Extractor package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
* @author Aldo Chiecchia <[email protected]>
*/

namespace Mmoreram\Extractor\Resolver\Interfaces;
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"require-dev": {
"phpunit/phpunit": ">=3.7",
"fabpot/php-cs-fixer": "~0.1",
"mmoreram/php-formatter": "~1.0"
"mmoreram/php-formatter": "~1.0",
"ext-tar": "*"
},
"suggest": {
"ext-phar": "For usage of Phar adapter",
Expand Down
File renamed without changes.
File renamed without changes.
Binary file added tests/Adapter/Fixtures/file.tar
Binary file not shown.
Binary file added tests/Adapter/Fixtures/file.tar.bz2
Binary file not shown.
Binary file added tests/Adapter/Fixtures/file.tar.gz
Binary file not shown.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/Adapter/PharExtractorAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function setUp()
public function testExtract()
{
$pharExtractorAdapter = new PharExtractorAdapter();
$finder = $pharExtractorAdapter->extract(dirname(__FILE__). '/Fixtures/phar.phar');
$finder = $pharExtractorAdapter->extract(dirname(__FILE__). '/Fixtures/file.phar');

$this->assertEquals($finder->count(), 3);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Adapter/RarExtractorAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function setUp()
public function testExtract()
{
$rarExtractorAdapter = new RarExtractorAdapter();
$finder = $rarExtractorAdapter->extract(dirname(__FILE__). '/Fixtures/rar.rar');
$finder = $rarExtractorAdapter->extract(dirname(__FILE__). '/Fixtures/file.rar');

$this->assertEquals($finder->count(), 3);
}
Expand Down
46 changes: 46 additions & 0 deletions tests/Adapter/TarBz2ExtractorAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* This file is part of the Extractor package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
*/

namespace Extractor\tests\Adapter;

use Mmoreram\Extractor\Adapter\TarBz2ExtractorAdapter;
use PHPUnit_Framework_TestCase;

/**
* Class TarBz2ExtractorAdapterTest
*/
class TarBz2ExtractorAdapterTest extends PHPUnit_Framework_TestCase
{
/**
* Setup
*/
public function setUp()
{
$pharExtractorAdapter = new TarBz2ExtractorAdapter();
if (!$pharExtractorAdapter->isAvailable()) {

$this->markTestSkipped('Phar extension not installed');
}
}

/**
* Test extract
*/
public function testExtract()
{
$tarExtractorAdapter = new TarBz2ExtractorAdapter();
$finder = $tarExtractorAdapter->extract(dirname(__FILE__). '/Fixtures/file.tar.bz2');

$this->assertEquals($finder->count(), 3);
}
}
Loading

0 comments on commit 2684dee

Please sign in to comment.