Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.x' into 156-dependencies
Browse files Browse the repository at this point in the history
# Conflicts:
#	qodana.sarif.json
#	src/OpenApi/Config/Tags.php
  • Loading branch information
mattamon committed May 17, 2024
2 parents 66e8044 + da91d99 commit 04febb6
Show file tree
Hide file tree
Showing 45 changed files with 2,467 additions and 17 deletions.
6 changes: 5 additions & 1 deletion config/assets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ services:

# Hydrators
Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomSettingsHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomSettingsHydrator
class: Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomSettingsHydrator

# Encoder
Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoderInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoder
23 changes: 23 additions & 0 deletions config/properties.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
Pimcore\Bundle\StudioBackendBundle\Property\Controller\:
resource: '../src/Property/Controller'
public: true
tags: [ 'controller.service_arguments' ]


Pimcore\Bundle\StudioBackendBundle\Property\Repository\PropertyRepositoryInterface:
class: Pimcore\Bundle\StudioBackendBundle\Property\Repository\PropertyRepository

Pimcore\Bundle\StudioBackendBundle\Property\Service\PropertyServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Property\Service\PropertyService

Pimcore\Bundle\StudioBackendBundle\Property\Hydrator\PropertyHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Property\Hydrator\PropertyHydrator

79 changes: 79 additions & 0 deletions src/Asset/Controller/Data/TextController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Controller\Data;

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoderInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content\DataJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\MethodNotAllowedResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\NotFoundResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnauthorizedResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnprocessableContentResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnsupportedMediaTypeResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\ElementProviderTrait;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class TextController extends AbstractApiController
{
use ElementProviderTrait;

public function __construct(
SerializerInterface $serializer,
private readonly ServiceResolverInterface $serviceResolver,
private readonly TextEncoderInterface $textEncoder,

) {
parent::__construct($serializer);
}

#[Route('/assets/{id}/text', name: 'pimcore_studio_api_get_asset_data_text', methods: ['GET'])]
//#[IsGranted('STUDIO_API')]
//#[IsGranted(UserPermissions::ASSETS->value)]
#[Get(
path: self::API_PATH . '/assets/{id}/text',
operationId: 'getAssetDataTextById',
summary: 'Get asset data in text UTF8 representation by id',
security: self::SECURITY_SCHEME,
tags: [Tags::Assets->name]
)]
#[IdParameter(type: 'asset')]
#[SuccessResponse(
description: 'UTF8 encoded text data',
content: new DataJson('UTF 8 encoded text data')
)]
#[UnauthorizedResponse]
#[NotFoundResponse]
#[MethodNotAllowedResponse]
#[UnsupportedMediaTypeResponse]
#[UnprocessableContentResponse]
public function getTextData(int $id): JsonResponse
{
$element = $this->getElement($this->serviceResolver, 'asset', $id);

return $this->jsonResponse(['data' => $this->textEncoder->encodeUTF8($element)]);
}
}
44 changes: 44 additions & 0 deletions src/Asset/Encoder/TextEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Encoder;

use ForceUTF8\Encoding;
use Pimcore\Bundle\StudioBackendBundle\Exception\InvalidElementTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\MaxFileSizeExceededException;
use Pimcore\Model\Asset\Text;
use Pimcore\Model\Element\ElementInterface;

final class TextEncoder implements TextEncoderInterface
{
private const MAX_FILE_SIZE = 2000000;

/**
* @throws InvalidElementTypeException|MaxFileSizeExceededException
*/
public function encodeUTF8(ElementInterface $element): string
{
if (!$element instanceof Text) {
throw new InvalidElementTypeException('Element must be an instance of Text');
}

if ($element->getFileSize() > self::MAX_FILE_SIZE) {
throw new MaxFileSizeExceededException(self::MAX_FILE_SIZE);
}

return Encoding::toUTF8($element->getData());
}
}
29 changes: 29 additions & 0 deletions src/Asset/Encoder/TextEncoderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Encoder;

use Pimcore\Bundle\StudioBackendBundle\Exception\InvalidElementTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\MaxFileSizeExceededException;
use Pimcore\Model\Element\ElementInterface;

interface TextEncoderInterface
{
/**
* @throws InvalidElementTypeException|MaxFileSizeExceededException
*/
public function encodeUTF8(ElementInterface $element): string;
}
1 change: 1 addition & 0 deletions src/DependencyInjection/PimcoreStudioBackendExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('filters.yaml');
$loader->load('icon.yaml');
$loader->load('open_api.yaml');
$loader->load('properties.yaml');
$loader->load('security.yaml');
$loader->load('services.yaml');
$loader->load('translation.yaml');
Expand Down
48 changes: 48 additions & 0 deletions src/Event/AbstractPreResponseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Event;

use Pimcore\Bundle\StudioBackendBundle\Util\Schema\AdditionalAttributesInterface;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @internal
*/
abstract class AbstractPreResponseEvent extends Event
{
public function __construct(protected readonly AdditionalAttributesInterface $responseObject)
{
}

public function hasAdditionalAttribute(string $key): bool {
return $this->responseObject->hasAdditionalAttribute($key);
}

public function getAdditionalAttribute(string $key): mixed
{
return $this->responseObject->getAdditionalAttribute($key);
}

public function addAdditionalAttribute(string $key, mixed $value): void
{
$this->responseObject->addAdditionalAttribute($key, $value);
}

public function removeAdditionalAttribute(string $key): void {
$this->responseObject->removeAdditionalAttribute($key);
}
}
32 changes: 32 additions & 0 deletions src/Exception/ElementSavingFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Exception;

/**
* @internal
*/
final class ElementSavingFailedException extends AbstractApiException
{
public function __construct(int $id, ?string $error = null)
{
parent::__construct(500, sprintf(
'Failed to save element with ID %s: %s',
$id,
$error ?? 'Unknown error'
));
}
}
31 changes: 31 additions & 0 deletions src/Exception/MaxFileSizeExceededException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Exception;

/**
* @internal
*/
final class MaxFileSizeExceededException extends AbstractApiException
{
public function __construct(int|float $maxFileSize)
{
parent::__construct(
413,
sprintf('Max file size of %d bytes exceeded', $maxFileSize)
);
}
}
31 changes: 31 additions & 0 deletions src/Exception/NotWriteableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Exception;

/**
* @internal
*/
final class NotWriteableException extends AbstractApiException
{
public function __construct(string $type)
{
parent::__construct(500, sprintf(
'Cannot create: %s',
$type
));
}
}
28 changes: 28 additions & 0 deletions src/Exception/PropertyNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Exception;

/**
* @internal
*/
final class PropertyNotFoundException extends AbstractApiException
{
public function __construct(string $id)
{
parent::__construct(404, 'Property with ID ' . $id . ' not found');
}
}
Loading

0 comments on commit 04febb6

Please sign in to comment.