From a338c7cb38d9e9e9c967b3ea9f96819801dc23be Mon Sep 17 00:00:00 2001 From: Netkas Date: Sun, 26 Feb 2023 15:55:24 -0500 Subject: [PATCH] Replaced `$extension` parameter with `$options` parameter in `\TempFile > TempFile > __construct()` so that it's possible to specify the file extension and the file name prefix. --- CHANGELOG.md | 7 ++++ README.md | 36 +++++++++++++++++--- project.json | 2 +- src/TempFile/Options.php | 22 ++++++++++++ src/TempFile/TempFile.php | 70 +++++++++++++++++++++++++++++---------- tests/file_test.php | 5 ++- 6 files changed, 118 insertions(+), 24 deletions(-) create mode 100644 src/TempFile/Options.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 7288ee1..4b4720c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.1.0] - 2023-02-26 + +### Changed + + - Replaced `$extension` parameter with `$options` parameter in `\TempFile > TempFile > __construct()` + so that it's possible to specify the file extension and the file name prefix. + ## [1.0.0] - 2023-02-25 ### Added diff --git a/README.md b/README.md index 12a04b3..ef61445 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,18 @@ TempFile is a very simple library used for creating temporary files without having to write code to delete them once you're done with them. +## Table of Contents + + +* [TempFile](#tempfile) + * [Table of Contents](#table-of-contents) + * [Installation](#installation) + * [Compiling from source](#compiling-from-source) + * [Usage](#usage) + * [Options](#options) + * [License](#license) + + ## Installation The library can be installed using ncc: @@ -44,15 +56,20 @@ make release ## Usage -Just create a class object, optionally specifying a file extension to use (without the dot). And that's all, an -Exception will be thrown if the file could not be created. +Just create a class object, optionally specifying options you'd like to use. ```php require_once('ncc'); import('net.nosial.tempfile'); -$file1 = new TempFile(); -$file2 = new TempFile('txt'); +$file1 = new TempFile\TempFile(); +$file2 = new TempFile\TempFile([ + TempFile\Options::Extension => 'txt', + TempFile\Options::Prefix => 'prefix_', + TempFile\Options::Suffix => '_suffix', + TempFile\Options::RandomLength => 8, + TempFile\Options::Directory => '/tmp', +]); ``` You can obtain the file path by using the `getFilepath()` method or by using the object as a string. @@ -66,6 +83,17 @@ Files are automatically deleted when the object is destroyed, if for some reason properly called, a shutdown function is automatically registered to delete all the temporary files that were created. +### Options + +The following options are available: + + - `TempFile\Options::Extension` - The file extension to use, defaults to 'tmp', + - `TempFile\Options::Prefix` - The prefix to use for the file name, empty by default, + - `TempFile\Options::Suffix` - The suffix to use for the file name, empty by default, + - `TempFile\Options::RandomLength` - The length of the random string to use for the file name, defaults to 16, + - `TempFile\Options::Directory` - The directory to create the file in, defaults to the system's temporary directory. + - `TempFile\Options::Filename` - The filename to use, if specified, the random string will not be used. + ## License This library is licensed under the MIT license, see the LICENSE file diff --git a/project.json b/project.json index 91c7f8f..b5108b3 100644 --- a/project.json +++ b/project.json @@ -12,7 +12,7 @@ "package": "net.nosial.tempfile", "copyright": "Copyright (c) 2022-2023 Nosial", "description": "TempFile is a PHP library for creating temporary files.", - "version": "1.0.0", + "version": "1.1.0", "uuid": "910f98fe-b4c9-11ed-b13f-fdc283a6db6d" }, "build": { diff --git a/src/TempFile/Options.php b/src/TempFile/Options.php new file mode 100644 index 0000000..de547c7 --- /dev/null +++ b/src/TempFile/Options.php @@ -0,0 +1,22 @@ + $value) { - $extension = 'tmp'; + if(!is_string($value) && !is_int($value)) + throw new InvalidArgumentException(sprintf('The value for option %s must be a string or int, got %s', $option, gettype($value))); + if(!in_array($option, Options::All)) + throw new InvalidArgumentException(sprintf('The option %s is not valid', $option)); } - $this->extension = ltrim($extension, '.'); - $this->filename = $this->randomString() . '.' . $this->extension; - $this->filepath = $this->getTempDir() . DIRECTORY_SEPARATOR . $this->filename; + if(!isset($options[Options::Extension])) + $options[Options::Extension] = 'tmp'; + if(!isset($options[Options::RandomLength])) + $options[Options::RandomLength] = 8; + + if(isset($options[Options::Filename])) + { + $this->filename = $options[Options::Filename]; + } + else + { + $this->filename = self::randomString((int)$options[Options::RandomLength]); + } + + if(isset($options[Options::Prefix])) + $this->filename = $options[Options::Prefix] . $this->filename; + if(isset($options[Options::Suffix])) + $this->filename = $this->filename . $options[Options::Suffix]; + $this->filename .= '.' . $options[Options::Extension]; + $this->filename = preg_replace('/[^a-zA-Z0-9.\-_]/', '', $this->filename); + + if(isset($options[Options::Directory])) + { + if(!file_exists($options[Options::Directory]) || !is_dir($options[Options::Directory])) + throw new InvalidArgumentException(sprintf('The directory %s does not exist or is not a a valid path', $options[Options::Directory])); + if(!is_writable($options[Options::Directory])) + throw new InvalidArgumentException(sprintf('The directory %s is not writable', $options[Options::Directory])); + + $this->filepath = $options[Options::Directory] . DIRECTORY_SEPARATOR . $this->filename; + } + else + { + $this->filepath = self::getTempDir() . DIRECTORY_SEPARATOR . $this->filename; + } if(!file_exists($this->filepath)) { @@ -79,14 +112,15 @@ /** * Generates a random string of a given length * + * @param int $length * @return string */ - private function randomString(): string + private static function randomString(int $length=8): string { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; - for ($i = 0; $i < 32; $i++) + for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } @@ -99,7 +133,7 @@ * @return string * @throws Exception */ - private function getTempDir(): string + private static function getTempDir(): string { if(function_exists('sys_get_temp_dir')) { diff --git a/tests/file_test.php b/tests/file_test.php index 84e0f1d..4f24699 100644 --- a/tests/file_test.php +++ b/tests/file_test.php @@ -3,7 +3,10 @@ require 'ncc'; import('net.nosial.tempfile'); - $temp = new \TempFile\TempFile('bin'); + $temp = new \TempFile\TempFile([ + \TempFile\Options::Extension => 'txt', + \TempFile\Options::Filename => 'test', + ]); print(sprintf('Tempfile: %s', $temp->getFilepath()) . PHP_EOL); file_put_contents($temp, 'Hello, world!');