Compare commits

...

7 commits

Author SHA1 Message Date
netkas
cba1db6a7c Bumped version & updated CHANGELOG.md 2024-09-29 21:24:54 -04:00
netkas
52d6a45bda Updated .gitignore 2024-09-29 21:22:03 -04:00
netkas
e000f2b359 Add PHPUnit tests for TempFile class 2024-09-29 21:21:48 -04:00
netkas
2bf304c5ef Refactor TempFile class field types and improve filename validation 2024-09-29 21:15:01 -04:00
netkas
fbba022431 Update PHP language level to 8.3 2024-09-29 21:10:00 -04:00
netkas
1f661a9324 Refactor TempFile class and improve error handling 2024-09-29 21:09:49 -04:00
netkas
817a27afc3 Refactor build system and add CI pipeline 2024-09-29 21:06:03 -04:00
11 changed files with 340 additions and 49 deletions

162
.github/workflows/ncc_workflow.yml vendored Normal file
View file

@ -0,0 +1,162 @@
name: CI
on:
push:
branches:
- '**'
release:
types: [created]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
container:
image: php:8.3
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install dependencies
run: |
apt update -yqq
apt install git libpq-dev libzip-dev zip make wget gnupg -yqq
- name: Install phive
run: |
wget -O phive.phar https://phar.io/releases/phive.phar
wget -O phive.phar.asc https://phar.io/releases/phive.phar.asc
gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79
gpg --verify phive.phar.asc phive.phar
chmod +x phive.phar
mv phive.phar /usr/local/bin/phive
- name: Install phab
run: |
phive install phpab --global --trust-gpg-keys 0x2A8299CE842DD38C
- name: Install latest version of NCC
run: |
git clone https://git.n64.cc/nosial/ncc.git
cd ncc
make redist
NCC_DIR=$(find build/ -type d -name "ncc_*" | head -n 1)
if [ -z "$NCC_DIR" ]; then
echo "NCC build directory not found"
exit 1
fi
php "$NCC_DIR/INSTALL" --auto
cd .. && rm -rf ncc
- name: Build project
run: |
ncc build --config release --log-level debug
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: TempFile_build
path: build/release/net.nosial.tempfile.ncc
check-phpunit:
runs-on: ubuntu-latest
outputs:
phpunit-exists: ${{ steps.check.outputs.phpunit-exists }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Check for phpunit.xml
id: check
run: |
if [ -f phpunit.xml ]; then
echo "phpunit-exists=true" >> $GITHUB_OUTPUT
else
echo "phpunit-exists=false" >> $GITHUB_OUTPUT
fi
test:
needs: [build, check-phpunit]
runs-on: ubuntu-latest
container:
image: php:8.3
if: needs.check-phpunit.outputs.phpunit-exists == 'true'
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: TempFile_build
path: TempFile_build # Adjust this to download the artifact directly under 'TempFile_build'
- name: Install dependencies
run: |
apt update -yqq
apt install git libpq-dev libzip-dev zip make wget gnupg -yqq
curl -sSLf -o /usr/local/bin/install-php-extensions https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions
chmod +x /usr/local/bin/install-php-extensions
install-php-extensions zip
- name: Install phive
run: |
wget -O phive.phar https://phar.io/releases/phive.phar
wget -O phive.phar.asc https://phar.io/releases/phive.phar.asc
gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79
gpg --verify phive.phar.asc phive.phar
chmod +x phive.phar
mv phive.phar /usr/local/bin/phive
- name: Install phab
run: |
phive install phpab --global --trust-gpg-keys 0x2A8299CE842DD38C
- name: Install latest version of NCC
run: |
git clone https://git.n64.cc/nosial/ncc.git
cd ncc
make redist
NCC_DIR=$(find build/ -type d -name "ncc_*" | head -n 1)
if [ -z "$NCC_DIR" ]; then
echo "NCC build directory not found"
exit 1
fi
php "$NCC_DIR/INSTALL" --auto
cd .. && rm -rf ncc
- name: Install NCC packages
run: |
ncc package install --package="TempFile_build/net.nosial.tempfile.ncc" --build-source --reinstall -y --log-level debug
- name: Run PHPUnit tests
run: |
wget https://phar.phpunit.de/phpunit-11.3.phar
php phpunit-11.3.phar --configuration phpunit.xml
release:
needs: [build, test]
permissions: write-all
runs-on: ubuntu-latest
container:
image: php:8.3
if: github.event_name == 'release'
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: TempFile_build
path: TempFile_build
- name: Upload to GitHub Release
uses: softprops/action-gh-release@v1
with:
files: |
TempFile_build/net.nosial.tempfile.ncc
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

4
.gitignore vendored
View file

@ -1 +1,3 @@
build/
build/
/.phpunit.result.cache
/.idea/php-test-framework.xml

7
.idea/php.xml generated
View file

@ -15,10 +15,15 @@
<path value="/etc/ncc" />
</include_path>
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="8.2" />
<component name="PhpProjectSharedConfiguration" php_language_level="8.3" />
<component name="PhpStanOptionsConfiguration">
<option name="transferred" value="true" />
</component>
<component name="PhpUnit">
<phpunit_settings>
<PhpUnitSettings load_method="PHPUNIT_PHAR" custom_loader_path="$USER_HOME$/phar/phpunit.phar" phpunit_phar_path="$USER_HOME$/phar/phpunit.phar" />
</phpunit_settings>
</component>
<component name="PsalmOptionsConfiguration">
<option name="transferred" value="true" />
</component>

View file

@ -5,6 +5,21 @@ 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.2.0] - 2024-09-29
This update introduces an update for ncc 2.1+ & PHP 8.3 compatibility.
### Added
- Added PHPUnit tests for TempFile class
### Changed
- Refactor build system and add CI pipeline
- Refactor TempFile class and improve error handling
- Update PHP language level to 8.3
- Refactor TempFile class filed types and improves filename validation
- Updated .gitignore file
## [1.1.0] - 2023-02-26
### Changed
@ -12,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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

