Skip to content

Commit 74e02f7

Browse files
authored
Merge pull request #55 from picamator/development
Release 5.5.0
2 parents e2c20c4 + cb3f19f commit 74e02f7

File tree

17 files changed

+298
-226
lines changed

17 files changed

+298
-226
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ jobs:
158158

159159
- name: Generate Transfer Objects
160160
env:
161-
PROJECT_ROOT: ${{ github.workspace }}
161+
PICAMATOR_TRANSFER_OBJECT_PROJECT_ROOT: ${{ github.workspace }}
162162
run: composer transfer-generate -- -c ./config/generator.config.yml
163163

164164
- name: Run PHPUnit
165165
env:
166-
PROJECT_ROOT: ${{ github.workspace }}
166+
PICAMATOR_TRANSFER_OBJECT_PROJECT_ROOT: ${{ github.workspace }}
167167
run: composer phpunit

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services:
55
- "host.docker.internal:host-gateway"
66
environment:
77
XDEBUG_MODE: ${XDEBUG_MODE:-off}
8-
PROJECT_ROOT: /home/transfer/transfer-object
8+
PICAMATOR_TRANSFER_OBJECT_PROJECT_ROOT: ${PICAMATOR_TRANSFER_OBJECT_PROJECT_ROOT:-/home/transfer/transfer-object}
99
build:
1010
context: docker/php
1111
args:
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Picamator\TransferObject\Shared\Environment\Enum;
6+
7+
use Deprecated;
8+
9+
enum EnvironmentEnum: string
10+
{
11+
/**
12+
* @api
13+
*/
14+
#[Deprecated(message: 'Please use PROJECT_ROOT instead.', since: '5.5.0')]
15+
case PROJECT_ROOT_ALIAS = 'PROJECT_ROOT';
16+
17+
/**
18+
* @api
19+
*/
20+
case PROJECT_ROOT = self::ENV_PREFIX . 'PROJECT_ROOT';
21+
22+
/**
23+
* @api
24+
*/
25+
case MAX_FILE_SIZE_MB = self::ENV_PREFIX . 'MAX_FILE_SIZE_MB';
26+
27+
/**
28+
* @api
29+
*/
30+
private const string ENV_PREFIX = 'PICAMATOR_TRANSFER_OBJECT_';
31+
32+
private const array DEFAULT_VALUES = [
33+
self::PROJECT_ROOT->name => '',
34+
self::PROJECT_ROOT_ALIAS->name => '',
35+
self::MAX_FILE_SIZE_MB->name => '10',
36+
];
37+
38+
public function getDefault(): string
39+
{
40+
return self::DEFAULT_VALUES[$this->name];
41+
}
42+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Picamator\TransferObject\Shared\Environment;
6+
7+
use Picamator\TransferObject\Shared\Environment\Enum\EnvironmentEnum;
8+
9+
readonly class EnvironmentReader implements EnvironmentReaderInterface
10+
{
11+
public function getProjectRoot(): string
12+
{
13+
$projectRoot = $this->getEnvironment(EnvironmentEnum::PROJECT_ROOT)
14+
?: $this->getEnvironment(EnvironmentEnum::PROJECT_ROOT_ALIAS);
15+
16+
if ($projectRoot === '') {
17+
return $this->getcwd() ?: '';
18+
}
19+
20+
$projectRoot = trim($projectRoot);
21+
22+
return rtrim($projectRoot, '\/');
23+
}
24+
25+
public function getMaxFileSizeMegabytes(): string
26+
{
27+
return $this->getEnvironment(EnvironmentEnum::MAX_FILE_SIZE_MB);
28+
}
29+
30+
private function getEnvironment(EnvironmentEnum $environment): string
31+
{
32+
$value = $this->getenv($environment->value);
33+
if (is_string($value)) {
34+
return $value;
35+
}
36+
37+
return $environment->getDefault();
38+
}
39+
40+
/**
41+
* @return array<int, string>|false|string
42+
*/
43+
protected function getenv(string $name): array|false|string
44+
{
45+
return getenv($name);
46+
}
47+
48+
protected function getcwd(): false|string
49+
{
50+
return getcwd();
51+
}
52+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Picamator\TransferObject\Shared\Environment;
6+
7+
interface EnvironmentReaderInterface
8+
{
9+
public function getProjectRoot(): string;
10+
11+
public function getMaxFileSizeMegabytes(): string;
12+
}

src/Shared/SharedFactoryTrait.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Picamator\TransferObject\Shared;
66

77
use Picamator\TransferObject\Dependency\DependencyFactoryTrait;
8+
use Picamator\TransferObject\Shared\Environment\EnvironmentReader;
9+
use Picamator\TransferObject\Shared\Environment\EnvironmentReaderInterface;
810
use Picamator\TransferObject\Shared\Filesystem\FileAppender;
911
use Picamator\TransferObject\Shared\Filesystem\FileAppenderInterface;
1012
use Picamator\TransferObject\Shared\Filesystem\FileReader;
@@ -75,7 +77,7 @@ final protected function createFileSizeValidator(): FileSizeValidatorInterface
7577
{
7678
return $this->getCached(
7779
key: 'shared:FileSizeValidator',
78-
factory: fn(): FileSizeValidatorInterface => new FileSizeValidator(),
80+
factory: fn(): FileSizeValidatorInterface => new FileSizeValidator($this->createEnvironmentReader()),
7981
);
8082
}
8183

@@ -102,4 +104,12 @@ final protected function createFileReader(): FileReaderInterface
102104
factory: fn(): FileReaderInterface => new FileReader(),
103105
);
104106
}
107+
108+
final protected function createEnvironmentReader(): EnvironmentReaderInterface
109+
{
110+
return $this->getCached(
111+
key: 'shared:EnvironmentReader',
112+
factory: fn(): EnvironmentReaderInterface => new EnvironmentReader(),
113+
);
114+
}
105115
}

