Skip to content

Commit

Permalink
Add POC elements
Browse files Browse the repository at this point in the history
  • Loading branch information
alexz707 committed Jan 26, 2024
1 parent 7f220cd commit a821a5b
Show file tree
Hide file tree
Showing 11 changed files with 416 additions and 1 deletion.
31 changes: 31 additions & 0 deletions config/api_platform/resources/asset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
resources:
Pimcore\Model\Asset:
provider: Pimcore\Bundle\StudioApiBundle\State\AssetProvider
normalizationContext:
groups: ['get']
denormalizationContext:
groups: ['set']
properties:
id:
identifier: true
parentId: ~
type: ~
data: ~
fullPath: ~

Pimcore\Model\Asset\Image:
provider: Pimcore\Bundle\StudioApiBundle\State\AssetProvider
properties:
id:
identifier: true
normalizationContext:
groups: ['get']
denormalizationContext:
groups: ['set']

Pimcore\Model\Asset\Image\Thumbnail:
provider: Pimcore\Bundle\StudioApiBundle\State\AssetProvider
normalizationContext:
groups: ['get']
denormalizationContext:
groups: ['set']
19 changes: 19 additions & 0 deletions config/api_platform/resources/user.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
resources:
Pimcore\Model\User:
operations:
ApiPlatform\Metadata\Post:
status: 202
processor: Pimcore\Bundle\StudioApiBundle\State\ResetPasswordProcessor
input: Pimcore\Bundle\StudioApiBundle\Dto\ResetPasswordRequest
output: false
uriTemplate: '/users/reset-password'
# ApiPlatform\Metadata\Get:
# controller: ApiPlatform\Action\NotFoundAction
# read: false
# output: false


normalizationContext:
groups: [ 'get' ]
denormalizationContext:
groups: [ 'set' ]
4 changes: 4 additions & 0 deletions config/pimcore/routing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
api_platform:
resource: .
type: api_platform
prefix: /api
64 changes: 64 additions & 0 deletions config/serialization/asset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Pimcore\Model\Asset:
attributes:
id:
groups: ['get', 'bla:read']
parentId:
groups: ['get']
type:
groups: ['get']
filename:
groups: ['get']
path:
groups: ['get']
mimetype:
groups: ['get']
creationDate:
groups: ['get']
modificationDate:
groups: ['get']
userOwner:
groups: ['get']
userModification:
groups: ['get']
properties:
groups: ['get']
versions:
groups: ['get']
metadata:
groups: ['get']
locked:
groups: ['get']
customSettings:
groups: ['get']
hasMetaData:
groups: ['get']
dependencies:
groups: ['get']
scheduledTasks:
groups: ['get']
versionCount:
groups: ['get']
fullPath:
groups: ['get']

Pimcore\Model\Asset\Image:
attributes:
thumbnail:
groups: ['get']
format:
groups: ['get']
dimensions:
groups: ['get']
width:
groups: [ 'get' ]
height:
groups: [ 'get' ]

Pimcore\Model\Asset\Image\Thumbnail:
attributes:
path:
groups: ['get']
imageTag:
groups: ['get']
media:
groups: ['get']
4 changes: 4 additions & 0 deletions config/serialization/user.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Pimcore\Bundle\StudioApiBundle\Dto\ResetPasswordRequest:
attributes:
username:
groups: ['get', 'set']
9 changes: 8 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ services:
Pimcore\Bundle\StudioApiBundle\Controller\:
resource: '../src/Controller'
public: true
tags: [ 'controller.service_arguments' ]
tags: [ 'controller.service_arguments' ]

Pimcore\Bundle\StudioApiBundle\State\AssetProvider: ~
Pimcore\Bundle\StudioApiBundle\State\ResetPasswordProcessor: ~

Pimcore\Bundle\StudioApiBundle\Serializer\AssetNormalizer:
tags:
- { name: 'serializer.normalizer' }
69 changes: 69 additions & 0 deletions src/DependencyInjection/PimcoreStudioApiExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\DependencyInjection;

use Exception;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;


/**
* This is the class that loads and manages your bundle configuration.
*
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html
*/