View file

@ -1,8 +1,23 @@
release:
ncc build --config="release"
# Variables
CONFIG ?= release
LOG_LEVEL = debug
OUTDIR = build/$(CONFIG)
PACKAGE = $(OUTDIR)/net.nosial.tempfile.ncc
install:
ncc package install --package="build/release/net.nosial.tempfile.ncc" --skip-dependencies --reinstall -y
# Default Target
all: build
uninstall:
ncc package uninstall -y --package="net.nosial.tempfile"
# Build Steps
build:
ncc build --config=$(CONFIG) --log-level $(LOG_LEVEL)
install: build
ncc package install --package=$(PACKAGE) --skip-dependencies --build-source --reinstall -y --log-level $(LOG_LEVEL)
test: build
phpunit
clean:
rm -rf build
.PHONY: all build install test clean

3
bootstrap.php Normal file
View file

@ -0,0 +1,3 @@
<?php
require 'ncc';
import('net.nosial.tempfile');

11
phpunit.xml Normal file
View file

@ -0,0 +1,11 @@
<phpunit bootstrap="bootstrap.php">
<testsuites>
<testsuite name="TempFile Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<ini name="error_reporting" value="-1"/>
<server name="KERNEL_DIR" value="app/"/>
</php>
</phpunit>

View file

@ -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.1.0",
"version": "1.2.0",
"uuid": "910f98fe-b4c9-11ed-b13f-fdc283a6db6d"
},
"build": {
@ -21,7 +21,16 @@
"configurations": [
{
"name": "release",
"output_path": "build/release"
"build_type": "ncc",
"output": "build/release/%ASSEMBLY.PACKAGE%.ncc"
},
{
"name": "debug",
"build_type": "ncc",
"output": "build/debug/%ASSEMBLY.PACKAGE%.ncc",
"define_constants": {
"DEBUG": "1"
}
}
]
}

View file

