Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Partially writing of resources #18

Closed
wants to merge 6 commits into from
Closed
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
8 changes: 3 additions & 5 deletions config/api_platform/resources/asset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ resources:
ApiPlatform\Metadata\Get:
normalizationContext:
groups: [ 'asset:read', 'asset:item:get', 'dependency:read', 'property:read', 'element:read', 'element:item:get']
ApiPlatform\Metadata\Put:
ApiPlatform\Metadata\Post:
ApiPlatform\Metadata\Delete:
ApiPlatform\Metadata\Patch:
provider: Pimcore\Bundle\StudioApiBundle\State\AssetProvider
provider: Pimcore\Bundle\StudioApiBundle\State\Asset\Provider
processor: Pimcore\Bundle\StudioApiBundle\State\Asset\Processor
normalizationContext:
groups: ['asset:read', 'dependency:read', 'property:read', 'element:read']
denormalizationContext:
groups: ['asset:write']
groups: ['asset:write', 'element:write']
properties:
id:
identifier: true
Expand Down
6 changes: 4 additions & 2 deletions config/api_platform/resources/asset/image.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
resources:
Pimcore\Bundle\StudioApiBundle\Dto\Asset\Image:
provider: Pimcore\Bundle\StudioApiBundle\State\AssetProvider
provider: Pimcore\Bundle\StudioApiBundle\State\Asset\Provider
processor: Pimcore\Bundle\StudioApiBundle\State\Asset\Processor
operations:
ApiPlatform\Metadata\Get:
normalizationContext:
groups: ['image:read', 'asset:read', 'asset:item:get', 'dependency:read', 'property:read', 'element:read', 'element:item:get' ]
ApiPlatform\Metadata\Patch:
properties:
id:
identifier: true
normalizationContext:
groups: ['image:read', 'asset:read', 'dependency:read', 'property:read', 'element:read']
denormalizationContext:
groups: ['image:write', 'asset:write']
groups: ['image:write', 'asset:write', 'element:write']
18 changes: 9 additions & 9 deletions config/serialization/asset.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
Pimcore\Bundle\StudioApiBundle\Dto\Asset:
attributes:
type:
groups: ['asset:read']
groups: ['asset:read', 'asset:write']
iconName:
groups: [ 'asset:read' ]
groups: [ 'asset:read' , 'asset:write']
children:
groups: ['asset:read']
groups: ['asset:read', 'asset:write']
filename:
groups: ['asset:read']
groups: ['asset:read', 'asset:write']
mimetype:
groups: ['asset:read']
groups: ['asset:read', 'asset:write']
metadata:
groups: ['asset:read']
groups: ['asset:read', 'asset:write']
customSettings:
groups: ['asset:item:get']
groups: ['asset:item:get', 'asset:write']
hasMetaData:
groups: ['asset:read']
groups: ['asset:read', 'asset:write']
userPermissions:
groups: ['asset:read']
groups: ['asset:read', 'asset:write']
14 changes: 4 additions & 10 deletions config/serialization/asset/image.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
Pimcore\Bundle\StudioApiBundle\Dto\Asset\Image:
attributes:
thumbnail:
groups: ['image:read', 'asset:read']
format:
groups: ['image:read', 'asset:read']
groups: ['image:read', 'asset:read', 'image:write']
dimensions:
groups: ['image:read', 'asset:read']
groups: ['image:read', 'asset:read', 'image:write']
width:
groups: ['image:read', 'asset:read']
groups: ['image:read', 'asset:read', 'image:write']
height:
groups: ['image:read', 'asset:read']
animated:
groups: ['image:read', 'asset:read']
vectorGraphic:
groups: ['image:read', 'asset:read']
groups: ['image:read', 'asset:read', 'image:write']
# lowQualityPreviewDataUri:
# groups: ['image:read']
# lowQualityPreviewPath:
Expand Down
34 changes: 17 additions & 17 deletions config/serialization/element.yaml
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
Pimcore\Bundle\StudioApiBundle\Dto\Element:
attributes:
id:
groups: ['element:read']
groups: ['element:read', 'element:write']
parentId:
groups: ['element:read']
groups: ['element:read', 'element:write']
permissions:
groups: ['element:read']
groups: ['element:read', 'element:write']
path:
groups: ['element:read']
groups: ['element:read', 'element:write']
creationDate:
groups: ['element:read']
groups: ['element:read', 'element:write']
modificationDate:
groups: ['element:read']
groups: ['element:read', 'element:write']
userOwner:
groups: ['element:read']
groups: ['element:read', 'element:write']
userModification:
groups: ['element:read']
groups: ['element:read', 'element:write']
properties:
groups: ['element:item:get']
groups: ['element:item:get', 'element:write']
versions:
groups: ['element:read']
groups: ['element:read', 'element:write']
locked:
groups: ['element:read']
groups: ['element:read', 'element:write']
lock:
groups: ['element:read']
groups: ['element:read', 'element:write']
dependencies:
groups: ['element:item:get']
groups: ['element:item:get', 'element:write']
scheduledTasks:
groups: ['element:item:get']
groups: ['element:item:get', 'element:write']
versionCount:
groups: ['element:item:get']
groups: ['element:item:get', 'element:write']
fullPath:
groups: ['element:read']
groups: ['element:read', 'element:write']
userPermissions:
groups: ['element:read']
groups: ['element:read', 'element:write']
23 changes: 22 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ services:
tags: [ 'controller.service_arguments' ]

