Skip to content
Open
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
132 changes: 132 additions & 0 deletions Multipart/UploadedFileStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/*
* This file is part of the auto1-oss/service-api-components-bundle.
*
* (c) AUTO1 Group SE https://www.auto1-group.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Auto1\ServiceAPIComponentsBundle\Multipart;

use Psr\Http\Message\StreamInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;

final class UploadedFileStream implements StreamInterface
{
private StreamInterface $inner;

private UploadedFile $uploadedFile;

public function __construct(StreamInterface $inner, UploadedFile $uploadedFile)
{
$this->inner = $inner;
$this->uploadedFile = $uploadedFile;
}

public function __toString(): string
{
return (string) $this->inner;
}

public function close(): void
{
$this->inner->close();
}

public function detach()
{
return $this->inner->detach();
}

public function getSize(): ?int
{
return $this->inner->getSize();
}

public function tell(): int
{
return $this->inner->tell();
}

public function eof(): bool
{
return $this->inner->eof();
}

public function isSeekable(): bool
{
return $this->inner->isSeekable();
}

public function seek(int $offset, int $whence = SEEK_SET): void
{
$this->inner->seek($offset, $whence);
}

public function rewind(): void
{
$this->inner->rewind();
}

public function isWritable(): bool
{
return $this->inner->isWritable();
}

public function write(string $string): int
{
return $this->inner->write($string);
}

public function isReadable(): bool
{
return $this->inner->isReadable();
}

public function read(int $length): string
{
return $this->inner->read($length);
}

public function getContents(): string
{
return $this->inner->getContents();
}

public function getMetadata(?string $key = null)
{
if ('mime-type' === $key) {
return $this->uploadedFile->getClientMimeType();
}

if ('filename' === $key) {
return $this->uploadedFile->getClientOriginalName();
}

if (null === $key) {
$innerMetadata = $this->inner->getMetadata();

return array_merge(
is_array($innerMetadata) ? $innerMetadata : [],
[
'mime-type' => $this->uploadedFile->getClientMimeType(),
'filename' => $this->uploadedFile->getClientOriginalName(),
]
);
}

return $this->inner->getMetadata($key);
}

/**
* Escape hatch for code that needs Symfony-specific file operations
*/
public function getUploadedFile(): UploadedFile
{
return $this->uploadedFile;
}
}
24 changes: 20 additions & 4 deletions Resources/config/symfony_services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ services:
auto1.api.response.serializer:
class: Symfony\Component\Serializer\Serializer
arguments:
- ['@auto1.api.serializer.denormalizer.array_access', '@auto1.api.serializer.denormalizer.array', '@auto1.api.serializer.normalizer.datetime', '@auto1.api.response.transformer.normalizer']
- ['@serializer.encoder.json']
$normalizers:
- '@auto1.api.serializer.denormalizer.array_access'
- '@auto1.api.serializer.denormalizer.array'
- '@auto1.api.serializer.normalizer.datetime'
- '@auto1.api.response.transformer.normalizer'
$encoders:
- '@serializer.encoder.json'

auto1.api.serializer.denormalizer.array_access:
class: Auto1\ServiceAPIComponentsBundle\Service\Serializer\Normalizer\ArrayAccessNormalizer
Expand All @@ -67,8 +72,19 @@ services:
- '@serializer.property_accessor'
- '@property_info'

auto1.api.serializer.normalizer.stream_interface:
class: Auto1\ServiceAPIComponentsBundle\Service\Serializer\Normalizer\StreamInterfaceDenormalizer

auto1.api.request.serializer:
class: Symfony\Component\Serializer\Serializer
arguments:
- ['@auto1.api.serializer.normalizer.datetime', '@auto1.api.serializer.denormalizer.array', '@auto1.api.serializer.normalizer.object', '@auto1.api.serializer.normalizer.get_set_method']
- ['@auto1.api.encoder.json', '@auto1.api.encoder.url', '@auto1.api.encoder.void']
$normalizers:
- '@auto1.api.serializer.normalizer.datetime'
- '@auto1.api.serializer.normalizer.stream_interface'
- '@auto1.api.serializer.denormalizer.array'
- '@auto1.api.serializer.normalizer.object'
- '@auto1.api.serializer.normalizer.get_set_method'
$encoders:
- '@auto1.api.encoder.json'
- '@auto1.api.encoder.url'
- '@auto1.api.encoder.void'
6 changes: 1 addition & 5 deletions Service/Logger/LoggerAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,9 @@ protected function getLogger() : LoggerInterface

/**
* @param LoggerInterface $logger
*
* @return $this
*/
public function setLogger(LoggerInterface $logger)
public function setLogger(LoggerInterface $logger) : void
{
$this->traitLogger = $logger;

return $this;
}
}
45 changes: 45 additions & 0 deletions Service/Serializer/Normalizer/StreamInterfaceDenormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the auto1-oss/service-api-handler-bundle.
*
* (c) AUTO1 Group SE https://www.auto1-group.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Auto1\ServiceAPIComponentsBundle\Service\Serializer\Normalizer;

use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

class StreamInterfaceDenormalizer implements DenormalizerInterface
{
public function denormalize($data, string $type, ?string $format = null, array $context = []): ?StreamInterface
{
if (null === $data) {
return null;
}

if (!$data instanceof StreamInterface) {
throw new InvalidArgumentException(
sprintf(
'Expected an instance of "%s", got "%s".',
StreamInterface::class,
is_object($data) ? get_class($data) : gettype($data)
)
);
}

return $data;
}

public function supportsDenormalization($data, string $type, ?string $format = null, array $context = []): bool
{
return is_a($type, StreamInterface::class, true);
}
}
Loading