@ -1,38 +1,38 @@
<?php
/** @noinspection PhpMissingFieldTypeInspection */
namespace TempFile;
use Exception;
use InvalidArgumentException;
use ncc\Runtime;
use RuntimeException;
class TempFile
{
private const string RANDOM_CHARACTERS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
/**
* An array of temporary files to be deleted on shutdown
*
* @var string[]
*/
private static $temporary_files = [];
private static array $temporary_files = [];
/**
* Indicates whether the shutdown handler has been registered
*
* @var bool
*/
private static $shutdown_handler_registered = false;
private static bool $shutdown_handler_registered = false;
/**
* @var string
*/
private $filename;
private string $filename;
/**
* @var string
*/
private $filepath;
private string $filepath;
/**
* Create a new temporary file with optional options:
@ -50,15 +50,25 @@
foreach($options as $option => $value)
{
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));
}
}
if(!isset($options[Options::Extension]))
{
$options[Options::Extension] = 'tmp';
}
if(!isset($options[Options::RandomLength]))
{
$options[Options::RandomLength] = 8;
}
if(isset($options[Options::Filename]))
{
@ -70,18 +80,40 @@
}
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);
$replaced = preg_replace('/[^a-zA-Z0-9.\-_]/', '', $this->filename);
if($replaced === false)
{
throw new InvalidArgumentException('The filename contains invalid characters');
}
if(is_array($replaced))
{
$replaced = implode('', $replaced);
}
$this->filename = $replaced;
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;
}
@ -101,7 +133,6 @@
}
self::$temporary_files[] = $this->filepath;
if(!self::$shutdown_handler_registered && function_exists('register_shutdown_function'))
{
register_shutdown_function([self::class, 'shutdownHandler']);
@ -117,13 +148,14 @@
*/
private static function randomString(int $length=8): string
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$charactersLength = strlen(self::RANDOM_CHARACTERS);
$randomString = '';
for ($i = 0; $i < $length; $i++)
{
$randomString .= $characters[rand(0, $charactersLength - 1)];
$randomString .= self::RANDOM_CHARACTERS[rand(0, $charactersLength - 1)];
}
return $randomString;
}
@ -153,15 +185,6 @@
}
}
try
{
return Runtime::getDataPath('net.nosial.tempfile');
}
catch(Exception $e)
{
unset($e);
}
$local_tmp = getcwd() . DIRECTORY_SEPARATOR . 'temp';
if(is_writeable(getcwd()))
@ -174,7 +197,7 @@
return $local_tmp;
}
throw new Exception('Unable to find a suitable temporary directory');
throw new RuntimeException('Unable to find a suitable temporary directory');
}
/**

View file

@ -0,0 +1,62 @@
<?php
namespace TempFile;
use PHPUnit\Framework\TestCase;
/**
* Tests for the TempFile class.
*/
class TempFileTest extends TestCase
{
/**
* Tests the __construct method of TempFile class.
*/
public function testConstruct()
{
// Test with default options.
$tempFile = new TempFile(null);
$this->assertTrue(is_file($tempFile->getFilepath()));
$this->assertStringEndsWith('.tmp', $tempFile->getFilename());
// Test with custom options.
$customOptions = [
Options::Extension => 'txt',
Options::Filename => 'testfile',
Options::Prefix => 'prefix_',
Options::Suffix => '_suffix',
Options::RandomLength => 5,
Options::Directory => sys_get_temp_dir(),
];
$tempFile = new TempFile($customOptions);
$this->assertStringEndsWith('.txt', $tempFile->getFilename());
$this->assertStringStartsWith('prefix_', $tempFile->getFilename());
$this->assertStringEndsWith('_suffix.txt', $tempFile->getFilename());
$this->assertSame($customOptions[Options::Directory], dirname($tempFile->getFilepath()));
// Test when a non-string and non-integer value is given to any options.
$customOptions[Options::Prefix] = [];
$this->expectException(\InvalidArgumentException::class);
new TempFile($customOptions);
// Test when an invalid option is given.
$customOptions = ['invalid_option' => 'value'];
$this->expectException(\InvalidArgumentException::class);
new TempFile($customOptions);
// Test when a directory that does not exist is given.
$customOptions = [Options::Directory => '/nonexistent/directory'];
$this->expectException(\InvalidArgumentException::class);
new TempFile($customOptions);
// Test when a directory that is not writable is given.
$customOptions = [Options::Directory => '/'];
$this->expectException(\InvalidArgumentException::class);
new TempFile($customOptions);
// Test if is writeable
$customOptions = [Options::Directory => sys_get_temp_dir()];
$tempFile = new TempFile($customOptions);
$this->assertTrue(is_writable($tempFile->getFilepath()));
}
}

View file

@ -1,17 +0,0 @@
<?php
require 'ncc';
import('net.nosial.tempfile');
$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!');
print(sprintf('Filesize: %s', filesize($temp->getFilepath())) . PHP_EOL);
sleep(10);
print('Exiting...' . PHP_EOL);
exit(0);