Skip to content

Commit

Permalink
Merge pull request #9 from keboola/odin-php7
Browse files Browse the repository at this point in the history
new version
  • Loading branch information
odinuv authored Feb 18, 2019
2 parents 7a1703e + e4854e8 commit ca31326
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 178 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
composer.lock
vendor
/build/logs
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: php
php:
- 5.6
- 7.0
- 7.1
- 7.2
env:
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Temp Library [![Build Status](https://travis-ci.org/keboola/php-temp.svg?branch=master)](https://travis-ci.org/keboola/php-temp) [![Maintainability](https://api.codeclimate.com/v1/badges/1f0a96227c7e6483467d/maintainability)](https://codeclimate.com/github/keboola/php-temp/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/1f0a96227c7e6483467d/test_coverage)](https://codeclimate.com/github/keboola/php-temp/test_coverage)

This library provides an isolated temporary directory for an application. The library has methods
This library provides an isolated temporary folder for an application. The library has methods
for generating randomly named folders and files.

## Usage
Expand All @@ -14,6 +14,16 @@ echo 'Files are stored in: ' . $temp->getTmpFolder();
$temp->remove();
```

Available methods:

- `getTmpFolder` -- Get the name of the temporary folder.
- `createFile` -- Create a named file in the temporary folder.
- `createTmpFile` -- Create a random file in the temporary folder.

## Migration from version 1.0
The temp folder is no longer deleted automatically in the destructor. It needs to
be removed explicitly by calling the `remove()` method.
- The temp folder is no longer deleted automatically in the destructor. It needs to
be removed explicitly by calling the `remove()` method.
- The public `setId` method was removed. This function was rarely used and is no longer available.
- The public `initRunFolder` method was removed. The folder is now initialized when used and there is
no need to call `initRunFolder` any more.
- The protected `getTmpPath` method is now private.
39 changes: 33 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "keboola/php-temp",
"description": "Temp library handles application temporary folder",
"description": "Temp library handles application temporary files",
"license": "MIT",
"keywords": [
"filesystem",
Expand All @@ -13,15 +13,42 @@
}
],
"require": {
"symfony/filesystem": ">=2.8",
"php": ">=5.6"
"symfony/filesystem": ">2.8",
"php": ">=7.1"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
"phpunit/phpunit": "^7.4",
"jakub-onderka/php-parallel-lint": "^1.0",
"phpstan/phpstan-shim": "^0.10.5",
"keboola/coding-standard": "^6.0"
},
"autoload": {
"psr-0": {
"Keboola\\Temp": "src/"
"psr-4": {
"Keboola\\Temp\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Keboola\\Temp\\Tests\\": "tests/"
}
},
"scripts": {
"tests": "phpunit",
"phpstan": "phpstan analyse ./src ./tests --level=max --no-progress -c phpstan.neon",
"phpcs": "phpcs -n --ignore=vendor --extensions=php .",
"phplint": "parallel-lint -j 10 --exclude vendor .",
"build": [
"@phplint",
"@phpcs",
"@phpstan",
"@tests"
],
"ci": [
"@composer validate --no-check-publish --no-check-all",
"@build"
]
},
"config": {
"sort-packages": true
}
}
4 changes: 4 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0"?>
<ruleset name="Project">
<rule ref="vendor/keboola/coding-standard/src/ruleset.xml"/>
</ruleset>
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
ignoreErrors:
3 changes: 1 addition & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php">

<testsuite name="API Test Suite">
<testsuite name="Library Test Suite">
<directory>tests</directory>
</testsuite>

Expand Down
135 changes: 0 additions & 135 deletions src/Keboola/Temp/Temp.php

This file was deleted.

118 changes: 118 additions & 0 deletions src/Temp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

declare(strict_types=1);

namespace Keboola\Temp;

use Symfony\Component\Filesystem\Filesystem;

class Temp
{
/**
* @var String
*/
private $prefix;

/**
* @var Filesystem
*/
private $fileSystem;

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

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

public function __construct(string $prefix = '')
{
$this->prefix = $prefix;
$this->id = uniqid("run-", true);
$this->fileSystem = new Filesystem();
}

private function initRunFolder(): void
{
clearstatcache();
$path = $this->getTmpPath();
if (!file_exists($path) && !is_dir($path)) {
$this->fileSystem->mkdir($path, 0777);
}
$this->tmpFolder = $path;
}

/**
* Get path to the temporary folder.
*
* @return string
*/
private function getTmpPath(): string
{
$tmpDir = sys_get_temp_dir();
if (!empty($this->prefix)) {
$tmpDir .= "/" . $this->prefix;
}
$tmpDir .= "/" . $this->id;
return $tmpDir;
}

/**
* Returns path to the temporary folder.
*
* @return string
*/
public function getTmpFolder(): string
{
if (!$this->tmpFolder) {
$this->initRunFolder();
}
return $this->tmpFolder;
}

/**
* Create a randomly named temporary file.
*
* @param string $suffix filename suffix
* @throws \Exception
* @return \SplFileInfo
*/
public function createTmpFile(string $suffix = ''): \SplFileInfo
{
$file = uniqid();
if ($suffix) {
$file .= '-' . $suffix;
}
return $this->createFile($file);
}

/**
* Creates a named temporary file.
*
* @param string $fileName
* @return \SplFileInfo
* @throws \Exception
*/
public function createFile(string $fileName): \SplFileInfo
{
$fileInfo = new \SplFileInfo($this->getTmpFolder() . '/' . $fileName);
$pathName = $fileInfo->getPathname();
if (!file_exists(dirname($pathName))) {
$this->fileSystem->mkdir(dirname($pathName), 0777);
}
$this->fileSystem->touch($pathName);
$this->fileSystem->chmod($pathName, 0600);
return $fileInfo;
}

/**
* Delete the whole temporary folder including all files.
*/
public function remove(): void
{
$this->fileSystem->remove($this->getTmpFolder());
}
}
Loading

0 comments on commit ca31326

Please sign in to comment.