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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ jobs:

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

- name: Run PHPUnit
env:
PROJECT_ROOT: ${{ github.workspace }}
PICAMATOR_TRANSFER_OBJECT_PROJECT_ROOT: ${{ github.workspace }}
run: composer phpunit
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
- "host.docker.internal:host-gateway"
environment:
XDEBUG_MODE: ${XDEBUG_MODE:-off}
PROJECT_ROOT: /home/transfer/transfer-object
PICAMATOR_TRANSFER_OBJECT_PROJECT_ROOT: ${PICAMATOR_TRANSFER_OBJECT_PROJECT_ROOT:-/home/transfer/transfer-object}
build:
context: docker/php
args:
Expand Down
42 changes: 42 additions & 0 deletions src/Shared/Environment/Enum/EnvironmentEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Picamator\TransferObject\Shared\Environment\Enum;

use Deprecated;

enum EnvironmentEnum: string
{
/**
* @api
*/
#[Deprecated(message: 'Please use PROJECT_ROOT instead.', since: '5.5.0')]
case PROJECT_ROOT_ALIAS = 'PROJECT_ROOT';

/**
* @api
*/
case PROJECT_ROOT = self::ENV_PREFIX . 'PROJECT_ROOT';

/**
* @api
*/
case MAX_FILE_SIZE_MB = self::ENV_PREFIX . 'MAX_FILE_SIZE_MB';

/**
* @api
*/
private const string ENV_PREFIX = 'PICAMATOR_TRANSFER_OBJECT_';

private const array DEFAULT_VALUES = [
self::PROJECT_ROOT->name => '',
self::PROJECT_ROOT_ALIAS->name => '',
self::MAX_FILE_SIZE_MB->name => '10',
];

public function getDefault(): string
{
return self::DEFAULT_VALUES[$this->name];
}
}
52 changes: 52 additions & 0 deletions src/Shared/Environment/EnvironmentReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Picamator\TransferObject\Shared\Environment;

use Picamator\TransferObject\Shared\Environment\Enum\EnvironmentEnum;

readonly class EnvironmentReader implements EnvironmentReaderInterface
{
public function getProjectRoot(): string
{
$projectRoot = $this->getEnvironment(EnvironmentEnum::PROJECT_ROOT)
?: $this->getEnvironment(EnvironmentEnum::PROJECT_ROOT_ALIAS);

if ($projectRoot === '') {
return $this->getcwd() ?: '';
}

$projectRoot = trim($projectRoot);

return rtrim($projectRoot, '\/');
}

public function getMaxFileSizeMegabytes(): string
{
return $this->getEnvironment(EnvironmentEnum::MAX_FILE_SIZE_MB);
}

private function getEnvironment(EnvironmentEnum $environment): string
{
$value = $this->getenv($environment->value);
if (is_string($value)) {
return $value;
}

return $environment->getDefault();
}

/**
* @return array<int, string>|false|string
*/
protected function getenv(string $name): array|false|string
{
return getenv($name);
}

protected function getcwd(): false|string
{
return getcwd();
}
}
12 changes: 12 additions & 0 deletions src/Shared/Environment/EnvironmentReaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Picamator\TransferObject\Shared\Environment;

interface EnvironmentReaderInterface
{
public function getProjectRoot(): string;

public function getMaxFileSizeMegabytes(): string;
}
12 changes: 11 additions & 1 deletion src/Shared/SharedFactoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Picamator\TransferObject\Shared;

use Picamator\TransferObject\Dependency\DependencyFactoryTrait;
use Picamator\TransferObject\Shared\Environment\EnvironmentReader;
use Picamator\TransferObject\Shared\Environment\EnvironmentReaderInterface;
use Picamator\TransferObject\Shared\Filesystem\FileAppender;
use Picamator\TransferObject\Shared\Filesystem\FileAppenderInterface;
use Picamator\TransferObject\Shared\Filesystem\FileReader;
Expand Down Expand Up @@ -75,7 +77,7 @@ final protected function createFileSizeValidator(): FileSizeValidatorInterface
{
return $this->getCached(
key: 'shared:FileSizeValidator',
factory: fn(): FileSizeValidatorInterface => new FileSizeValidator(),
factory: fn(): FileSizeValidatorInterface => new FileSizeValidator($this->createEnvironmentReader()),
);
}

Expand All @@ -102,4 +104,12 @@ final protected function createFileReader(): FileReaderInterface
factory: fn(): FileReaderInterface => new FileReader(),
);
}

final protected function createEnvironmentReader(): EnvironmentReaderInterface
{
return $this->getCached(
key: 'shared:EnvironmentReader',
factory: fn(): EnvironmentReaderInterface => new EnvironmentReader(),
);
}
}
18 changes: 15 additions & 3 deletions src/Shared/Validator/FileSizeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Picamator\TransferObject\Shared\Validator;

use Picamator\TransferObject\Generated\ValidatorMessageTransfer;
use Picamator\TransferObject\Shared\Environment\EnvironmentReaderInterface;

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

private const int MAX_FILE_SIZE_BYTES = 10_000_000;
private const int MAX_FILE_SIZE_BYTE_MULTIPLIER = 1_000_000;

