diff --git a/.gitignore b/.gitignore
index 82cfc4e..1fdff8f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
.idea
composer.lock
vendor
+/build/logs
diff --git a/.travis.yml b/.travis.yml
index c745618..d93816d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,7 +1,5 @@
language: php
php:
- - 5.6
- - 7.0
- 7.1
- 7.2
env:
diff --git a/README.md b/README.md
index a63a854..47a55f1 100644
--- a/README.md
+++ b/README.md
@@ -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
@@ -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.
diff --git a/composer.json b/composer.json
index 97fcd42..3d8ec43 100644
--- a/composer.json
+++ b/composer.json
@@ -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",
@@ -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
}
}
diff --git a/phpcs.xml b/phpcs.xml
new file mode 100644
index 0000000..67e37cf
--- /dev/null
+++ b/phpcs.xml
@@ -0,0 +1,4 @@
+
+
+
+
diff --git a/phpstan.neon b/phpstan.neon
new file mode 100644
index 0000000..096cae4
--- /dev/null
+++ b/phpstan.neon
@@ -0,0 +1,2 @@
+parameters:
+ ignoreErrors:
diff --git a/phpunit.xml b/phpunit.xml
index 282ebae..c8a890e 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -7,10 +7,9 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
- syntaxCheck="false"
bootstrap="vendor/autoload.php">
-
+
tests
diff --git a/src/Keboola/Temp/Temp.php b/src/Keboola/Temp/Temp.php
deleted file mode 100644
index b2ce5fc..0000000
--- a/src/Keboola/Temp/Temp.php
+++ /dev/null
@@ -1,135 +0,0 @@
-prefix = $prefix;
- $this->id = uniqid("run-", true);
- $this->filesystem = new Filesystem();
- }
-
- public function initRunFolder()
- {
- clearstatcache();
- if (!file_exists($this->getTmpPath()) && !is_dir($this->getTmpPath())) {
- $this->filesystem->mkdir($this->getTmpPath(), 0777, true);
- }
- }
-
- /**
- * Get path to temp directory
- *
- * @return string
- */
- protected function getTmpPath()
- {
- $tmpDir = sys_get_temp_dir();
- if (!empty($this->prefix)) {
- $tmpDir .= "/" . $this->prefix;
- }
- $tmpDir .= "/" . $this->id;
- return $tmpDir;
- }
-
- /**
- * Returns path to temp folder for current request
- *
- * @return string
- */
- public function getTmpFolder()
- {
- return $this->getTmpPath();
- }
-
- /**
- * Create empty file in TMP directory
- *
- * @param string $suffix filename suffix
- * @throws \Exception
- * @return \SplFileInfo
- */
- public function createTmpFile($suffix = null)
- {
- $file = uniqid();
-
- if ($suffix) {
- $file .= '-' . $suffix;
- }
-
- return $this->createFile($file);
- }
-
- /**
- * Creates named temporary file
- *
- * @param $fileName
- * @return \SplFileInfo
- * @throws \Exception
- */
- public function createFile($fileName)
- {
- $this->initRunFolder();
-
- $fileInfo = new \SplFileInfo($this->getTmpPath() . '/' . $fileName);
-
- $pathName = $fileInfo->getPathname();
-
- if (!file_exists(dirname($pathName))) {
- $this->filesystem->mkdir(dirname($pathName), 0777, true);
- }
-
- $this->filesystem->touch($pathName);
- $this->files[] = array(
- 'file' => $fileInfo
- );
- $this->filesystem->chmod($pathName, 0600);
-
- return $fileInfo;
- }
-
- /**
- * Set temp id
- *
- * @param $id
- */
- public function setId($id)
- {
- $this->id = $id;
- }
-
- /**
- * Delete all files created by syrup component run
- */
- public function remove()
- {
- foreach ($this->files as $file) {
- if (file_exists($file['file']) && is_file($file['file'])) {
- $this->filesystem->remove($file['file']->getPathname());
- }
- }
- $this->filesystem->remove($this->getTmpPath());
- }
-}
diff --git a/src/Temp.php b/src/Temp.php
new file mode 100644
index 0000000..3558302
--- /dev/null
+++ b/src/Temp.php
@@ -0,0 +1,118 @@
+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());
+ }
+}
diff --git a/tests/Keboola/Temp/TempTest.php b/tests/TempTest.php
similarity index 56%
rename from tests/Keboola/Temp/TempTest.php
rename to tests/TempTest.php
index 6dbcf44..5afbf17 100644
--- a/tests/Keboola/Temp/TempTest.php
+++ b/tests/TempTest.php
@@ -1,35 +1,34 @@
getTmpFolder();
-
- /** @var \SplFileInfo $file */
$file = $temp->createTmpFile('filename_suffix');
- $this->assertFileExists($file->getPathname());
- $this->assertContains($tempFolder, $file->getPathname());
+ self::assertFileExists($file->getPathname());
+ self::assertContains($tempFolder, $file->getPathname());
}
- public function testCreateFile()
+ public function testCreateFile(): void
{
$temp = new Temp();
-
$file = $temp->createFile('test');
self::assertInstanceOf('SplFileInfo', $file);
self::assertEquals($temp->getTmpFolder() . '/' . $file->getFilename(), $file->getPathname());
}
- public function testCreateFileNested()
+ public function testCreateFileNested(): void
{
$temp = new Temp();
$temp->createFile('dir/file');
@@ -37,33 +36,20 @@ public function testCreateFileNested()
self::assertFileExists($temp->getTmpFolder() . '/dir/file');
}
- public function testGetTmpFolder()
+ public function testGetTmpFolder(): void
{
$temp = new Temp('test');
-
$tempFolder = $temp->getTmpFolder();
- $this->assertNotEmpty($tempFolder);
- $this->assertContains(sys_get_temp_dir() . '/test', $temp->getTmpFolder());
+ self::assertNotEmpty($tempFolder);
+ self::assertContains(sys_get_temp_dir() . '/test', $temp->getTmpFolder());
}
- public function testSetTmpFolder()
- {
- $temp = new Temp('test');
- $temp->setId("aabb");
- $expectedTmpDir = sys_get_temp_dir() . "/test/aabb";
- $this->assertEquals($expectedTmpDir, $temp->getTmpFolder());
-
- $temp->createFile('file');
- self::assertFileExists(sys_get_temp_dir() . "/test/aabb/file");
- }
-
- public function testCleanup()
+ public function testCleanup(): void
{
$temp = new Temp();
$temp->createFile('file');
$temp->createFile('dir/file2');
-
$dir = $temp->getTmpFolder();
self::assertFileExists($dir . '/file');
@@ -73,11 +59,9 @@ public function testCleanup()
self::assertFileNotExists($dir);
}
- public function testCleanupForeignFile()
+ public function testCleanupForeignFile(): void
{
$temp = new Temp();
- $temp->initRunFolder();
-
$dir = $temp->getTmpFolder();
touch($dir . '/file');