/**
* @internal
*/
class PimcoreStudioApiExtension extends Extension implements PrependExtensionInterface
{
/**
* {@inheritdoc}
*
* @throws Exception
*/
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

// Load services and configuration
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
$loader->load('services.yaml');

// Set default serializer mapping if not provided in the app's config
if (!isset($config['serializer']['mapping']['paths'])) {
$config['serializer']['mapping']['paths'] = [__DIR__ . '/../../config/serialization'];
}

// Pass the configuration to the custom normalizer
$container->setParameter('pimcore_studio_api.serializer.mapping.paths', $config['serializer']['mapping']['paths']);

Check warning on line 55 in src/DependencyInjection/PimcoreStudioApiExtension.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Line is longer than allowed by code style

Line is longer than allowed by code style (\> 120 columns)

Check warning on line 55 in src/DependencyInjection/PimcoreStudioApiExtension.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Line is longer than allowed by code style

Line is longer than allowed by code style (\> 120 columns)
}

public function prepend(ContainerBuilder $container): void
{
$apiPlatformConfig = [
"mapping"=>[

Check notice on line 61 in src/DependencyInjection/PimcoreStudioApiExtension.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Unnecessary double quotes

\[EA\] Safely use single quotes instead.
"paths"=> [

Check notice on line 62 in src/DependencyInjection/PimcoreStudioApiExtension.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Unnecessary double quotes

\[EA\] Safely use single quotes instead.
__DIR__ . '/../../config/api_platform/'
]
]
];
$container->prependExtensionConfig('api_platform', $apiPlatformConfig);
}
}
17 changes: 17 additions & 0 deletions src/Dto/ResetPasswordRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

namespace Pimcore\Bundle\StudioApiBundle\Dto;

use Symfony\Component\Validator\Constraints as Assert;

class ResetPasswordRequest
{
#[Assert\NotBlank]
public string $username;

public function getUsername(): string
{
return $this->username;
}
}
53 changes: 53 additions & 0 deletions src/Serializer/AssetNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

Check failure on line 1 in src/Serializer/AssetNormalizer.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Missing strict types declaration

Strict types declaration is missing

namespace Pimcore\Bundle\StudioApiBundle\Serializer;

use Pimcore\Model\Asset;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class AssetNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;

private const ALREADY_CALLED = 'ASSET_NORMALIZER_ALREADY_CALLED';

public function __construct(private RequestStack $requestStack)

Check failure on line 17 in src/Serializer/AssetNormalizer.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, 11.x-dev as 11.99.9, true)

Property Pimcore\Bundle\StudioApiBundle\Serializer\AssetNormalizer::$requestStack is never read, only written.

Check failure on line 17 in src/Serializer/AssetNormalizer.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.1, lowest, false)

Property Pimcore\Bundle\StudioApiBundle\Serializer\AssetNormalizer::$requestStack is never read, only written.

Check notice on line 17 in src/Serializer/AssetNormalizer.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Property can be 'readonly'

Property can be 'readonly'
{
}

public function normalize($object, $format = null, array $context = array()): array

Check notice on line 21 in src/Serializer/AssetNormalizer.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Traditional syntax array literal detected

Traditional syntax array literal used
{
$context[self::ALREADY_CALLED] = true;

$data = $this->normalizer->normalize($object, $format, $context);

if (isset($data['data']) && $data['data']) {
$data['data'] = base64_encode($data['data']);
}

if ($object instanceof Asset\Image) {
$data['thumbnail'] = $object->getThumbnail()->getPath(['frontend' => true]);
}

return $data;
}

public function supportsNormalization($data, $format = null, array $context = []): bool
{
if (isset($context[self::ALREADY_CALLED])) {
return false;
}

return $data instanceof Asset;
}

public function getSupportedTypes(?string $format): array
{
return [
Asset::class => false,
];
}
}
33 changes: 33 additions & 0 deletions src/State/AssetProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);

namespace Pimcore\Bundle\StudioApiBundle\State;

use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use Pimcore\Model\Asset;
use Pimcore\Model\Element\Tag;

Check notice on line 10 in src/State/AssetProvider.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Unused import

Import 'Pimcore\\Model\\Element\\Tag' is never used

final class AssetProvider implements ProviderInterface
{

public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
// collection of assets, needs to be further investigated
if ($operation instanceof CollectionOperationInterface) {
$assetListing = new Asset\Listing();
$assetListing->setLimit(10);

if (isset($context['filters']['page'])) {
$assetListing->setOffset(10 * $context['filters']['page']);
}
return $assetListing;
}
// getting a single asset by id
$test = Asset::getById($uriVariables['id']);

Check warning on line 28 in src/State/AssetProvider.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

One-time use variables

\[EA\] Variable $test is redundant.

Check notice on line 28 in src/State/AssetProvider.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Unnecessary local variable

Unnecessary local variable

//$tag = Tag::getTagsForElement('asset', $test->getId());
return $test;
}
}
Loading

0 comments on commit a821a5b

Please sign in to comment.