Skip to content

Commit

Permalink
Merge pull request #10 from Nekland/feature/temporary-file-helper
Browse files Browse the repository at this point in the history
Add new feature : TemporaryFile class
  • Loading branch information
Nek- authored Jan 4, 2019
2 parents 1d8dbfa + 81c7c26 commit 3301ecf
Show file tree
Hide file tree
Showing 20 changed files with 557 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3

branches:
only:
Expand All @@ -12,3 +14,4 @@ install:
- composer install --prefer-source
script:
- ./vendor/bin/phpspec run
- ./vendor/bin/phpunit
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._
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
"license": "MIT",
"description": "Just some tools to work better with PHP",
"autoload": {
"psr-4": { "Nekland\\Tools\\": "src/" }
"psr-4": { "Nekland\\Tools\\": "src/Tools/", "Nekland\\Utils\\": "src/Utils/" }
},
"autoload-dev": {
"psr-4": { "Nekland\\Utils\\Test\\": "tests/Nekland/Utils/" }
},
"require": {
"php": "^5.6 || ^7.0"
},
"require-dev": {
"phpspec/phpspec": "^3.2",
"bossa/phpspec2-expect": "^2.3"
"bossa/phpspec2-expect": "^2.3",
"phpunit/phpunit": "^5.7"
},
"_comment": [
"PHPSpec is limited to ^3.2 to keep compatibility with PHP5.6"
Expand Down
20 changes: 20 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Nekland Tools">
<directory suffix="Test.php">./tests/Nekland</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./docs</directory>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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);
}
}
Loading

0 comments on commit 3301ecf

Please sign in to comment.