# Providers
Pimcore\Bundle\StudioApiBundle\State\AssetProvider: ~
Pimcore\Bundle\StudioApiBundle\State\Asset\Provider: ~
Pimcore\Bundle\StudioApiBundle\State\UserProvider: ~
Pimcore\Bundle\StudioApiBundle\State\ScheduledTaskProvider: ~
Pimcore\Bundle\StudioApiBundle\State\VersionProvider: ~

# Processors
Pimcore\Bundle\StudioApiBundle\State\Asset\Processor: ~
Pimcore\Bundle\StudioApiBundle\State\ResetPasswordProcessor: ~
Pimcore\Bundle\StudioApiBundle\State\Token\Create\Processor: ~
Pimcore\Bundle\StudioApiBundle\State\Token\Refresh\Processor: ~
Expand Down Expand Up @@ -70,12 +71,27 @@ services:
Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\PermissionsHydratorInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\PermissionsHydrator

# Core Data
Pimcore\Bundle\StudioApiBundle\Service\CoreData\V1\Hydrator\Asset\ImageHydratorInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\CoreData\V1\Hydrator\Asset\ImageHydrator

Pimcore\Bundle\StudioApiBundle\Service\CoreData\V1\Hydrator\PermissionsHydratorInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\CoreData\V1\Hydrator\PermissionsHydrator

# Services
Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\AssetHydratorServiceInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\AssetHydratorService
arguments:
$assetHydratorLocator: '@generic_data_index.asset_hydrator.service_locator'

Pimcore\Bundle\StudioApiBundle\Service\CoreData\V1\Hydrator\AssetHydratorServiceInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\CoreData\V1\Hydrator\AssetHydratorService
arguments:
$assetHydratorLocator: '@pimcore_model_data.asset_hydrator.service_locator'

Pimcore\Bundle\StudioApiBundle\Service\AssetServiceInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\AssetService

Pimcore\Bundle\StudioApiBundle\Service\AssetSearchServiceInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\AssetSearchService

Expand Down Expand Up @@ -126,3 +142,8 @@ services:
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Text: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\TextHydratorInterface'
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Unknown: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\UnknownHydratorInterface'
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Video: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\VideoHydratorInterface'

pimcore_model_data.asset_hydrator.service_locator:
class: Symfony\Component\DependencyInjection\ServiceLocator
arguments:
- Pimcore\Model\Asset\Image: '@Pimcore\Bundle\StudioApiBundle\Service\CoreData\V1\Hydrator\Asset\ImageHydratorInterface'
9 changes: 7 additions & 2 deletions src/Dto/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(
private readonly string $iconName,
private readonly bool $hasChildren,
private readonly string $type,
private readonly string $filename,
private string $filename,
private readonly ?string $mimeType,
private readonly array $metaData,
private readonly bool $workflowWithPermissions,
Expand Down Expand Up @@ -70,11 +70,16 @@ public function hasWorkflowWithPermissions(): bool
return $this->workflowWithPermissions;
}

public function getFilename(): ?string
public function getFilename(): string
{
return $this->filename;
}

public function setFilename(string $filename): void
{
$this->filename = $filename;
}

