/vendor/neutron/temporary-filesystem/src/Neutron/TemporaryFilesystem/Manager.php |
@@ -0,0 +1,156 @@ |
<?php |
|
/* |
* This file is part of TemporaryFilesystem. |
* |
* (c) Romain Neutron <imprec@gmail.com> |
* |
* For the full copyright and license information, please view the LICENSE |
* file that was distributed with this source code. |
*/ |
|
namespace Neutron\TemporaryFilesystem; |
|
use Symfony\Component\Filesystem\Filesystem; |
use Symfony\Component\Filesystem\Exception\IOException as SfIOException; |
|
class Manager implements TemporaryFilesystemInterface |
{ |
/** @var Filesystem */ |
private $fs; |
/** @var TemporaryFilesystem */ |
private $tmpFs; |
/** @var array */ |
private $files = array(); |
|
const DEFAULT_SCOPE = '_tmp_fs_'; |
|
public function __construct(TemporaryFilesystemInterface $tmpFs, Filesystem $fs) |
{ |
$this->fs = $fs; |
$this->tmpFs = $tmpFs; |
|
register_shutdown_function(array($this, 'clean'), null, false); |
} |
|
/** |
* {@inheritdoc} |
*/ |
public function createEmptyFile($basePath, $prefix = self::DEFAULT_SCOPE, $suffix = null, $extension = null, $maxTry = 65536) |
{ |
$file = $this->tmpFs->createEmptyFile($basePath, $prefix, $suffix, $extension, $maxTry); |
$this->add($file, $prefix); |
|
return $file; |
} |
|
/** |
* {@inheritdoc} |
*/ |
public function createTemporaryDirectory($mode = 0777, $maxTry = 65536, $prefix = self::DEFAULT_SCOPE) |
{ |
$dir = $this->tmpFs->createTemporaryDirectory($mode, $maxTry, $prefix); |
$this->add($dir, $prefix); |
|
return $dir; |
} |
|
/** |
* {@inheritdoc} |
*/ |
public function createTemporaryFile($prefix = self::DEFAULT_SCOPE, $suffix = null, $extension = null, $maxTry = 65536) |
{ |
$file = $this->tmpFs->createTemporaryFile($prefix, $suffix, $extension, $maxTry); |
$this->add($file, $prefix); |
|
return $file; |
} |
|
/** |
* {@inheritdoc} |
*/ |
public function createTemporaryFiles($quantity = 1, $prefix = self::DEFAULT_SCOPE, $suffix = null, $extension = null, $maxTry = 65536) |
{ |
$files = $this->tmpFs->createTemporaryFiles($quantity, $prefix, $suffix, $extension, $maxTry); |
$this->add($files, $prefix); |
|
return $files; |
} |
|
/** |
* Adds file to be handled by the manager. |
* |
* @param string|array $files |
* @param string $scope |
* |
* @return Manager |
*/ |
public function add($files, $scope = self::DEFAULT_SCOPE) |
{ |
if (!is_array($files)) { |
$files = array($files); |
} |
if ('' === trim($scope)) { |
$scope = self::DEFAULT_SCOPE; |
} |
if (!isset($this->files[$scope])) { |
$this->files[$scope] = array(); |
} |
|
$this->files[$scope] = array_merge($this->files[$scope], $files); |
|
return $this; |
} |
|
/** |
* Removes all managed files in a scope. If no scope provided, all scopes |
* are cleared. |
* |
* @param string $scope |
* |
* @return Manager |
* |
* @throws IOException |
*/ |
public function clean($scope = null, $throwException = true) |
{ |
if (null !== $scope) { |
$this->cleanScope($scope, $throwException); |
} else { |
foreach ($this->files as $scope => $files) { |
$this->cleanScope($scope, $throwException); |
} |
} |
|
return $this; |
} |
|
/** |
* Factory for the Manager |
* |
* @return Manager |
*/ |
public static function create() |
{ |
$fs = new Filesystem(); |
|
return new static(new TemporaryFilesystem($fs), $fs); |
} |
|
private function cleanScope($scope, $throwException) |
{ |
if (!isset($this->files[$scope])) { |
return; |
} |
|
try { |
$this->fs->remove($this->files[$scope]); |
unset($this->files[$scope]); |
} catch (SfIOException $e) { |
unset($this->files[$scope]); |
if ($throwException) { |
throw new IOException('Unable to remove all the files', $e->getCode(), $e); |
} |
} |
} |
} |
/vendor/neutron/temporary-filesystem/src/Neutron/TemporaryFilesystem/TemporaryFilesystem.php |
@@ -0,0 +1,130 @@ |
<?php |
|
/* |
* This file is part of TemporaryFilesystem. |
* |
* (c) Romain Neutron <imprec@gmail.com> |
* |
* For the full copyright and license information, please view the LICENSE |
* file that was distributed with this source code. |
*/ |
|
namespace Neutron\TemporaryFilesystem; |
|
use Symfony\Component\Filesystem\Filesystem; |
use Symfony\Component\Filesystem\Exception\IOException as SfIOException; |
|
class TemporaryFilesystem implements TemporaryFilesystemInterface |
{ |
/** @var Filesystem */ |
private $filesystem; |
|
public function __construct(Filesystem $filesystem) |
{ |
$this->filesystem = $filesystem; |
} |
|
/** |
* {@inheritdoc} |
*/ |
public function createTemporaryDirectory($mode = 0777, $maxTry = 65536, $prefix = null) |
{ |
$basePath = sys_get_temp_dir(); |
|
while ($maxTry > 0) { |
$dir = $basePath . DIRECTORY_SEPARATOR |
. $prefix . base_convert(mt_rand(0x19A100, 0x39AA3FF), 10, 36); |
|
if (false === file_exists($dir)) { |
try { |
$this->filesystem->mkdir($dir, $mode); |
} catch (SfIOException $e) { |
throw new IOException('Unable to make directory', $e->getCode(), $e); |
} |
|
return $dir; |
} |
|
$maxTry --; |
} |
|
throw new IOException('Unable to generate a temporary directory'); |
} |
|
/** |
* {@inheritdoc} |
*/ |
public function createTemporaryFiles($quantity = 1, $prefix = null, $suffix = null, $extension = null, $maxTry = 65536) |
{ |
if ($quantity < 1) { |
throw new \InvalidArgumentException('Invalid temporary files quantity'); |
} |
|
$files = array(); |
|
while ($quantity > 0) { |
$files[] = $this->createEmptyFile(sys_get_temp_dir(), $prefix, $suffix, $extension, $maxTry); |
$quantity --; |
} |
|
return $files; |
} |
|
/** |
* {@inheritdoc} |
*/ |
public function createTemporaryFile($prefix = null, $suffix = null, $extension = null, $maxTry = 65536) |
{ |
$files = $this->createTemporaryFiles(1, $prefix, $suffix, $extension, $maxTry); |
|
return array_pop($files); |
} |
|
/** |
* {@inheritdoc} |
*/ |
public function createEmptyFile($basePath, $prefix = null, $suffix = null, $extension = null, $maxTry = 65536) |
{ |
if (false === is_dir($basePath) || false === is_writeable($basePath)) { |
throw new IOException(sprintf('`%s` should be a writeable directory', $basePath)); |
} |
|
if ($suffix === null && $extension === null) { |
if (false === $file = @tempnam($basePath, $prefix)) { |
throw new IOException('Unable to generate a temporary filename'); |
} |
|
return $file; |
} |
|
while ($maxTry > 0) { |
$file = $basePath . DIRECTORY_SEPARATOR |
. $prefix . base_convert(mt_rand(0x19A100, 0x39AA3FF), 10, 36) . $suffix |
. ( $extension ? '.' . $extension : ''); |
|
if (false === file_exists($file)) { |
try { |
$this->filesystem->touch($file); |
} catch (SfIOException $e) { |
throw new IOException('Unable to touch file', $e->getCode(), $e); |
} |
|
return $file; |
} |
|
$maxTry --; |
} |
|
throw new IOException('Unable to generate a temporary filename'); |
} |
|
/** |
* Creates a TemporaryFilesystem |
* |
* @return TemporaryFilesystem |
*/ |
public static function create() |
{ |
return new static(new Filesystem()); |
} |
} |
/vendor/neutron/temporary-filesystem/src/Neutron/TemporaryFilesystem/TemporaryFilesystemInterface.php |
@@ -0,0 +1,86 @@ |
<?php |
|
/* |
* This file is part of TemporaryFilesystem. |
* |
* (c) Romain Neutron <imprec@gmail.com> |
* |
* For the full copyright and license information, please view the LICENSE |
* file that was distributed with this source code. |
*/ |
|
namespace Neutron\TemporaryFilesystem; |
|
use Symfony\Component\Filesystem\Exception\IOException; |
|
interface TemporaryFilesystemInterface |
{ |
/** |
* Creates a temporary directory. |
* |
* @param octal $mode The directory mode |
* @param integer $maxTry The maximum number of trials |
* @param string $prefix The directory prefix |
* |
* @return string The name of the created directory |
* |
* @throws IOException In case the directory could not be created |
*/ |
public function createTemporaryDirectory($mode = 0777, $maxTry = 65536, $prefix = null); |
|
/** |
* Creates an array of temporary files. |
* |
* Temporary files are created inside the system temporary folder. You must |
* removed them manually at the end of use. |
* |
* @param integer $quantity The quantity of temporary files requested |
* @param string $prefix The prefix of the files |
* @param string $suffix The suffix of the files |
* @param string $extension The extension of the files |
* @param integer $maxTry The maximum number of trials to create one temporary file |
* |
* @return array An array of filenames |
* |
* @throws \InvalidArgumentException In case you provide a wrong argument |
* @throws IOException In case of failure |
*/ |
public function createTemporaryFiles($quantity = 1, $prefix = null, $suffix = null, $extension = null, $maxTry = 65536); |
|
/** |
* Creates a temporary file. |
* |
* Temporary files are created inside the system temporary folder. You must |
* removed them manually at the end of use. |
* |
* @param string $prefix The prefix of the files |
* @param string $suffix The suffix of the files |
* @param string $extension The extension of the files |
* @param integer $maxTry The maximum number of trials to create one temporary file |
* |
* @return array An array of filenames |
* |
* @throws \InvalidArgumentException In case you provide a wrong argument |
* @throws IOException In case of failure |
*/ |
public function createTemporaryFile($prefix = null, $suffix = null, $extension = null, $maxTry = 65536); |
|
/** |
* Create an empty file in the specified directory. |
* |
* The new file is created in the requested directory and will fit the |
* the given parameters. Please note that the filename contains some |
* random caracters. |
* |
* @param string $basePath The directory where to create the file |
* @param string $prefix The prefix of the file |
* @param string $suffix The suffix of the file |
* @param string $extension The extension of the file |
* @param integer $maxTry The maximum number of trials to create the file |
* |
* @return string The path of the created file |
* |
* @throws IOException in case of failure |
*/ |
public function createEmptyFile($basePath, $prefix = null, $suffix = null, $extension = null, $maxTry = 65536); |
} |