Skip to content

Commit

Permalink
Merge pull request #3 from mmoreram/feature/temporary-and-specific-di…
Browse files Browse the repository at this point in the history
…rectories

Added temporary and specific directories
  • Loading branch information
mmoreram committed Sep 5, 2014
2 parents c4db8e2 + 0a354be commit aacd179
Show file tree
Hide file tree
Showing 21 changed files with 341 additions and 68 deletions.
36 changes: 18 additions & 18 deletions Adapter/Abstracts/AbstractExtractorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,31 @@

namespace Mmoreram\Extractor\Adapter\Abstracts;

use Mmoreram\Extractor\Filesystem\Interfaces\DirectoryInterface;
use Symfony\Component\Finder\Finder;

/**
* Class AbstractExtractorAdapter
*/
abstract class AbstractExtractorAdapter
{
/**
* @var DirectoryInterface
*
* Directory
*/
protected $directory;

/**
* Construct method
*
* @param DirectoryInterface $directory Directory
*/
public function __construct(DirectoryInterface $directory)
{
$this->directory = $directory;
}

/**
* Create finder from a directory
*
Expand All @@ -34,22 +52,4 @@ protected function createFinderFromDirectory($directory)

return $finder;
}

/**
* Returns new random temporary dir
*
* @return string Temporary dir
*/
protected function getRandomTemporaryDir()
{
$tempfile = tempnam(sys_get_temp_dir(), '_extractor');

if (file_exists($tempfile)) {
unlink($tempfile);
}

mkdir($tempfile);

return($tempfile);
}
}
6 changes: 3 additions & 3 deletions Adapter/PharExtractorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public function isAvailable()
*/
public function extract($filePath)
{
$tmpDirectory = $this->getRandomTemporaryDir();
$directory = $this->directory->getDirectoryPath();
$pharArchive = new Phar($filePath);
$pharArchive->extractTo($tmpDirectory);
$pharArchive->extractTo($directory);

return $this->createFinderFromDirectory($tmpDirectory);
return $this->createFinderFromDirectory($directory);
}
}
6 changes: 3 additions & 3 deletions Adapter/RarExtractorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function isAvailable()
*/
public function extract($filePath)
{
$tmpDirectory = $this->getRandomTemporaryDir();
$directory = $this->directory->getDirectoryPath();
$rarArchive = RarArchive::open($filePath);
$rarEntries = $rarArchive->getEntries();

Expand All @@ -62,9 +62,9 @@ public function extract($filePath)
*/
foreach ($rarEntries as $rarEntry) {

$rarEntry->extract($tmpDirectory);
$rarEntry->extract($directory);
}

return $this->createFinderFromDirectory($tmpDirectory);
return $this->createFinderFromDirectory($directory);
}
}
6 changes: 3 additions & 3 deletions Adapter/TarBz2ExtractorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public function isAvailable()
*/
public function extract($filePath)
{
$tmpDirectory = $this->getRandomTemporaryDir();
$directory = $this->directory->getDirectoryPath();
$pharArchive = new PharData($filePath, null, null, Phar::BZ2);
$pharArchive->extractTo($tmpDirectory);
$pharArchive->extractTo($directory);

return $this->createFinderFromDirectory($tmpDirectory);
return $this->createFinderFromDirectory($directory);
}
}
6 changes: 3 additions & 3 deletions Adapter/TarExtractorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public function isAvailable()
*/
public function extract($filePath)
{
$tmpDirectory = $this->getRandomTemporaryDir();
$directory = $this->directory->getDirectoryPath();
$pharArchive = new PharData($filePath, null, null, Phar::TAR);
$pharArchive->extractTo($tmpDirectory);
$pharArchive->extractTo($directory);

return $this->createFinderFromDirectory($tmpDirectory);
return $this->createFinderFromDirectory($directory);
}
}
6 changes: 3 additions & 3 deletions Adapter/TarGzExtractorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public function isAvailable()
*/
public function extract($filePath)
{
$tmpDirectory = $this->getRandomTemporaryDir();
$directory = $this->directory->getDirectoryPath();
$pharArchive = new PharData($filePath, null, null, Phar::GZ);
$pharArchive->extractTo($tmpDirectory);
$pharArchive->extractTo($directory);

return $this->createFinderFromDirectory($tmpDirectory);
return $this->createFinderFromDirectory($directory);
}
}
6 changes: 3 additions & 3 deletions Adapter/ZipExtractorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public function isAvailable()
*/
public function extract($filePath)
{
$tmpDirectory = $this->getRandomTemporaryDir();
$directory = $this->directory->getDirectoryPath();
$zipArchive = new ZipArchive();
$zipArchive->open($filePath);
$zipArchive->extractTo($tmpDirectory);
$zipArchive->extractTo($directory);

return $this->createFinderFromDirectory($tmpDirectory);
return $this->createFinderFromDirectory($directory);
}
}
42 changes: 37 additions & 5 deletions Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Mmoreram\Extractor\Exception\AdapterNotAvailableException;
use Mmoreram\Extractor\Exception\ExtensionNotSupportedException;
use Mmoreram\Extractor\Exception\FileNotFoundException;
use Mmoreram\Extractor\Resolver\ExtensionResolver;
use Mmoreram\Extractor\Filesystem\Interfaces\DirectoryInterface;
use Mmoreram\Extractor\Resolver\Interfaces\ExtensionResolverInterface;
use Symfony\Component\Finder\Finder;