public function getType(): string
{
return $this->type;
Expand Down
5 changes: 1 addition & 4 deletions src/Dto/Asset/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@

use Pimcore\Bundle\StudioApiBundle\Dto\Asset;

class Image extends Asset
final class Image extends Asset
{
//use MetaData\EmbeddedMetaDataTrait;

public function __construct(
private readonly string $format,
private readonly int $width,
private readonly int $height,
private readonly bool $vectorGraphic,
private readonly bool $animated,
private readonly string $thumbnailPath,
string $iconName,
bool $hasChildren,
Expand Down Expand Up @@ -77,7 +74,7 @@

public function getFormat(): string
{
return $this->format;

Check failure on line 77 in src/Dto/Asset/Image.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, false)

Access to an undefined property Pimcore\Bundle\StudioApiBundle\Dto\Asset\Image::$format.

Check failure on line 77 in src/Dto/Asset/Image.php

View workflow job for this annotation

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

Access to an undefined property Pimcore\Bundle\StudioApiBundle\Dto\Asset\Image::$format.
}

public function getWidth(): int
Expand All @@ -92,12 +89,12 @@

public function isVectorGraphic(): bool
{
return $this->vectorGraphic;

Check failure on line 92 in src/Dto/Asset/Image.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, false)

Access to an undefined property Pimcore\Bundle\StudioApiBundle\Dto\Asset\Image::$vectorGraphic.

Check failure on line 92 in src/Dto/Asset/Image.php

View workflow job for this annotation

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

Access to an undefined property Pimcore\Bundle\StudioApiBundle\Dto\Asset\Image::$vectorGraphic.
}

public function isAnimated(): bool
{
return $this->animated;

Check failure on line 97 in src/Dto/Asset/Image.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, false)

Access to an undefined property Pimcore\Bundle\StudioApiBundle\Dto\Asset\Image::$animated.

Check failure on line 97 in src/Dto/Asset/Image.php

View workflow job for this annotation

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

Access to an undefined property Pimcore\Bundle\StudioApiBundle\Dto\Asset\Image::$animated.
}
//
// public function getLowQualityPreviewPath(): string
Expand Down
7 changes: 6 additions & 1 deletion src/Dto/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Element
{
public function __construct(
private readonly int $id,
private readonly int $parentId,
private int $parentId,
private readonly string $path,
private readonly int $userOwner,
private readonly int $userModification,
Expand All @@ -45,6 +45,11 @@ public function getParentId(): int
return $this->parentId;
}

public function setParentId(int $parentId): void
{
$this->parentId = $parentId;
}

public function getPath(): string
{
return $this->path;
Expand Down
79 changes: 79 additions & 0 deletions src/Service/AssetService.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\StudioApiBundle\Service;

use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\SynchronousProcessingServiceInterface;
use Pimcore\Bundle\StaticResolverBundle\Models\Asset\AssetResolverInterface;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Model\Asset as CoreAsset;
use Pimcore\Model\Element\DuplicateFullPathException;
use Pimcore\Model\Exception\NotFoundException;

final readonly class AssetService implements AssetServiceInterface
{
public function __construct(
private AssetResolverInterface $assetResolver,
private SynchronousProcessingServiceInterface $synchronousProcessingService
) {
}

/**
* @throws DuplicateFullPathException
*/
public function processAsset(Asset $data): CoreAsset
{
$asset = $this->assetResolver->getById($data->getId());

if(!$asset) {
throw new NotFoundException('Asset not found');
}

$differences = $this->compare($asset, $data);
foreach($differences as $field => $difference) {
$setter = 'set' . $field;
$asset->$setter($difference['new']);
}
$this->synchronousProcessingService->enable();
$asset->save();

return $asset;
}

private function compare(CoreAsset $current, Asset $data): array
{
$fieldsToCheck = $this->fieldsToCheck(get_class_methods($data));
$differences = [];
foreach($fieldsToCheck as $field) {
if($current->$field() !== $data->$field()) {
$fieldName = str_replace('get', '', $field);
$differences[$fieldName] = [
'current' => $current->$field(),
'new' => $data->$field(),
];
}
}

return $differences;
}

private function fieldsToCheck(array $methods): array
{
$setters = array_filter($methods, static fn ($method) => str_starts_with($method, 'set'));

return array_map(static fn ($setter) => str_replace('set', 'get', $setter), $setters);
}
}
25 changes: 25 additions & 0 deletions src/Service/AssetServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\StudioApiBundle\Service;

use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Model\Asset as CoreAsset;

interface AssetServiceInterface
{
public function processAsset(Asset $data): CoreAsset;
}
Loading
Loading