-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Task][New feature] Object data preview (#675)
* First draft for preview * Apply php-cs-fixer changes * Refinement * Apply php-cs-fixer changes * Add permission * Split line * Add declare strict * Apply php-cs-fixer changes * Make internal
- Loading branch information
Showing
10 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?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\DataObject\Attribute\Request; | ||
|
||
use Attribute; | ||
use OpenApi\Attributes\JsonContent; | ||
use OpenApi\Attributes\RequestBody; | ||
use Pimcore\Bundle\StudioBackendBundle\DataObject\MappedParameter\PreviewParameter; | ||
|
||
#[Attribute(Attribute::TARGET_METHOD)] | ||
final class PreviewRequestBody extends RequestBody | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct( | ||
required: true, | ||
content: new JsonContent(ref: PreviewParameter::class) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?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\DataObject\Controller; | ||
|
||
use OpenApi\Attributes\Post; | ||
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; | ||
use Pimcore\Bundle\StudioBackendBundle\DataObject\Attribute\Request\PreviewRequestBody; | ||
use Pimcore\Bundle\StudioBackendBundle\DataObject\MappedParameter\PreviewParameter; | ||
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\PreviewUrlServiceInterface; | ||
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses; | ||
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\RedirectResponse as RedirectResponseAttribute; | ||
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags; | ||
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes; | ||
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\UserPermissions; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; | ||
use Symfony\Component\Routing\Attribute\Route; | ||
use Symfony\Component\Security\Http\Attribute\IsGranted; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
final class PreviewController extends AbstractApiController | ||
{ | ||
public function __construct( | ||
SerializerInterface $serializer, | ||
private PreviewUrlServiceInterface $previewUrlService | ||
) { | ||
parent::__construct($serializer); | ||
} | ||
|
||
#[Route('/data-objects/preview', name: 'pimcore_studio_api_data_objects_preview', methods: ['POST'])] | ||
#[IsGranted(UserPermissions::DATA_OBJECTS->value)] | ||
#[Post( | ||
path: self::PREFIX . '/data-objects/preview', | ||
operationId: 'data_object_preview_by_id', | ||
description: 'data_object_preview_by_id_description', | ||
summary: 'data_object_preview_by_id_summary', | ||
tags: [Tags::DataObjects->value] | ||
)] | ||
#[PreviewRequestBody] | ||
#[RedirectResponseAttribute(description: 'data_object_preview_by_id_success_response')] | ||
#[DefaultResponses([ | ||
HttpResponseCodes::REDIRECT, | ||
])] | ||
public function preview(#[MapRequestPayload] PreviewParameter $previewParameter): RedirectResponse | ||
{ | ||
return new RedirectResponse($this->previewUrlService->getPreviewUrl($previewParameter)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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\DataObject\MappedParameter; | ||
|
||
use OpenApi\Attributes\Property; | ||
use OpenApi\Attributes\Schema; | ||
|
||
#[Schema( | ||
title: 'Data Object Preview Parameters', | ||
required: ['id'], | ||
type: 'object' | ||
)] | ||
final readonly class PreviewParameter | ||
{ | ||
public function __construct( | ||
#[Property(description: 'ID', type: 'integer', example: 83)] | ||
private int $id, | ||
#[Property(description: 'Site', type: 'integer', default: 0, example: 1)] | ||
private int $site = 0 | ||
) { | ||
} | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getSite(): int | ||
{ | ||
return $this->site; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?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\DataObject\Service; | ||
|
||
use Exception; | ||
use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface; | ||
use Pimcore\Bundle\StudioBackendBundle\DataObject\MappedParameter\PreviewParameter; | ||
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException; | ||
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException; | ||
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\RequestTrait; | ||
use Pimcore\Model\DataObject\ClassDefinition\PreviewGeneratorInterface; | ||
use Pimcore\Model\DataObject\Concrete; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
/** | ||
* @interal | ||
*/ | ||
final readonly class PreviewUrlService implements PreviewUrlServiceInterface | ||
{ | ||
use RequestTrait; | ||
|
||
public function __construct( | ||
private RequestStack $requestStack, | ||
private PreviewGeneratorInterface $defaultPreviewGenerator, | ||
private ServiceResolverInterface $serviceResolver | ||
) { | ||
} | ||
|
||
/** | ||
* @throws Exception|NotFoundException | ||
*/ | ||
public function getPreviewUrl(PreviewParameter $previewParameter): string | ||
{ | ||
$session = $this->getCurrentSession($this->requestStack); | ||
|
||
$dataObject = $this->serviceResolver->getElementFromSession( | ||
'object', | ||
$previewParameter->getId(), | ||
$session->getId() | ||
); | ||
|
||
if (!$dataObject instanceof Concrete) { | ||
throw new NotFoundException('Data Object', $previewParameter->getId()); | ||
} | ||
|
||
$url = $this->getPreviewGenerator($dataObject)?->generatePreviewUrl( | ||
$dataObject, | ||
['preview' => true, 'context' => $this] | ||
); | ||
|
||
if (!$url) { | ||
throw new InvalidArgumentException('Could not generate preview url'); | ||
} | ||
|
||
return $this->buildRedirectUrl($url, $previewParameter); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
private function getPreviewGenerator(Concrete $dataObject): ?PreviewGeneratorInterface | ||
{ | ||
$previewService = $dataObject->getClass()->getPreviewGenerator(); | ||
|
||
if (!$previewService && $dataObject->getClass()->getLinkGenerator()) { | ||
$previewService = $this->defaultPreviewGenerator; | ||
} | ||
|
||
return $previewService; | ||
} | ||
|
||
private function buildRedirectUrl(string $url, PreviewParameter $previewParameter): string | ||
{ | ||
// replace all remaining % signs | ||
$url = str_replace('%', '%25', $url); | ||
$urlParts = parse_url($url); | ||
|
||
$redirectParameters = array_filter([ | ||
'pimcore_object_preview' => $previewParameter->getId(), | ||
'site' => $previewParameter->getSite(), | ||
'dc' => time(), | ||
]); | ||
|
||
$previewUrl = $urlParts['path'] . '?' . http_build_query($redirectParameters); | ||
|
||
if (isset($urlParts['query'])) { | ||
$previewUrl .= '&' . $urlParts['query']; | ||
} | ||
|
||
return $previewUrl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?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\DataObject\Service; | ||
|
||
use Pimcore\Bundle\StudioBackendBundle\DataObject\MappedParameter\PreviewParameter; | ||
|
||
/** | ||
* @interal | ||
*/ | ||
interface PreviewUrlServiceInterface | ||
{ | ||
public function getPreviewUrl(PreviewParameter $previewParameter): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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\OpenApi\Attribute\Response; | ||
|
||
use Attribute; | ||
use OpenApi\Attributes\Header; | ||
use OpenApi\Attributes\Response; | ||
use OpenApi\Attributes\Schema; | ||
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes; | ||
|
||
#[Attribute(Attribute::TARGET_METHOD)] | ||
final class RedirectResponse extends Response | ||
{ | ||
public function __construct(string $description = 'Redirect') | ||
{ | ||
parent::__construct( | ||
response: HttpResponseCodes::REDIRECT->value, | ||
description: $description, | ||
headers: [ | ||
new Header( | ||
header: 'Location', | ||
description: 'Redirect location', | ||
required: true, | ||
schema: new Schema( | ||
type: 'string', | ||
format: 'uri' | ||
) | ||
), | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters