Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .github/workflows/codesniffer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ name: "Codesniffer"

on:
pull_request:

workflow_dispatch:
push:
branches: ["*"]

schedule:
- cron: "0 8 * * 1"

jobs:
codesniffer:
name: "Codesniffer"
uses: contributte/.github/.github/workflows/codesniffer.yml@v1
with:
php: "8.2"
uses: contributte/.github/.github/workflows/codesniffer.yml@master
7 changes: 3 additions & 4 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ name: "Coverage"

on:
pull_request:

workflow_dispatch:
push:
branches: ["*"]

schedule:
- cron: "0 8 * * 1"

jobs:
coverage:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester-coverage.yml@v1
uses: contributte/.github/.github/workflows/nette-tester-coverage-v2.yml@master
with:
php: "8.2"
php: "8.4"
7 changes: 2 additions & 5 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ name: "Phpstan"

on:
pull_request:

workflow_dispatch:
push:
branches: ["*"]

schedule:
- cron: "0 8 * * 1"

jobs:
phpstan:
name: "Phpstan"
uses: contributte/.github/.github/workflows/phpstan.yml@v1
with:
php: "8.2"
uses: contributte/.github/.github/workflows/phpstan.yml@master
27 changes: 16 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@ name: "Nette Tester"

on:
pull_request:

workflow_dispatch:
push:
branches: [ "*" ]

branches: ["*"]
schedule:
- cron: "0 8 * * 1"

jobs:
test82:
test84:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester.yml@v1
uses: contributte/.github/.github/workflows/nette-tester.yml@master
with:
php: "8.2"
php: "8.4"

test83:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester.yml@master
with:
php: "8.3"

test81:
test82:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester.yml@v1
uses: contributte/.github/.github/workflows/nette-tester.yml@master
with:
php: "8.1"
php: "8.2"

testlower:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester.yml@v1
uses: contributte/.github/.github/workflows/nette-tester.yml@master
with:
php: "8.1"
php: "8.2"
composer: "composer update --no-interaction --no-progress --prefer-dist --prefer-stable --prefer-lowest"
17 changes: 6 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
.PHONY: install
.PHONY: install qa cs csf phpstan tests coverage

install:
composer update

.PHONY: qa
qa: phpstan cs

.PHONY: cs
cs:
ifdef GITHUB_ACTION
vendor/bin/phpcs --standard=ruleset.xml --encoding=utf-8 --extensions="php,phpt" --colors -nsp -q --report=checkstyle src tests | cs2pr
vendor/bin/phpcs --standard=ruleset.xml --encoding=utf-8 --colors -nsp --extensions=php,phpt -q --report=checkstyle src tests | cs2pr
else
vendor/bin/phpcs --standard=ruleset.xml --encoding=utf-8 --extensions="php,phpt" --colors -nsp src tests
vendor/bin/phpcs --standard=ruleset.xml --encoding=utf-8 --colors -nsp --extensions=php,phpt src tests
endif

.PHONY: csf
csf:
vendor/bin/phpcbf --standard=ruleset.xml --encoding=utf-8 --extensions="php,phpt" --colors -nsp src tests
vendor/bin/phpcbf --standard=ruleset.xml --encoding=utf-8 --colors -nsp --extensions=php,phpt src tests

.PHONY: phpstan
phpstan:
vendor/bin/phpstan analyse -c phpstan.neon --memory-limit=256M
vendor/bin/phpstan analyse -c phpstan.neon

.PHONY: tests
tests:
vendor/bin/tester -s -p php --colors 1 -C tests/Cases

.PHONY: coverage
coverage:
ifdef GITHUB_ACTION
vendor/bin/tester -s -p phpdbg --colors 1 -C --coverage coverage.xml --coverage-src src tests/Cases
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
}
],
"require": {
"php": ">=8.1",
"php": ">=8.2",
"nette/utils": "^4.0.0"
},
"require-dev": {
"nette/di": "^3.2.4",
"contributte/phpstan": "^0.2",
"contributte/qa": "^0.4",
"contributte/tester": "^0.4",
"contributte/phpstan": "^0.2",
"mockery/mockery": "^1.5.0"
"mockery/mockery": "^1.6.0",
"nette/di": "^3.2.4"
},
"suggest": {
"nette/di": "to use DateTimeExtension[CompilerExtension]"
Expand Down Expand Up @@ -54,7 +54,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.7.x-dev"
"dev-master": "0.8.x-dev"
}
}
}
4 changes: 1 addition & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ includes:

parameters:
level: 9
phpVersion: 80100
phpVersion: 80200

scanDirectories:
- src
Expand All @@ -14,5 +14,3 @@ parameters:
paths:
- src
- .docs

ignoreErrors:
2 changes: 1 addition & 1 deletion ruleset.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Contributte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
<!-- Rulesets -->
<rule ref="./vendor/contributte/qa/ruleset-8.0.xml">
<rule ref="./vendor/contributte/qa/ruleset-8.2.xml">
<exclude name="SlevomatCodingStandard.PHP.DisallowReference.DisallowedPassingByReference"/>
</rule>

Expand Down
6 changes: 2 additions & 4 deletions src/Deeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ class Deeper

/**
* @template T
* @param array-key $key
* @param array<T> $arr
* @param non-empty-string $sep
*/
public static function has($key, array $arr, string $sep = '.'): bool
public static function has(string|int $key, array $arr, string $sep = '.'): bool
{
try {
static::get($key, $arr, $sep);
Expand All @@ -26,13 +25,12 @@ public static function has($key, array $arr, string $sep = '.'): bool

/**
* @template T
* @param array-key $key
* @param array<T> $arr
* @param non-empty-string $sep
* @param ?T $default
* @return ?T
*/
public static function get($key, array $arr, string $sep = '.', $default = null)
public static function get(string|int $key, array $arr, string $sep = '.', mixed $default = null): mixed
{
if (func_num_args() < 4) {
return Arrays::get($arr, static::flat($key, $sep));
Expand Down
15 changes: 15 additions & 0 deletions src/Exception/Runtime/FileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);

namespace Contributte\Utils\Exception\Runtime;

use Contributte\Utils\Exception\RuntimeException;

class FileNotFoundException extends RuntimeException
{

public function __construct(string $path)
{
parent::__construct(sprintf('File not found at path "%s"', $path));
}

}
104 changes: 104 additions & 0 deletions src/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php declare(strict_types = 1);

namespace Contributte\Utils;

use Contributte\Utils\Exception\Runtime\FileNotFoundException;
use Contributte\Utils\Http\FileResponse;
use Nette\Utils\FileSystem as NetteFileSystem;
use Nette\Utils\Image;

class File
{

private string $path;

private string|null $title;

public function __construct(string $path, string|null $title = null)
{
$this->path = FileSystem::pathalize($path);
$this->title = $title;
}

public function getName(): string
{
return basename($this->path);
}

/**
* @throws FileNotFoundException
*/
public function getSize(): int
{
$this->assertExists();

$size = filesize($this->path);

return $size !== false ? $size : 0;
}

public function getTitle(): string|null
{
return $this->title;
}

public function getPath(): string
{
return $this->path;
}

public function exists(): bool
{
return file_exists($this->path) && is_file($this->path);
}

/**
* @throws FileNotFoundException
*/
public function move(string $destination, bool $overwrite = true): static
{
$this->assertExists();

$destination = FileSystem::pathalize($destination);
NetteFileSystem::rename($this->path, $destination, $overwrite);
$this->path = $destination;

return $this;
}

/**
* @throws FileNotFoundException
*/
public function toImage(): Image
{
$this->assertExists();

return Image::fromFile($this->path);
}

/**
* @throws FileNotFoundException
*/
public function toResponse(string|null $name = null, string|null $contentType = null, bool $forceDownload = true): FileResponse
{
$this->assertExists();

return new FileResponse(
$this->path,
$name ?? $this->title ?? $this->getName(),
$contentType,
$forceDownload
);
}

/**
* @throws FileNotFoundException
*/
private function assertExists(): void
{
if (!$this->exists()) {
throw new FileNotFoundException($this->path);
}
}

}
Loading