public function __construct(private EnvironmentReaderInterface $environmentReader)
{
}

public function validate(string $path): ?ValidatorMessageTransfer
{
Expand All @@ -26,12 +31,14 @@ public function validate(string $path): ?ValidatorMessageTransfer
return $this->createErrorMessageTransfer($errorMessage);
}

if ($fileSize > self::MAX_FILE_SIZE_BYTES) {
$maxFileSizeBytes = $this->getMaxFileSizeBytes();

if ($fileSize > $maxFileSizeBytes) {
$errorMessage = sprintf(
self::MAX_SIZE_ERROR_MESSAGE_TEMPLATE,
$path,
$fileSize,
self::MAX_FILE_SIZE_BYTES
$maxFileSizeBytes,
);

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

private function getMaxFileSizeBytes(): int
{
return (int)$this->environmentReader->getMaxFileSizeMegabytes() * self::MAX_FILE_SIZE_BYTE_MULTIPLIER;
}

protected function filesize(string $path): int|false
{
return @filesize($path);
Expand Down
2 changes: 2 additions & 0 deletions src/Transfer/AbstractTransfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

/**
* @api
*
* @phpstan-consistent-constructor
*/
abstract class AbstractTransfer implements TransferInterface
{
Expand Down
9 changes: 1 addition & 8 deletions src/TransferGenerator/Config/ConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use ArrayObject;
use Picamator\TransferObject\Shared\SharedFactoryTrait;
use Picamator\TransferObject\TransferGenerator\Config\Environment\ConfigEnvironmentRender;
use Picamator\TransferObject\TransferGenerator\Config\Environment\ConfigEnvironmentRenderInterface;
use Picamator\TransferObject\TransferGenerator\Config\Loader\ConfigLoader;
use Picamator\TransferObject\TransferGenerator\Config\Loader\ConfigLoaderInterface;
use Picamator\TransferObject\TransferGenerator\Config\Parser\Builder\ConfigBuilder;
Expand Down Expand Up @@ -134,11 +132,6 @@ className: ConfigParser::class,

protected function createConfigContentBuilder(): ConfigContentBuilderInterface
{
return new ConfigContentBuilder($this->createConfigEnvironmentRender());
}

protected function createConfigEnvironmentRender(): ConfigEnvironmentRenderInterface
{
return new ConfigEnvironmentRender();
return new ConfigContentBuilder($this->createEnvironmentReader());
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,41 @@
namespace Picamator\TransferObject\TransferGenerator\Config\Parser\Builder;

use Picamator\TransferObject\Generated\ConfigContentTransfer;
use Picamator\TransferObject\TransferGenerator\Config\Environment\ConfigEnvironmentRenderInterface;
use Picamator\TransferObject\TransferGenerator\Config\Parser\Filter\ConfigNormalizerTrait;
use Picamator\TransferObject\Shared\Environment\EnvironmentReaderInterface;

readonly class ConfigContentBuilder implements ConfigContentBuilderInterface
{
use ConfigNormalizerTrait;
protected const string PLACEHOLDER = '${PROJECT_ROOT}';

public function __construct(
private ConfigEnvironmentRenderInterface $environmentRender,
) {
public function __construct(private EnvironmentReaderInterface $environmentReader)
{
}

public function createContentTransfer(array $configData): ConfigContentTransfer
{
$contentTransfer = new ConfigContentTransfer($configData);

return $this->renderPathKeys($contentTransfer);
}
$relativeDefinitionPath = $this->filterPath($contentTransfer->definitionPath);
$contentTransfer->relativeDefinitionPath = $this->replacePlaceholder($relativeDefinitionPath, '');

private function renderPathKeys(ConfigContentTransfer $contentTransfer): ConfigContentTransfer
{
$contentTransfer->relativeDefinitionPath =
$this->environmentRender->renderRelativeProjectRoot($contentTransfer->definitionPath);
$projectRoot = $this->environmentReader->getProjectRoot();

$contentTransfer->definitionPath =
$this->environmentRender->renderProjectRoot($contentTransfer->definitionPath);
$definitionPath = $this->replacePlaceholder($contentTransfer->definitionPath, $projectRoot);
$contentTransfer->definitionPath = $definitionPath;

$contentTransfer->transferPath =
$this->environmentRender->renderProjectRoot($contentTransfer->transferPath);
$transferPath = $this->replacePlaceholder($contentTransfer->transferPath, $projectRoot);
$contentTransfer->transferPath = $transferPath;

return $contentTransfer;
}

private function filterPath(string $path): string
{
return rtrim($path, '\/');
}

private function replacePlaceholder(string $path, string $replace): string
{
return str_replace(static::PLACEHOLDER, $replace, $path);
}
}
1 change: 1 addition & 0 deletions src/TransferGenerator/Definition/DefinitionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ protected function createDefinitionFinder(): DefinitionFinderInterface
{
return new DefinitionFinder(
$this->createFinder(),
$this->createEnvironmentReader(),
$this->getConfig(),
);
}
Expand Down
Loading