Expand All @@ -27,7 +27,14 @@
class Extractor
{
/**
* @var ExtensionResolver
* @var DirectoryInterface
*
* Directory
*/
protected $directory;

/**
* @var ExtensionResolverInterface
*
* Extension resolver
*/
Expand All @@ -36,10 +43,15 @@ class Extractor
/**
* Construct method
*
* @param DirectoryInterface $directory Directory
* @param ExtensionResolverInterface $extensionResolver Extension resolver
*/
public function __construct(ExtensionResolverInterface $extensionResolver)
public function __construct(
DirectoryInterface $directory,
ExtensionResolverInterface $extensionResolver
)
{
$this->directory = $directory;
$this->extensionResolver = $extensionResolver;
}

Expand All @@ -62,6 +74,7 @@ public function extractFromFile($filePath)
}

$extension = pathinfo($filePath, PATHINFO_EXTENSION);
$this->checkDirectory();

$extractorAdapterNamespace = $this
->extensionResolver
Expand All @@ -83,10 +96,29 @@ public function extractFromFile($filePath)
*
* @param string $extractorAdapterNamespace Extractor Adapter namespace
*
* @return ExtractorAdapterInterface Extrator adapter
* @return ExtractorAdapterInterface Extractor adapter
*/
protected function instanceExtractorAdapter($extractorAdapterNamespace)
{
return new $extractorAdapterNamespace();
return new $extractorAdapterNamespace($this->directory);
}

/**
* Check directory existence and integrity
*
* @return $this self Object
*/
protected function checkDirectory()
{
$directoryPath = $this
->directory
->getDirectoryPath();

if (!is_dir($directoryPath)) {

mkdir($directoryPath);
}

return $this;
}
}
27 changes: 27 additions & 0 deletions Filesystem/Interfaces/DirectoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\Filesystem\Interfaces;

/**
* Interface DirectoryInterface
*/
interface DirectoryInterface
{
/**
* Get directory path
*
* @return string Directory path
*/
public function getDirectoryPath();
}
59 changes: 59 additions & 0 deletions Filesystem/SpecificDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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\Filesystem;

use Mmoreram\Extractor\Filesystem\Interfaces\DirectoryInterface;

/**
* Class SpecificDirectory
*/
class SpecificDirectory implements DirectoryInterface
{
/**
* @var string
*
* Directory path
*/
public $directoryPath;

/**
* Construct method
*
* @param string $directoryPath Directory path
*/
public function __construct($directoryPath)
{
$this->directoryPath = realpath($directoryPath);
}

/**
* Get directory path
*
* @return string Directory path
*/
public function getDirectoryPath()
{
return $this->directoryPath;
}

/**
* String representation of the object
*
* @return string Directory path
*/
public function __toString()
{
return $this->getDirectoryPath();
}
}
57 changes: 57 additions & 0 deletions Filesystem/TemporaryDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\Filesystem;

use Mmoreram\Extractor\Filesystem\Interfaces\DirectoryInterface;

/**
* Class TemporaryDirectory
*/
class TemporaryDirectory implements DirectoryInterface
{
/**
* @var string
*
* Directory path
*/
public $directoryPath;

/**
* Construct method
*/
public function __construct()
{
$this->directoryPath = sys_get_temp_dir() . "/" . uniqid(time());
}

/**
* Get directory path
*
* @return string Directory path
*/
public function getDirectoryPath()
{
return $this->directoryPath;
}

/**
* String representation of the object
*
* @return string Directory path
*/
public function __toString()
{
return $this->getDirectoryPath();
}
}
Loading

0 comments on commit aacd179

Please sign in to comment.