src/Shared/Validator/FileSizeValidator.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Picamator\TransferObject\Shared\Validator;
66

77
use Picamator\TransferObject\Generated\ValidatorMessageTransfer;
8+
use Picamator\TransferObject\Shared\Environment\EnvironmentReaderInterface;
89

910
readonly class FileSizeValidator implements FileSizeValidatorInterface
1011
{
@@ -15,7 +16,11 @@
1516
File "%s" ("%d" bytes) exceeds the maximum allowed size of "%d" bytes. Please split the file into smaller parts.
1617
TEMPLATE;
1718

18-
private const int MAX_FILE_SIZE_BYTES = 10_000_000;
19+
private const int MAX_FILE_SIZE_BYTE_MULTIPLIER = 1_000_000;
20+
21+
public function __construct(private EnvironmentReaderInterface $environmentReader)
22+
{
23+
}
1924

2025
public function validate(string $path): ?ValidatorMessageTransfer
2126
{
@@ -26,12 +31,14 @@ public function validate(string $path): ?ValidatorMessageTransfer
2631
return $this->createErrorMessageTransfer($errorMessage);
2732
}
2833

29-
if ($fileSize > self::MAX_FILE_SIZE_BYTES) {
34+
$maxFileSizeBytes = $this->getMaxFileSizeBytes();
35+
36+
if ($fileSize > $maxFileSizeBytes) {
3037
$errorMessage = sprintf(
3138
self::MAX_SIZE_ERROR_MESSAGE_TEMPLATE,
3239
$path,
3340
$fileSize,
34-
self::MAX_FILE_SIZE_BYTES
41+
$maxFileSizeBytes,
3542
);
3643

3744
return $this->createErrorMessageTransfer($errorMessage);
@@ -40,6 +47,11 @@ public function validate(string $path): ?ValidatorMessageTransfer
4047
return null;
4148
}
4249

50+
private function getMaxFileSizeBytes(): int
51+
{
52+
return (int)$this->environmentReader->getMaxFileSizeMegabytes() * self::MAX_FILE_SIZE_BYTE_MULTIPLIER;
53+
}
54+
4355
protected function filesize(string $path): int|false
4456
{
4557
return @filesize($path);

src/Transfer/AbstractTransfer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
/**
1212
* @api
13+
*
14+
* @phpstan-consistent-constructor
1315
*/
1416
abstract class AbstractTransfer implements TransferInterface
1517
{

src/TransferGenerator/Config/ConfigFactory.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
use ArrayObject;
88
use Picamator\TransferObject\Shared\SharedFactoryTrait;
9-
use Picamator\TransferObject\TransferGenerator\Config\Environment\ConfigEnvironmentRender;
10-
use Picamator\TransferObject\TransferGenerator\Config\Environment\ConfigEnvironmentRenderInterface;
119
use Picamator\TransferObject\TransferGenerator\Config\Loader\ConfigLoader;
1210
use Picamator\TransferObject\TransferGenerator\Config\Loader\ConfigLoaderInterface;
1311
use Picamator\TransferObject\TransferGenerator\Config\Parser\Builder\ConfigBuilder;
@@ -134,11 +132,6 @@ className: ConfigParser::class,
134132

135133
protected function createConfigContentBuilder(): ConfigContentBuilderInterface
136134
{
137-
return new ConfigContentBuilder($this->createConfigEnvironmentRender());
138-
}
139-
140-
protected function createConfigEnvironmentRender(): ConfigEnvironmentRenderInterface
141-
{
142-
return new ConfigEnvironmentRender();
135+
return new ConfigContentBuilder($this->createEnvironmentReader());
143136
}
144137
}

src/TransferGenerator/Config/Environment/ConfigEnvironmentRender.php

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)