Skip to content

Commit

Permalink
Add temporary file & directory helper classes
Browse files Browse the repository at this point in the history
These classes are completely different from other tools of this library.
That's why they are moved to a new namespace called "utils".
  • Loading branch information
Nek- committed Jan 4, 2019
1 parent 6a73da3 commit 1dd20b3
Show file tree
Hide file tree
Showing 13 changed files with 528 additions and 1 deletion.
75 changes: 74 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Nekland Tools

[![Build Status](https://travis-ci.org/Nekland/Tools.svg?branch=master)](https://travis-ci.org/Nekland/Tools)

Just some classes helping to code with PHP in general.
Just some classes helping to code with PHP in general. No dependencies. High quality code.

**This repository follows semver.**

Expand All @@ -24,6 +24,8 @@ Here is the list of tools it provides:
- [ArrayTools](#arraytools-class)
- [EqualableInterface](#equalableinterface)
- [DateTimeComparator](#datetimecomparator-class)
- [TemporaryFile](#temporary-file-management)
- [TemporaryDirectory](#temporary-directory-management)

### StringTools class

Expand Down Expand Up @@ -149,3 +151,74 @@ DateTimeComparator::lowest($dateTime1, $dateTime2) : ?\DateTimeInterface

* `$dateTime1` The first \DateTimeInterface or null
* `$dateTime2` The second \DateTimeInterface or null

### Temporary file management

The class `TemporaryFile` helps you to create temporary file with ease.

#### ::__construct()

```php
TemporaryFile::__construct(TemporaryDirectory $dir = null, string $prefix = '')
```

**Examples:**

```php
// Create a file in temporary folder
$file = new TemporaryFile();

// Create a file inside a temporary directory (see TemporaryDirectory class)
$file = new TemporaryFile($temporaryDir);

// Create a file in a temporary folder with php_ prefix.
$file = new TemporaryFile(null, 'php_');
```

#### ::setContent & ::getContent

```php
TemporaryFile::setContent(string $content)
TemporaryFile::getContent(): string
```

#### ::remove()

Removes the file from filesystem.

```php
TemporaryFile::remove()
```

### Temporary directory management

#### ::__construct()

```php
TemporaryDirectory::__construct(string $dir = null, string $prefix = 'phpgenerated')
```

**Examples:**

```php
// create a new directory
$directory = new TemporaryDirectory();
```

#### ::getTemporaryFile()

Create a `TemporaryFile` from the directory generated.

```php
TemporaryDirectory::getTemporaryFile(): TemporaryFile
```

#### ::remove()

Removes the directory.

```php
TemporaryDirectory::remove(bool $force = false): void
```

_If `force` is specified to true, it will remove the directory even if it has files inside._
16 changes: 16 additions & 0 deletions src/Utils/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* This file is a part of NeklandTools package.
*
* (c) Nekland <[email protected]>
*
* For the full license, take a look to the LICENSE file
* on the root directory of this project
*/

namespace Nekland\Utils\Exception;

class LogicException extends \LogicException implements NeklandExceptionInterface
{

}
15 changes: 15 additions & 0 deletions src/Utils/Exception/NeklandExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* This file is a part of NeklandTools package.
*
* (c) Nekland <[email protected]>
*
* For the full license, take a look to the LICENSE file
* on the root directory of this project
*/

namespace Nekland\Utils\Exception;

interface NeklandExceptionInterface
{
}
16 changes: 16 additions & 0 deletions src/Utils/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* This file is a part of NeklandTools package.
*
* (c) Nekland <[email protected]>
*
* For the full license, take a look to the LICENSE file
* on the root directory of this project
*/

namespace Nekland\Utils\Exception;

class RuntimeException extends \RuntimeException implements NeklandExceptionInterface
{

}
17 changes: 17 additions & 0 deletions src/Utils/Tempfile/Exception/CannotCreateFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is a part of NeklandTools package.
*
* (c) Nekland <[email protected]>
*
* For the full license, take a look to the LICENSE file
* on the root directory of this project
*/

namespace Nekland\Utils\Tempfile\Exception;

use Nekland\Utils\Exception\RuntimeException;

class CannotCreateFileException extends RuntimeException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* This file is a part of NeklandTools package.
*
* (c) Nekland <[email protected]>
*
* For the full license, take a look to the LICENSE file
* on the root directory of this project
*/

namespace Nekland\Utils\Tempfile\Exception;

use Nekland\Utils\Exception\RuntimeException;

class ImpossibleToCreateDirectoryException extends RuntimeException
{
public function __construct($directory = "", $code = 0, $previous = null)
{
parent::__construct(sprintf('Impossible to create directory "%s".', $directory), $code, $previous);
}
}
17 changes: 17 additions & 0 deletions src/Utils/Tempfile/Exception/ImpossibleToUpdateFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is a part of NeklandTools package.
*
* (c) Nekland <[email protected]>
*
* For the full license, take a look to the LICENSE file
* on the root directory of this project
*/

namespace Nekland\Utils\Tempfile\Exception;

use Nekland\Utils\Exception\RuntimeException;

class ImpossibleToUpdateFileException extends RuntimeException
{
}
18 changes: 18 additions & 0 deletions src/Utils/Tempfile/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This file is a part of NeklandTools package.
*
* (c) Nekland <[email protected]>
*
* For the full license, take a look to the LICENSE file
* on the root directory of this project
*/

namespace Nekland\Utils\Tempfile\Exception;

use Nekland\Utils\Exception\LogicException;

class InvalidArgumentException extends LogicException
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* This file is a part of NeklandTools package.
*
* (c) Nekland <[email protected]>
*
* For the full license, take a look to the LICENSE file
* on the root directory of this project
*/

namespace Nekland\Utils\Tempfile\Exception;

use Nekland\Utils\Exception\LogicException;

class TemporaryFileAlreadyRemovedException extends LogicException
{
public function __construct($path = "", $code = 0, $previous = null)
{
$message = \sprintf('The file "%s" has already been removed.', $path);
parent::__construct($message, $code, $previous);
}
}
115 changes: 115 additions & 0 deletions src/Utils/Tempfile/TemporaryDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* This file is a part of NeklandTools package.
*
* (c) Nekland <[email protected]>
*
* For the full license, take a look to the LICENSE file
* on the root directory of this project
*/

namespace Nekland\Utils\Tempfile;

use Nekland\Utils\Tempfile\Exception\ImpossibleToCreateDirectoryException;

class TemporaryDirectory
{
/** @var bool */
private $wasAlreadyExisting;

/** @var string */
private $dir;

/** @var bool */
private $removed;

/**
* TemporaryDirectory constructor.
*
* @param null|string $dir
* @param string $prefix
*/
public function __construct($dir = null, $prefix = 'phpgenerated')
{
$this->removed = false;
$this->wasAlreadyExisting = false;
if (null !== $dir && \is_dir($dir)) {
$this->wasAlreadyExisting = true;
$this->dir = $dir;
}

if (null === $this->dir) {
$this->dir = \sys_get_temp_dir() . '/' . $prefix . '_' . \uniqid();
}

if (\mkdir($this->dir) === false) {
throw new ImpossibleToCreateDirectoryException($this->dir);
}
}

/**
* Generates a TemporaryFile from the current TemporaryDirectory
*
* @return TemporaryFile
*/
public function getTemporaryFile()
{
return $this->files[] = new TemporaryFile($this);
}

/**
* @return string
*/
public function getPathname()
{
return $this->dir;
}

/**
* Removes directory. In case the directory contains files, it will not be removed.
* Unless you specify $force to true.
*
* @param bool $force
*/
public function remove($force = false)
{
$isEmpty = $this->isEmpty();
if ($force && !$isEmpty) {
$it = new \RecursiveDirectoryIterator($this->dir, \RecursiveDirectoryIterator::SKIP_DOTS);
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) {
if ($file->isDir()){
\rmdir($file->getRealPath());
} else {
\unlink($file->getRealPath());
}
}

\rmdir($this->dir);
$this->removed = true;

return;
}

if ($isEmpty) {
\rmdir($this->dir);
$this->removed = true;
}
}

/**
* @return bool
*/
public function hasBeenRemoved()
{
return $this->removed;
}

/**
* @return bool
*/
public function isEmpty()
{
return !(new \FilesystemIterator($this->dir))->valid();
}
}
Loading

0 comments on commit 1dd20b3

Please sign in to comment.